
104
|
第
3
章
resampled_p_value = sum(perm_chi2 > chi2observed) / len(perm_chi2)
print(f'Observed chi2: {chi2observed:.4f}')
print(f'Resampled p-value: {resampled_p_value:.4f}')
3.9.2
卡方检验
:
统计理论
渐进统计理论表明,卡方统计量的分布可以近似为
卡方分布
(参见
2.10
节)。合适的标准
卡方分布是由
自由度
(参见
3.7
节)决定的
。对于列联表,自由度与行数(
r
)和列数(
c
)
都有关,关系如下所示:
自由度
=
(
r
– 1)
×
(
c
– 1)
卡方分布通常是偏斜的,右侧有一个长尾。自由度分别为
1
、
2
、
5
、
20
的
卡方分布如
图
3-7
所示。观测统计量在卡方分布中的位置越远,
p
值就越小。
chisq.test
函数可以使用卡方分布作为参照计算出
p
值:
> chisq.test(clicks, simulate.p.value=FALSE)
Pearson's Chi-squared test
data: clicks
X-squared = 1.6659, df = 2, p-value = 0.4348
在
Python
中,可以使用函数
scipy.stats.chi2_contingency
:
chisq, pvalue, df, expected = stats.chi2_contingency(clicks)
print(f'Observed chi2: ...