【问题标题】:Binomial test in Python vs RPython 与 R 中的二项式检验
【发布时间】:2017-11-09 16:25:45
【问题描述】:

我正在尝试使用Python 重新实现最初在R 中开发的二项式检验。但是,我不确定我是否使用了正确的功能。

R,我得到:

> binom.test (2, 8, 11/2364, alternative = "greater")
0.25

对于Python & SciPy,我使用

from scipy.stats import binom
binom.sf(2, 8, float(11)/float(2364))
5.5441613055814931e-06

实际上我必须做binom.sf(2, 8, float(11)/float(2364)) 来确保第三个参数不是0 因为int 除法。

为什么这些值不同?我必须指定 Scipy / binom.sf 的时刻吗? 我应该使用其他库吗?

【问题讨论】:

  • scipyscipy.stats.binom_test,所以不需要使用生存功能。要获得与binom.sf 相同的结果,您需要binom.sf(1, 8, float(11)/float(2364)),因为您想包含 2 的概率。
  • 0 是草稿中未删除的行,抱歉...

标签: python r scipy binomial-cdf


【解决方案1】:

这是我在 R 中得到的:

> binom.test(2, 8, 11/2364, alternative = "greater")

    Exact binomial test

data:  2 and 8
number of successes = 2, number of trials = 8, p-value = 0.0005951
alternative hypothesis: true probability of success is greater than 0.00465313
95 percent confidence interval:
 0.04638926 1.00000000
sample estimates:
probability of success 
                  0.25 

>

请注意,p 值为 0.0005951。

将其与scipy.stats.binom_test(仅返回 p 值)的结果进行比较:

In [25]: from scipy.stats import binom_test

In [26]: binom_test(2, 8, 11/2364, alternative='greater')
Out[26]: 0.00059505960517880572

所以这与 R 一致。

要使用scipy.stats.binom 的生存功能,您必须调整第一个参数(如 Marius 的评论中所述):

In [27]: from scipy.stats import binom

In [28]: binom.sf(1, 8, 11/2364)
Out[28]: 0.00059505960517880572

(我使用的是 Python 3,所以 11/2364 等于 0.004653130287648054。如果您使用的是 Python 2,请务必将该分数写为 11.0/2364float(11)/2364。)

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多