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로 하는 가설 검정