ANOVA 檢定

R 中的假設檢定

Richie Cotton

Data Evangelist at DataCamp

工作滿意度:5 種分類

stack_overflow %>% 
  count(job_sat)
# A tibble: 5 x 2
  job_sat                   n
  <fct>                 <int>
1 Very dissatisfied       187
2 Slightly dissatisfied   385
3 Neither                 245
4 Slightly satisfied      777
5 Very satisfied          981
R 中的假設檢定

視覺化多個分佈

問題:不同工作滿意度層級的年薪平均是否不同?

stack_overflow %>% 
  ggplot(aes(x = job_sat, y = converted_comp)) +
  geom_boxplot() +
  coord_flip()

盒鬚圖:顯示 5 類別各自的薪酬分佈。「Very satisfied」看起來比其他類別高一些,但不容易判斷。

R 中的假設檢定

變異數分析(ANOVA)

mdl_comp_vs_job_sat <- lm(converted_comp ~ job_sat, data = stack_overflow)
anova(mdl_comp_vs_job_sat)
Analysis of Variance Table

Response: converted_comp
            Df   Sum Sq  Mean Sq F value Pr(>F)   
job_sat      4 1.09e+12 2.73e+11    3.65 0.0057 **
Residuals 2570 1.92e+14 7.47e+10                  

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
1 使用 lm() 的線性迴歸已在「Introduction to Regression in R」介紹過。
R 中的假設檢定

成對檢定

  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{slightly dissatisfied}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{neither}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{neither}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{neither}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{neither}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{slightly satisfied}} \neq \mu_{\text{very satisfied}}$

 

將顯著水準設為 $\alpha = 0.2$。

R 中的假設檢定

pairwise.t.test()

pairwise.t.test(stack_overflow$converted_comp, stack_overflow$job_sat, p.adjust.method = "none")
    Pairwise comparisons using t tests with pooled SD 

data:  stack_overflow$converted_comp and stack_overflow$job_sat 

                      Very dissatisfied Slightly dissatisfied Neither Slightly satisfied
Slightly dissatisfied 0.26860           -                     -       -                 
Neither               0.79578           0.36858               -       -                 
Slightly satisfied    0.29570           0.82931               0.41248 -                 
Very satisfied        0.34482           0.00384               0.15939 0.00084           

P value adjustment method: none

顯著差異:「Very satisfied」對「Slightly dissatisfied」;「Very satisfied」對「Neither」;「Very satisfied」對「Slightly satisfied」

R 中的假設檢定

群組數增加時…

散佈圖:配對數量 vs. 群組數量。群組數越多,配對數呈二次成長。

散佈圖:至少出現 1 個顯著結果的機率 vs. 群組數量。群組數越多,至少 1 個顯著結果的機率越高。

R 中的假設檢定

Bonferroni 校正

pairwise.t.test(stack_overflow$converted_comp, stack_overflow$job_sat, p.adjust.method = "bonferroni")
    Pairwise comparisons using t tests with pooled SD 

data:  stack_overflow$converted_comp and stack_overflow$job_sat 

                      Very dissatisfied Slightly dissatisfied Neither Slightly satisfied
Slightly dissatisfied 1.0000            -                     -       -                 
Neither               1.0000            1.0000                -       -                 
Slightly satisfied    1.0000            1.0000                1.0000  -                 
Very satisfied        1.0000            0.0384                1.0000  0.0084            

P value adjustment method: bonferroni

顯著差異:「Very satisfied」對「Slightly dissatisfied」;「Very satisfied」對「Slightly satisfied」

R 中的假設檢定

更多方法

p.adjust.methods
"holm"  "hochberg"  "hommel"  "bonferroni"  "BH"  "BY"  "fdr"  "none" 
R 中的假設檢定

Bonferroni 與 Holm 調整

p_values
0.268603 0.795778 0.295702 0.344819 0.368580 0.829315 0.003840 0.412482 0.159389 0.000838

Bonferroni

pmin(1, 10 * p_values)
1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 0.03840 1.00000 1.00000 0.00838

Holm(約略)

pmin(1, 10:1 * sort(p_values))
0.00838 0.03456 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 0.82931
R 中的假設檢定

一起來練習吧!

R 中的假設檢定

Preparing Video For Download...