如何对R中的多列执行配对t检验?
当我们在 R 数据框中有一个具有两个级别和多个数值列的因子列时,我们可以对这个数据框应用配对测试,但必须为相同的主题收集数据,否则它将不是配对数据。t.test这里讨论的数据的应用可以通过使用命令 lapply(df[-1], function(x)t.test(x~df$group)) 来完成,其中 group 是因子列,位于数据框中的第一个位置, x 包含数据框中的所有数字列,所有这些列都存储在名为 df 的数据框中。
示例
考虑以下数据框 -
x1<-sample(c("A","B"),20,replace=TRUE)输出结果y1<-rpois(20,5)
y2<-rpois(20,2)
y3<-rpois(20,10)
y4<-rpois(20,3)
y5<-rpois(20,2)
df<-data.frame(x1,y1,y2,y3,y4,y5)
df
x1 y1 y2 y3 y4 y51 A 5 0 9 3 0
2 B 4 1 6 1 1
3 B 4 2 10 1 1
4 A 3 0 13 2 1
5 A 6 1 10 0 2
6 A 6 1 16 4 2
7 A 10 3 9 5 1
8 B 5 0 16 0 2
9 B 2 1 10 3 3
10 B 3 2 11 2 3
11 A 3 1 9 1 2
12 B 7 1 8 2 2
13 A 6 0 4 0 5
14 B 9 2 15 1 5
15 A 4 1 8 1 2
16 B 7 0 11 3 2
17 B 2 5 9 3 2
18 A 7 2 14 3 2
19 B 3 3 13 0 0
20 A 5 2 9 3 5
使用t.test因子列 x1 在数据框 df1 的多列上配对应用-
示例
lapply(df[-1], function(x) t.test(x~df$x1))输出结果
$y1Welch Two Sample t-test
data: x by df$x1
t = 0.90555, df = 17.683, p-value = 0.3773
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.190729 2.990729
sample estimates:
mean in group A mean in group B
5.5 4.6
$y2
Welch Two Sample t-test
data: x by df$x1
t = -1.057, df = 15.664, p-value = 0.3065
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.8054596 0.6054596
sample estimates:
mean in group A mean in group B
1.1 1.7
$y3
Welch Two Sample t-test
data: x by df$x1
t = -0.55089, df = 17.802, p-value = 0.5886
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.853392 2.253392
sample estimates:
mean in group A mean in group B
10.1 10.9
$y4
Welch Two Sample t-test
data: x by df$x1
t = 0.92338, df = 16.062, p-value = 0.3695
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.7770541 1.9770541
sample estimates:
mean in group A mean in group B
2.2 1.6
$y5
Welch Two Sample t-test
data: x by df$x1
t = 0.14907, df = 17.521, p-value = 0.8832
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.312111 1.512111
sample estimates:
mean in group A mean in group B
2.2 2.1
以上是 如何对R中的多列执行配对t检验? 的全部内容, 来源链接: utcz.com/z/341448.html