卡方獨立性檢定

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 中的假設檢定

一起來練習吧!

Python 中的假設檢定

Preparing Video For Download...