卡方独立性检验

Python 假设检验

James Chapman

Curriculum Manager, DataCamp

回顾比例检验

age_by_hobbyist = stack_overflow.groupby("age_cat")['hobbyist'].value_counts()
age_cat      hobbyist
At least 30  Yes          812
             No           238
Under 30     Yes         1021
             No           190
Name: hobbyist, dtype: int64
from statsmodels.stats.proportion import proportions_ztest
n_hobbyists = np.array([812, 1021])
n_rows = np.array([812 + 238, 1021 + 190])
stat, p_value = proportions_ztest(count=n_hobbyists, nobs=n_rows, 
                                  alternative="two-sided")
(-4.223691463320559, 2.403330142685068e-05)
Python 假设检验

变量的独立性

先前检验结果:有证据表明 hobbyistage_cat 相关

统计独立——响应变量的成功比例在解释变量各类别中相同

Python 假设检验

变量独立性的检验

import pingouin
expected, observed, stats = pingouin.chi2_independence(data=stack_overflow, x='hobbyist',
                                                       y='age_cat', correction=False)
print(stats)
                 test    lambda       chi2  dof      pval    cramer     power
0             pearson  1.000000  17.839570  1.0  0.000024  0.088826  0.988205
1        cressie-read  0.666667  17.818114  1.0  0.000024  0.088773  0.988126
2      log-likelihood  0.000000  17.802653  1.0  0.000025  0.088734  0.988069
3       freeman-tukey -0.500000  17.815060  1.0  0.000024  0.088765  0.988115
4  mod-log-likelihood -1.000000  17.848099  1.0  0.000024  0.088848  0.988236
5              neyman -2.000000  17.976656  1.0  0.000022  0.089167  0.988694

$\chi^2$ 统计量 = 17.839570 = $(-4.223691463320559)^2$ = ($z$ 分数)$^2$

Python 假设检验

工作满意度与年龄分组

stack_overflow['age_cat'].value_counts()
Under 30       1211
At least 30    1050
Name: age_cat, dtype: int64
stack_overflow['job_sat'].value_counts()
Very satisfied           879
Slightly satisfied       680
Slightly dissatisfied    342
Neither                  201
Very dissatisfied        159
Name: job_sat, dtype: int64
Python 假设检验

提出假设

$H_{0}$:年龄分组与工作满意度相互独立

$H_{A}$:年龄分组与工作满意度不独立

alpha = 0.1
  • 检验统计量记为 $\chi^{2}$
  • 在独立性假设下,观测值与期望值相差多远?
Python 假设检验

探索性可视化:比例堆叠条形图

props = stack_overflow.groupby('job_sat')['age_cat'].value_counts(normalize=True)

wide_props = props.unstack()
wide_props.plot(kind="bar", stacked=True)
Python 假设检验

探索性可视化:比例堆叠条形图

按年龄分组填充的工作满意度比例堆叠条形图

Python 假设检验

卡方独立性检验

import pingouin
expected, observed, stats = pingouin.chi2_independence(data=stack_overflow, x="job_sat", y="age_cat")
print(stats)    
                 test    lambda      chi2  dof      pval    cramer     power
0             pearson  1.000000  5.552373  4.0  0.235164  0.049555  0.437417
1        cressie-read  0.666667  5.554106  4.0  0.235014  0.049563  0.437545
2      log-likelihood  0.000000  5.558529  4.0  0.234632  0.049583  0.437871
3       freeman-tukey -0.500000  5.562688  4.0  0.234274  0.049601  0.438178
4  mod-log-likelihood -1.000000  5.567570  4.0  0.233854  0.049623  0.438538
5              neyman -2.000000  5.579519  4.0  0.232828  0.049676  0.439419

【自由度】

$(\text{响应类别数} - 1) \times (\text{解释变量类别数} - 1)$

$(2 - 1) * (5 - 1) = 4$

Python 假设检验

交换自变量与因变量?

props = stack_overflow.groupby('age_cat')['job_sat'].value_counts(normalize=True)
wide_props = props.unstack()
wide_props.plot(kind="bar", stacked=True)
Python 假设检验

交换自变量与因变量?

按工作满意度填充的年龄分组比例堆叠条形图

Python 假设检验

双向做卡方

expected, observed, stats = pingouin.chi2_independence(data=stack_overflow, x="age_cat", y="job_sat")
print(stats[stats['test'] == 'pearson'])   
      test  lambda      chi2  dof      pval    cramer     power
0  pearson     1.0  5.552373  4.0  0.235164  0.049555  0.437417

提问:变量 X 与 Y 是否独立?

不要问:变量 X 是否独立于变量 Y?

Python 假设检验

方向与尾部如何处理?

  • 观测与期望计数平方均为非负
  • 卡方检验几乎总是右尾 $^{1}$
1 左尾卡方检验用于统计取证,以检测拟合是否"过好"(可能为伪造数据)。方差的卡方检验可为双尾。但这些用法较小众。
Python 假设检验

Passons à la pratique !

Python 假设检验

Preparing Video For Download...