gpt4 book ai didi

python 的 scipy.stats.ranksums 与 R 的 wilcox.test

转载 作者:太空狗 更新时间:2023-10-29 17:41:57 28 4
gpt4 key购买 nike

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 值?

最佳答案

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

R的默认值:

By default (if ‘exact’ is not specified), an exact p-value is computed if the samples contain less than 50 finite values and there are no ties. Otherwise, a normal approximation is used.

默认(如上图):

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 ...

关于python 的 scipy.stats.ranksums 与 R 的 wilcox.test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797658/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com