非参数方差分析与非配对 t 检验

R 中的假设检验

Richie Cotton

Data Evangelist at DataCamp

非参数检验

"非参数检验"是不对检验统计量假定分布的假设检验。

非参数检验有两类:

  1. 基于模拟。
  2. 基于秩。
R 中的假设检验

t_test()

$H_{0}$:$\mu_{child} - \mu_{adult} = 0$     $H_{A}$:$\mu_{child} - \mu_{adult} > 0$

library(infer)
stack_overflow %>% 
  t_test(
    converted_comp ~ age_first_code_cut,
    order = c("child", "adult"),
    alternative = "greater"
  )
# A tibble: 1 x 6
  statistic  t_df p_value alternative lower_ci upper_ci
      <dbl> <dbl>   <dbl> <chr>          <dbl>    <dbl>
1      2.40 2083. 0.00814 greater        8438.      Inf
R 中的假设检验

计算原假设分布

基于模拟的流程
null_distn <- stack_overflow %>% 
  specify(converted_comp ~ age_first_code_cut) %>%

hypothesize(null = "independence") %>%
generate(reps = 5000, type = "permute") %>%
calculate( stat = "diff in means", order = c("child", "adult") )
t 检验(用于对比)
library(infer)
stack_overflow %>% 
  t_test(
    converted_comp ~ age_first_code_cut,
    order = c("child", "adult"),
    alternative = "greater"
  )
R 中的假设检验

计算观测统计量

基于模拟的流程
obs_stat <- stack_overflow %>% 
  specify(converted_comp ~ age_first_code_cut) %>% 
  calculate(
    stat = "diff in means", 
    order = c("child", "adult")
  )
t 检验(用于对比)
library(infer)
stack_overflow %>% 
  t_test(
    converted_comp ~ age_first_code_cut,
    order = c("child", "adult"),
    alternative = "greater"
  )
R 中的假设检验

获取 p 值

基于模拟的流程
get_p_value(
  null_distn, obs_stat, 
  direction = "greater"
)
# A tibble: 1 x 1
  p_value
    <dbl>
1  0.0066
t 检验(用于对比)
library(infer)
stack_overflow %>% 
  t_test(
    converted_comp ~ age_first_code_cut,
    order = c("child", "adult"),
    alternative = "greater"
  )
# A tibble: 1 x 6
  statistic  t_df p_value alternative lower_ci upper_ci
      <dbl> <dbl>   <dbl> <chr>          <dbl>    <dbl>
1      2.40 2083. 0.00814 greater        8438.      Inf
R 中的假设检验

向量的秩

x <- c(1, 15, 3, 10, 6)
rank(x)
1 5 2 4 3

"Wilcoxon-Mann-Whitney 检验"(又称"Wilcoxon 秩和检验")大致相当于对数值输入的秩进行 t 检验。

R 中的假设检验

Wilcoxon–Mann–Whitney 检验

wilcox.test(
  converted_comp ~ age_first_code_cut,
  data = stack_overflow,
  alternative = "greater",
  correct = FALSE
) 
    Wilcoxon rank sum test

data:  converted_comp by age_first_code_cut
W = 967298, p-value <2e-16
alternative hypothesis: true location shift is greater than 0
1 亦称"Wilcoxon 秩和检验"和"Mann-Whitney U 检验"。
R 中的假设检验

Kruskal–Wallis 检验

Kruskal–Wallis 检验 之于 Wilcoxon–Mann–Whitney 检验,正如 ANOVA 之于 t 检验。

kruskal.test(
  converted_comp ~ job_sat,
  data = stack_overflow
)
    Kruskal-Wallis rank sum test

data:  converted_comp by job_sat
Kruskal-Wallis chi-square = 81, df = 4, p-value <2e-16
R 中的假设检验

让我们练习吧!

R 中的假设检验

Preparing Video For Download...