2標本比率検定

Pythonで学ぶ仮説検定

James Chapman

Curriculum Manager, DataCamp

2つの比率の比較

$H_{0}$: 30歳未満と30歳以上のホビイストユーザーの割合は等しい

$H_{0}$: $p_{\geq30} - p_{<30} = 0$

$H_{A}$: 30歳未満と30歳以上のホビイストユーザーの割合は異なる

$H_{A}$: $p_{\geq30} - p_{<30} \neq 0$

alpha = 0.05
Pythonで学ぶ仮説検定

zスコアの計算

  • 比率検定のzスコアの式:

$$ z = \frac{(\hat{p}_{\geq30} - \hat{p}_{<30}) - 0}{\text{SE}(\hat{p}_{\geq30} - \hat{p}_{<30})} $$

  • 標準誤差の式: $$ \text{SE}(\hat{p}_{\geq30} - \hat{p}_{<30}) = \sqrt{\dfrac{\hat{p} \times (1 - \hat{p})}{n_{\geq30}} + \dfrac{\hat{p} \times (1 - \hat{p})}{n_{<30}}} $$
  • $\hat{p}$ → $\hat{p}_{\geq30}$ と $\hat{p}_{<30}$ の加重平均

$$ \hat{p} = \frac{n_{\geq30} \times \hat{p}_{\geq30} + n_{<30} \times \hat{p}_{<30}}{n_{\geq30} + n_{<30} } $$

  • zスコアの計算に必要なのは標本から得た $\hat{p}_{\geq30}$、$\hat{p}_{<30}$、$n_{\geq30}$、$n_{<30}$ のみ
Pythonで学ぶ仮説検定

zスコアの計算に必要な数値の取得

p_hats = stack_overflow.groupby("age_cat")['hobbyist'].value_counts(normalize=True)
age_cat      hobbyist
At least 30  Yes         0.773333
             No          0.226667
Under 30     Yes         0.843105
             No          0.156895
Name: hobbyist, dtype: float64
n = stack_overflow.groupby("age_cat")['hobbyist'].count()
age_cat
At least 30    1050
Under 30       1211
Name: hobbyist, dtype: int64
Pythonで学ぶ仮説検定

zスコアの計算に必要な数値の取得

p_hats = stack_overflow.groupby("age_cat")['hobbyist'].value_counts(normalize=True)
age_cat      hobbyist
At least 30  Yes         0.773333
             No          0.226667
Under 30     Yes         0.843105
             No          0.156895
Name: hobbyist, dtype: float64
p_hat_at_least_30 = p_hats[("At least 30", "Yes")]
p_hat_under_30 = p_hats[("Under 30", "Yes")]
print(p_hat_at_least_30, p_hat_under_30)
0.773333 0.843105
Pythonで学ぶ仮説検定

zスコアの計算に必要な数値の取得

n = stack_overflow.groupby("age_cat")['hobbyist'].count()
age_cat
At least 30    1050
Under 30       1211
Name: hobbyist, dtype: int64
n_at_least_30 = n["At least 30"]
n_under_30 = n["Under 30"]
print(n_at_least_30, n_under_30)
1050 1211
Pythonで学ぶ仮説検定

zスコアの計算に必要な数値の取得

p_hat = (n_at_least_30 * p_hat_at_least_30 + n_under_30 * p_hat_under_30) / 
        (n_at_least_30 + n_under_30)

std_error = np.sqrt(p_hat * (1-p_hat) / n_at_least_30 + 
                    p_hat * (1-p_hat) / n_under_30)

z_score = (p_hat_at_least_30 - p_hat_under_30) / std_error

print(z_score)
-4.223718652693034
Pythonで学ぶ仮説検定

`proportions_ztest()`による比率検定

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
n_hobbyists = np.array([812, 1021])

n_rows = np.array([812 + 238, 1021 + 190])
from statsmodels.stats.proportion import proportions_ztest z_score, p_value = proportions_ztest(count=n_hobbyists, nobs=n_rows,
alternative="two-sided")
(-4.223691463320559, 2.403330142685068e-05)
Pythonで学ぶ仮説検定

練習しましょう!

Pythonで学ぶ仮説検定

Preparing Video For Download...