如何进行包含多重观察的学生t检验

How to perform student t test contain mulitple observation

这是我的数据,9个变量的900000个obs。 我试过 apply 函数但无法在 apply 函数中提供参数。 数据看起来像这样。

ID A1 A2 A3 A4 A5 B1 B2 B3 B4
1  10 12 11 13 15 50 55 56 57
2  20 22 23 21 20 60 76 78 71
3  10 12 13 15 14 50 55 52 53
...
90000 11 12 13 15 12 21 22 23 24

我需要对这 9 个变量进行 900000 次样本学生 t 检验,分为 2 组(A 组和 B 组)。 任何人都可以 post 这里的代码吗?

编辑:感谢您的评论,我做了以下更改。 样本数据

testx <- structure(list(RAS = c(0.554246173201929, 0.292104162206435, 
0.201932255556074), RASSYX2 = c(0.673628450549317, 0.370730964566956, 
0.240868661848041), RASSYX3 = c(0.592972062397773, 0.387737676651884, 
0.258971711587807)), .Names = c("RAS", "RASSYX2", "RASSYX3"), row.names =c(NA, 
3L), class = "data.frame")

testy <- structure(list(test2 = c(0.682230776398731, 0.299007374701463, 
0.21735652533812), test3 = c(0.660308325914822, 0.340956947569367, 
0.255153956615115), test4 = c(0.625506839884405, 0.281695127521423, 
0.265769288207206)), .Names = c("test2", "test3", "test4"), row.names = c(NA, 
3L), class = "data.frame")

testx的第1行应该和testy的第1行比较,会有900000行,我只需要让这个测试自动化900000次。 所以希望做95%置信度的双侧等方差t检验。

我试过了,但显然 y 不是我想要测试的。

apply(testx,1,t.test,testy)

感谢您澄清您的问题。在您使用模拟数据进行澄清之前,我编写了以下解决方案。

这里是模拟数据集。如果你的数据是宽格式的,你真的应该考虑把它变成长格式......除非你正在做一个你没有提到的配对测试。

set.seed(1)

d<-data.frame(PatID=1:100, 
              group=rep(c('A','B'),50),
              Var1=rnorm(100, 500, 20),
              Var2=rnorm(100, 500, 20),
              Var3=rnorm(100, 500, 20),
              Var4=rnorm(100, 500, 20))

现在我们遍历要测试的列名列表并执行测试。

vars_to_test<-c('Var1','Var2','Var3','Var4')

t_res<-lapply(vars_to_test, function(var){ t.test( d[,var] ~ d[,'group'])})

names(t_res)<-vars_to_test

t_res 现在是列表的列表...每个 t 检验一个元素。因为我命名了 t_res 的元素,所以我可以轻松访问我的任何变量的测试结果:

在这种情况下,我访问 t 检验的 p 值,测试 A 组和 B 组之间均值 Var1 的差异:

> t_res[['Var1']]$p.value
[1] 0.3373045