2標本比率の検定

Rによる仮説検定

Richie Cotton

Data Evangelist at DataCamp

2つの比率の比較

$H_{0}$: 30歳以上と30歳未満で、SOユーザーのホビイスト比率は同じ。

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

$H_{A}$: 30歳以上と30歳未満で、SOユーザーのホビイスト比率は異なる。

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

alpha <- 0.05
Rによる仮説検定

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}$ は $p$(共通の未知成功比率)のプール推定量。

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

計算するのは4つだけ:$\hat{p}_{\geq30}$、$\hat{p}_{<30}$、$n_{\geq30}$、$n_{<30}$。

Rによる仮説検定

zスコアのための数値

stack_overflow %>%
  group_by(age_cat) %>%
  summarize(
    p_hat = mean(hobbyist == "Yes"),
    n = n()
  )
# A tibble: 2 x 3
  age_cat     p_hat     n
  <chr>       <dbl> <int>
1 At least 30 0.773  1050
2 Under 30    0.843  1216
z_score
-4.217
Rによる仮説検定

prop_test() による比率の検定

library(infer)
stack_overflow %>% 
  prop_test(

hobbyist ~ age_cat, # 比率 ~ カテゴリ
order = c("At least 30", "Under 30"), # どの p-hat を引くか
success = "Yes", # 比率を数える応答値
alternative = "two-sided", # 対立仮説の種類
correct = FALSE # Yatesの連続性補正を適用するか
)
# A tibble: 1 x 6
  statistic chisq_df   p_value alternative lower_ci upper_ci
      <dbl>    <dbl>     <dbl> <chr>          <dbl>    <dbl>
1      17.8        1 0.0000248 two.sided     0.0605    0.165
Rによる仮説検定

練習しましょう!

Rによる仮説検定

Preparing Video For Download...