如何在 R 中对多列执行配对 t 检验?
如果一个 R 数据框架中存在一个具有两个级别以及多个数值列的因子列,则可以对此数据框架应用配对 t 检验,但必须为同一主题收集数据,否则将不会是配对数据。此处讨论的数据上的 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 y5 1 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
对数据框架 df1 的多个列应用配对 t 检验,其中包含因子列 x1 −
示例
lapply(df[-1], function(x) t.test(x~df$x1))
输出
$y1 Welch 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
广告