Rによる仮説検定
Richie Cotton
Data Evangelist at DataCamp
$p$: 母比率(未知の母数パラメータ)
$\hat{p}$: 標本比率(標本統計量)
$p_{0}$: 帰無仮説の母比率
$$ z = \frac{\hat{p} - \text{mean}(\hat{p})}{\text{standard error}(\hat{p})} = \frac{\hat{p} - p}{\text{standard error}(\hat{p})} $$
$H_{0}$が真のとき、$p = p_{0}$なので
$$ z = \dfrac{\hat{p} - p_{0}}{\text{standard error}(\hat{p})} $$
$SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) \approx \sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}$
$SE_{\hat{p}} = \sqrt{\dfrac{p_{0}*(1-p_{0})}{n}}}$
$H_{0}$が真のとき、
$z = \dfrac{\hat{p} - p_{0}}{\sqrt{\dfrac{p_{0}*(1-p_{0})}{n}}}$
標本情報($\hat{p}$と$n$)と帰無仮説のパラメータ($p_{0}$)のみを使用する。
$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}{\sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}}$
$H_{0}$: 30歳未満のSOユーザーの割合は0.5である。
$H_{A}$: 30歳未満のSOユーザーの割合は0.5ではない。
alpha <- 0.01
stack_overflow %>%
count(age_cat)
# A tibble: 2 x 2
age_cat n
<chr> <int>
1 At least 30 1050
2 Under 30 1216
p_hat <- stack_overflow %>%
summarize(prop_under_30 = mean(age_cat == "Under 30")) %>%
pull(prop_under_30)
0.5366
p_0 <- 0.50
n <- nrow(stack_overflow)
2266
$z = \dfrac{\hat{p} - p_{0}}{\sqrt{\dfrac{p_{0}*(1-p_{0})}{n}}}$
numerator <- p_hat - p_0
denominator <- sqrt(p_0 * (1 - p_0) / n)
z_score <- numerator / denominator
3.487
左側検定(「未満」)
p_value <- pnorm(z_score)
右側検定(「より大きい」)
p_value <- pnorm(z_score, lower.tail = FALSE)
両側検定(「等しくない」)
p_value <- pnorm(z_score) +
pnorm(z_score, lower.tail = FALSE)
p_value <- 2 * pnorm(z_score)
0.000244
p_value <= alpha
TRUE
Rによる仮説検定