对熊猫柱数据进行卡方检验



我根据找到的方法编写了测试。当查看Stack Overflow时,我看到了另一种方法(可以在这里看到(,它有点复杂,这让我怀疑我是否选择了正确的方法
我正在寻找方法来检查我的计算是否正确。

以下是相关代码:

from scipy.stats import chi2_contingency
import pandas as p
...
# Example data
data[['Eczema', 'Gender']]
Eczema  Gender
1     Healthy       0
4     Healthy       1
5     Healthy       0
6     Healthy       1
8     Healthy       1
..        ...     ...
601   Healthy       0
603   Healthy       0
604   Healthy       1
606  Diseased       1
607   Healthy       1
# The contingency table:
p.crosstab(data['Eczema'], data['Gender'])
Gender      0    1
Eczema            
Diseased    5   11
Healthy   219  233
# The calculation:
chi2, p, dof, ex = chi2_contingency(p.crosstab(data['Eczema'], data['Gender']))
p
0.27176974714995455

欢迎提出任何建议。谢谢

您链接到的另一种方法实际上并不是不同的方法。该问题中的代码试图进行与chi2_contingency中相同的计算,但存在一些错误。

您的代码看起来不错。p值为0.27时,可以说数据不支持否定湿疹性别之间没有关联的零假设。

最新更新