在阅读了最近一篇关于泊松分布应用程序的博文后,我尝试使用 Python 的“scipy.stats”模块以及 Excel/LibreOffice 的“POISSON”和“CHITEST”函数来重现它的发现。
对于文章中显示的预期值,我简单地使用了:
import scipy.stats
for i in range(8):
print(scipy.stats.poisson.pmf(i, 2)*31)
这重现了博客文章中显示的表格 - 我还在 LibreOffice 中重新创建了它,使用单元格 A1、A2、...、A8 中值为 0 到 7 的第一列 A 和简单的公式 '=POISSON( A1, 2, 0)*31' 在 B 列的前 8 行重复。
到目前为止一切顺利 - 现在是卡方 p 检验值:
在 LibreOffice 下,我只是在单元格 C1-C8 中写下观察到的值,并使用 '=CHITEST(C1:C8, B1:B8)' 来重现文章报告的 p 值 0.18。但是,在 scipy.stats 下,我似乎无法重现此值:
import numpy as np
import scipy.stats
obs = [4, 10, 7, 5, 4, 0, 0, 1]
exp = [scipy.stats.poisson.pmf(i, 2)*31 for i in range(8)]
# we only estimated one variable (the rate of 2 killings per year via 62/31)
# so dof will be N-1-estimates
estimates = 1
print(scipy.stats.chisquare(np.array(obs), np.array(exp), ddof=len(obs)-1-estimates))
# (10.112318133864241, 0.0014728159441179519)
# the p-test value reported is 0.00147, not 0.18...
#
# Maybe I need to aggregate categories with observations less than 5
# (as suggested in many textbooks of statistics for chi-squared tests)?
observedAggregateLessThan5 = [14, 7, 5, 5]
expectedAggregateLessThan5 = [exp[0]+exp[1], exp[2], exp[3], sum(exp[4:])]
print(scipy.stats.chisquare(np.array(observedAggregateLessThan5), np.array(expectedAggregateLessThan5), ddof=len(observedAggregateLessThan5)-1-estimates))
# (0.53561749342466913, 0.46425467595930309)
# Again the p-test value computed is not 0.18, it is 0.46...
我究竟做错了什么?