12

python 的 scipy.stats.ranksums 和 R 的 wilcox.test 都应该计算 Wilcoxon 秩和检验的两侧 p 值。但是当我在相同的数据上运行这两个函数时,我得到的 p 值相差几个数量级:

回复:

> x=c(57.07168,46.95301,31.86423,38.27486,77.89309,76.78879,33.29809,58.61569,18.26473,62.92256,50.46951,19.14473,22.58552,24.14309)
> y=c(8.319966,2.569211,1.306941,8.450002,1.624244,1.887139,1.376355,2.521150,5.940253,1.458392,3.257468,1.574528,2.338976)
> print(wilcox.test(x, y))

        Wilcoxon rank sum test

data:  x and y 
W = 182, p-value = 9.971e-08
alternative hypothesis: true location shift is not equal to 0 

Python:

>>> x=[57.07168,46.95301,31.86423,38.27486,77.89309,76.78879,33.29809,58.61569,18.26473,62.92256,50.46951,19.14473,22.58552,24.14309]
>>> y=[8.319966,2.569211,1.306941,8.450002,1.624244,1.887139,1.376355,2.521150,5.940253,1.458392,3.257468,1.574528,2.338976]
>>> scipy.stats.ranksums(x, y)
(4.415880433163923, 1.0059968254463979e-05)

所以 R 给了我 1e-7 而 Python 给了我 1e-5。

这种差异从何而来,哪一个是“正确”的 p 值?

4

1 回答 1

21

这取决于选项的选择(精确与正常近似,有或没有连续性校正):

R的默认值:

默认情况下(如果未指定 'exact'),如果样本包含的有限值少于 50 个且不存在平局,则会计算精确的 p 值。否则,使用正态近似。

默认(如上图):

wilcox.test(x, y)

    Wilcoxon rank sum test

data:  x and y 
W = 182, p-value = 9.971e-08
alternative hypothesis: true location shift is not equal to 0 

具有连续性校正的正态近似:

> wilcox.test(x, y, exact=FALSE, correct=TRUE)

    Wilcoxon rank sum test with continuity correction

data:  x and y 
W = 182, p-value = 1.125e-05
alternative hypothesis: true location shift is not equal to 0 

没有连续性校正的正态近似:

> (w0 <- wilcox.test(x, y, exact=FALSE, correct=FALSE))

    Wilcoxon rank sum test

data:  x and y 
W = 182, p-value = 1.006e-05
alternative hypothesis: true location shift is not equal to 0 

为了更精确:

w0$p.value
[1] 1.005997e-05

看起来 Python 给你的另一个值(4.415880433163923)是 Z 分数:

2*pnorm(4.415880433163923,lower.tail=FALSE)
[1] 1.005997e-05

我很想知道发生了什么,但我还要指出,p=1e-7p=1e-5...之间几乎没有任何实际区别。

于 2012-10-09T11:45:05.907 回答