如何在 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

更新於: 2021-03-16

2K+ 瀏覽

開啟您的求職之路

完成課程並獲得認證

開始學習
廣告