El *_*ude 2 python r scipy binomial-cdf
我正在尝试重新实现R用Python. 但是,我不确定我是否使用了正确的功能。
在R,我得到:
> binom.test (2, 8, 11/2364, alternative = "greater")
0.25
Run Code Online (Sandbox Code Playgroud)
使用Python& SciPy,我使用
from scipy.stats import binom
binom.sf(2, 8, float(11)/float(2364))
5.5441613055814931e-06
Run Code Online (Sandbox Code Playgroud)
事实上,我必须binom.sf(2, 8, float(11)/float(2364))确保第三个参数不是0因为 int 除法。
为什么值不同?我是否必须指定 Scipy / 的时刻binom.sf?我应该使用其他图书馆吗?
这是我在 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
>
Run Code Online (Sandbox Code Playgroud)
请注意,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
Run Code Online (Sandbox Code Playgroud)
所以这与 R 一致。
要使用 的生存函数scipy.stats.binom,您必须调整第一个参数(如 Marius 的评论中所述):
In [27]: from scipy.stats import binom
In [28]: binom.sf(1, 8, 11/2364)
Out[28]: 0.00059505960517880572
Run Code Online (Sandbox Code Playgroud)
(我使用的是 Python 3,所以11/2364等于0.004653130287648054。如果您使用的是 Python 2,请确保将该分数写为11.0/2364或float(11)/2364。)