본문 바로가기

Contact English

【RStudio】 9강. ANOVA 분석

 

9강. ANOVA 분석

 

추천글 : 【RStudio】 R 스튜디오 목차


1. one-way ANOVA [본문]

2. two-way ANOVA [본문]

3. 사후검정 [본문]


 

1. one-way ANOVA [목차]

 

? InsectSprays 
# starting httpd help server ... done 

shapiro.test(x = InsectSprays$count) # normality check 
# Shapiro-Wilk normality test 
# data: InsectSprays$count
# W = 0.9216, p-value = 0.0002525 

install.packages("car")
library("car") 
leveneTest(count ~ spray, data = InsectSprays) # homoscedasticity check 
# Levene's Test for Homogeneity of Variance (center = median) 
# Df F value Pr(>F) 
# group 5 3.8214 0.004223 ** 
# 66 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

anova_IS <- aov(count ~ spray, data = InsectSprays) 
summary(anova_IS) 
# Df Sum Sq Mean Sq F value Pr(>F) 
# spray 5 2669 533.8 34.7 <2e-16 *** 
# Residuals 66 1015 15.4 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

anova_residuals <- anova_IS$residuals 
summary(anova_residuals) 
# Min. 1st Qu. Median Mean 3rd Qu. Max. 
# -8.333 -1.958 -0.500 0.000 1.667 9.333

 

 

2. two-way ANOVA [목차]

 

dose <- as.factor(dose) 

aov_result1 <- aov(len ~ supp + dose) 
summary(aov_result1)
# Df Sum Sq Mean Sq F value Pr(>F) 
# supp 1 205.4 205.4 14.02 0.000429 *** 
# dose 2 2426.4 1213.2 82.81 < 2e-16 *** 
# Residuals 56 820.4 14.7 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

aov_result2 <- aov(len ~ supp + dose + supp * dose) 
summary(aov_result2) 
# Df Sum Sq Mean Sq F value Pr(>F) 
# supp 1 205.4 205.4 15.572 0.000231 *** 
# dose 2 2426.4 1213.2 92.000 < 2e-16 *** 
# supp:dose 2 108.3 54.2 4.107 0.021860 * 
# Residuals 54 712.1 13.2 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

aov_result3 <- aov(len ~ supp + dose + supp:dose) 
summary(aov_result3) # The same with aov_result2
# Df Sum Sq Mean Sq F value Pr(>F) 
# supp 1 205.4 205.4 15.572 0.000231 *** 
# dose 2 2426.4 1213.2 92.000 < 2e-16 *** 
# supp:dose 2 108.3 54.2 4.107 0.021860 * 
# Residuals 54 712.1 13.2 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

interaction.plot(x.factor = dose, trace.factor = supp, response = len, pch = c(19, 24))

 

3. 사후검정 [목차]

⑴ TukeyHSD(Tukey honestly significant difference)

 

TukeyHSD(anova_IS) 
# Tukey multiple comparisons of means 
# 95% family-wise confidence level 

# Fit: aov(formula = count ~ spray, data = InsectSprays) 

# $spray 
# diff lwr upr p adj 
# B-A 0.8333333 -3.866075 5.532742 0.9951810
# C-A -12.4166667 -17.116075 -7.717258 0.0000000 
# D-A -9.5833333 -14.282742 -4.883925 0.0000014 
# E-A -11.0000000 -15.699409 -6.300591 0.0000000 
# F-A 2.1666667 -2.532742 6.866075 0.7542147 
# C-B -13.2500000 -17.949409 -8.550591 0.0000000 
# D-B -10.4166667 -15.116075 -5.717258 0.0000002 
# E-B -11.8333333 -16.532742 -7.133925 0.0000000 
# F-B 1.3333333 -3.366075 6.032742 0.9603075 
# D-C 2.8333333 -1.866075 7.532742 0.4920707 
# E-C 1.4166667 -3.282742 6.116075 0.9488669 
# F-C 14.5833333 9.883925 19.282742 0.0000000 
# E-D -1.4166667 -6.116075 3.282742 0.9488669 
# F-D 11.7500000 7.050591 16.449409 0.0000000 
# F-E 13.1666667 8.467258 17.866075 0.0000000

 

입력 : 2019.11.17 00:13