Python和R中的二项式检验

3

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

在R中,我得到:

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

使用 PythonSciPy,我可以完成以下操作:

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

为什么这些值不同?我是否需要为Scipy / binom.sf指定时刻? 我应该使用其他库吗?


3
Scipyscipy.stats.binom_test函数,因此不需要使用生存函数。如果要使用binom.sf得到相同的结果,则需要使用binom.sf(1, 8, float(11)/float(2364)),因为您希望包括2的概率。 - Marius
0 是草稿中未删除的一行,抱歉... - El Dude
1个回答

8

这是我在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接