在R中对未配对数据执行wilcoxon测试

时间:2014-09-25 11:06:33

标签: r

我正在尝试使用R中的wilcox测试来确定两个未配对数据集之间是否存在显着差异,如下所示。我知道下面的数据是正常分布的,但我的原始数据不是正常分布的。

set.seed(1)
x1 <- rnorm(10, 4, 1)
x2 <- rnorm(10, 7, 1)

wilcox.test(x1, x2)


Wilcoxon rank sum test

data:  x1 and x2
W = 1, p-value = 2.165e-05
alternative hypothesis: true location shift is not equal to 0

我也尝试了下面的代码,但它提供了一个错误报告

wilcox.test(x1 ~ x2)
Error in wilcox.test.formula(x1 ~ x2) : 
grouping factor must have exactly 2 levels

我的问题是 - 这是对这些数据的正确方法吗? - 我认为它正在进行排序和测试 - 这就是我应该得到的。 p值表明两个数据集之间存在显着差异。

1 个答案:

答案 0 :(得分:4)

以下演示了使用相同数据在wilcox.test中使用公式:

> dd = data.frame(x1,x2)
> library(reshape2)
> melt(dd)
No id variables; using all as measure variables
   variable    value
1        x1 3.373546
2        x1 4.183643
3        x1 3.164371
4        x1 5.595281
5        x1 4.329508
6        x1 3.179532
7        x1 4.487429
8        x1 4.738325
9        x1 4.575781
10       x1 3.694612
11       x2 8.511781
12       x2 7.389843
13       x2 6.378759
14       x2 4.785300
15       x2 8.124931
16       x2 6.955066
17       x2 6.983810
18       x2 7.943836
19       x2 7.821221
20       x2 7.593901
> with(melt(dd), wilcox.test(value~variable))
No id variables; using all as measure variables

        Wilcoxon rank sum test

data:  value by variable
W = 1, p-value = 2.165e-05
alternative hypothesis: true location shift is not equal to 0

结果与wilcox.test(x1,x2)

相同
> wilcox.test(x1,x2)

        Wilcoxon rank sum test

data:  x1 and x2
W = 1, p-value = 2.165e-05
alternative hypothesis: true location shift is not equal to 0