如何在 R 資料框中對所有列執行 Wilcoxon 檢驗?


在 R 資料框中對所有列執行 Wilcoxon 檢驗,意味著我們要對單個樣本使用該檢驗,單個樣本的 Wilcoxon 檢驗用於檢驗樣本的中位數,中位數是否等於某個值。如果我們不提供任何值,則零為參考值。可以透過藉助 apply 函式和 wilcox.test 來對所有列執行 Wilcoxon 檢驗,如下面的示例所示。

考慮以下資料框 −

示例

 線上演示

x1<-rnorm(20,5,0.31)
x2<-rnorm(20,2,0.025)
x3<-rpois(20,4)
x4<-rpois(20,2)
x5<-rpois(20,5)
x6<-rpois(20,1)
x7<-round(rnorm(20,3,1.1),2)
x8<-round(rnorm(20,10,2.25),2)
df<-data.frame(x1,x2,x3,x4,x5,x6,x7,x8)
df

輸出

     x1      x2        x3  x4  x5  x6    x7   x8
1 4.667097  2.032878   5   1   8   0   3.82   5.68
2 4.556913  1.952845   7   3  5    0   4.62   12.57
3 5.274511  1.947305   3   0  2    1   3.27   7.03
4 4.621090  1.960653   4   3  4    0   3.37   9.71
5 4.808041  1.955832   4   3  4    0   2.96   7.58
6 4.509070  2.084535   2   4  10   1   3.04   6.42
7 5.230658  1.981629   2   2  5   0    2.26   7.25
8 4.724433  1.986739   3   2  5   0   3.76   10.46
9 5.123489  1.959177   3   1  1   0   3.97   10.55
10 5.179769  1.970168  3  3   4   0   3.91   7.68
11 5.133287  2.006720  5   1   7   0  1.89   10.85
12 4.677813  2.007699  3   0   6   2  1.81   9.01
13 4.662342  2.064619  7   2  5    3  2.72   8.42
14 5.375585  1.994618  2   1  3    0  4.09   9.83
15 5.574414  1.997730  2   4   3    0 3.14   9.58
16 5.279330  1.985777  8   4   10  2  2.95   9.60
17 5.258145  2.019408  2   3   6   1  3.42   10.68
18 5.051640  2.017030  6   3   4   2  3.91   11.66
19 5.064925  2.007080  4   1   6   0  3.06   8.74
20 4.957406  1.964513  9   2   5   0  4.59   11.11

對 df 的所有列執行 Wilcoxon 檢驗 −

apply(df,2,wilcox.test)
$x1
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0
$x2
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0
$x3
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 8.966e-05
alternative hypothesis: true location is not equal to 0
$x4
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 171, p-value = 0.0001896
alternative hypothesis: true location is not equal to 0
$x5


Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 9.095e-05
alternative hypothesis: true location is not equal to 0
$x6
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 28, p-value = 0.0206
alternative hypothesis: true location is not equal to 0
$x7
Wilcoxon signed rank test with continuity correction
data: newX[, i]
V = 210, p-value = 9.556e-05
alternative hypothesis: true location is not equal to 0
$x8
Wilcoxon signed rank exact test
data: newX[, i]
V = 210, p-value = 1.907e-06
alternative hypothesis: true location is not equal to 0

警告資訊 −

1: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
2: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
3: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with zeroes
4: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
5: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties
6: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with zeroes
7: In wilcox.test.default(newX[, i], ...) :
cannot compute exact p-value with ties

更新於:06-2-2021

2K+ 閱讀量

開啟您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.