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

Joh*_*ydx -1 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
Run Code Online (Sandbox Code Playgroud)

我也尝试了下面的代码,但给出了错误报告

wilcox.test(x1 ~ x2)
Error in wilcox.test.formula(x1 ~ x2) : 
grouping factor must have exactly 2 levels
Run Code Online (Sandbox Code Playgroud)

我的问题是-这是处理此数据的正确方法吗?-我认为它正在进行秩和检验-这就是我应该得到的。p值表明两个数据集之间存在显着差异。

rns*_*nso 5

以下使用相同的数据演示了公式在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
Run Code Online (Sandbox Code Playgroud)

结果与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
Run Code Online (Sandbox Code Playgroud)