R 中的假设检验
Richie Cotton
Data Evangelist at DataCamp
library(infer)
stack_overflow %>%
prop_test(
hobbyist ~ age_cat,
order = c("At least 30", "Under 30"),
alternative = "two-sided",
correct = FALSE
)
# 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
先前的假设检验结果:有证据表明变量hobbyist与age_cat存在关联。
如果响应变量中的成功比例在解释变量的各类别间相同,则两变量统计独立。
stack_overflow %>%
count(age_cat)
# A tibble: 2 x 2
age_cat n
<chr> <int>
1 At least 30 1050
2 Under 30 1211
stack_overflow %>%
count(job_sat)
# A tibble: 5 x 2
job_sat n
<fct> <int>
1 Very dissatisfied 159
2 Slightly dissatisfied 342
3 Neither 201
4 Slightly satisfied 680
5 Very satisfied 879
$H_{0}$:年龄类别与工作满意度相互独立。
$H_{A}$:年龄类别与工作满意度不独立。
alpha <- 0.1
ggplot(stack_overflow, aes(job_sat, fill = age_cat)) +
geom_bar(position = "fill") +
ylab("proportion")

library(infer)
stack_overflow %>%
chisq_test(age_cat ~ job_sat)
# A tibble: 1 x 3
statistic chisq_df p_value
<dbl> <int> <dbl>
1 5.55 4 0.235
自由度:
$(\text{响应类别数} - 1) \times (\text{解释变量类别数} - 1)$
$(2 - 1) * (5 - 1) = 4$
ggplot(stack_overflow, aes(age_cat, fill = job_sat)) +
geom_bar(position = "fill") +
ylab("proportion")

library(infer)
stack_overflow %>%
chisq_test(age_cat ~ job_sat)
# A tibble: 1 x 3
statistic chisq_df p_value
<dbl> <int> <dbl>
1 5.55 4 0.235
问
变量 X 与变量 Y 是否相互独立?
library(infer)
stack_overflow %>%
chisq_test(job_sat ~ age_cat)
# A tibble: 1 x 3
statistic chisq_df p_value
<dbl> <int> <dbl>
1 5.55 4 0.235
不问
变量 X 是否独立于变量 Y?
args(chisq_test)
function (x, formula, response = NULL, explanatory = NULL, ...)
R 中的假设检验