R로 하는 가설 검정
Richie Cotton
Data Evangelist at DataCamp
온라인에서 코딩 해결책을 검색했을 때 첫 번째 결과 링크가 이미 방문한 보라색으로 표시되어 있습니다. 어떤 기분이 드시나요?
purple_link_counts <- stack_overflow %>%
count(purple_link)
# A tibble: 4 x 2
purple_link n
<fct> <int>
1 Hello, old friend 1330
2 Amused 409
3 Indifferent 426
4 Annoyed 290
hypothesized <- tribble(
~ purple_link, ~ prop,
"Hello, old friend", 1 / 2,
"Amused" , 1 / 6,
"Indifferent" , 1 / 6,
"Annoyed" , 1 / 6
)
# A tibble: 4 x 2
purple_link prop
<chr> <dbl>
1 Hello, old friend 0.5
2 Amused 0.167
3 Indifferent 0.167
4 Annoyed 0.167
$H_{0}$: 표본은 가설 분포와 일치합니다.
$H_{A}$: 표본은 가설 분포와 일치하지 않습니다.
검정 통계량 $\chi^{2}$는 각 집단에서 관측값이 기댓값으로부터 얼마나 벗어나는지를 측정합니다.
alpha <- 0.01
n_total <- nrow(stack_overflow)
hypothesized <- tribble(
~ purple_link, ~ prop,
"Hello, old friend", 1 / 2,
"Amused" , 1 / 6,
"Indifferent" , 1 / 6,
"Annoyed" , 1 / 6
) %>%
mutate(n = prop * n_total)
# A tibble: 4 x 3
purple_link prop n
<chr> <dbl> <dbl>
1 Hello, old friend 0.5 1228.
2 Amused 0.167 409.
3 Indifferent 0.167 409.
4 Annoyed 0.167 409.
ggplot(purple_link_counts, aes(purple_link, n)) +
geom_col() +
geom_point(data = hypothesized, color = "purple")

hypothesized_props <- c(
"Hello, old friend" = 1 / 2,
Amused = 1 / 6,
Indifferent = 1 / 6,
Annoyed = 1 / 6
)
library(infer)
stack_overflow %>%
chisq_test(
response = purple_link,
p = hypothesized_props
)
# A tibble: 1 x 3
statistic chisq_df p_value
<dbl> <dbl> <dbl>
1 44.0 3 0.00000000154
R로 하는 가설 검정