[R] - 통계데이터분석 - 이원분산분석 (two-way ANOVA)

반응형

이원분산분석 (two-way ANOVA)

집단을 구분하는 독립변수가 두 개일 때 모집단간 평균의 동일성을 검정.

  • 주효과(main effect) 검정 : 각 독립변수에 의해 만들어지는 집단 간 평균의 차이에 대한 검정

  • 상호작용효과(interaction effect) 검정 : 두 독립변수의 조합에 의해 만들어지는 집단 간 평균의 차이에 대한 검정

이원분산분석(Two-way ANOVA)은 두 개의 범주형 독립변수가 연속형 종속변수에 미치는 영향을 분석하는 방법입니다. 이 방법은 두 독립변수의 효과가 종속변수에 미치는 영향을 각각 분리해보고, 두 변수가 함께 작용하여 종속변수에 미치는 영향을 확인하는 상호작용 효과도 분석합니다.

R 코드


str(ToothGrowth) # 데이터셋 : 보충제에 따른 이빨 성장 변화

# 수치형 변수인 dose를 범주형 변수로 변환
ToothGrowth$dose = factor(ToothGrowth$dose,
                           levels=c(0.5, 1.0, 2.0),
                           labels=c('low','med','high')) 

ToothGrowth

# 요약 통계량 

with(ToothGrowth, tapply(len, list(supp, dose), length)) # 표본크기 
with(ToothGrowth, tapply(len, list(supp, dose), mean)) # 평균
with(ToothGrowth, tapply(len, list(supp, dose), sd)) # 표준편차 

# insight : OJ 투여량과 VC투여량에 따라서 이빨 성장의 정도가 다름 , 
# OJ를 투여했을 때와 VC를 투여했을 때 다름 

ToothGrowth.aov = aov(len ~ supp * dose, data=ToothGrowth) # supp와 dose의 모든 상호작용 고려 
ToothGrowth.aov = aov(len ~ supp + dose + supp:dose , data=ToothGrowth)

summary(ToothGrowth.aov)

model.tables(ToothGrowth.aov, type="means")

boxplot(len ~ supp * dose, data = ToothGrowth,
        col=c("deeppink", 'yellowgreen'), las=1,
        xlab="vitamin c Type", ylab="Tooth Growth",
        main = "Effects of Vitamin C on tooth growth")

interaction.plot(x.factor=ToothGrowth$dose, 
                 trace.factor=ToothGrowth$supp, 
                 response = ToothGrowth$len,
                 trace.label = "Supplement", 
                 las=1, type='b', pch=c(1,19),
                 col = c("blue", "red"),
                 xlab="Dose Level", ylab="Tooth Length",
                 main = "Interaction plot for Tootht growth of guinea pigs") # las는 y,x축 평행


# 평균도표 

install.packages('gplots')
library(gplots)

# 두 변수의 조합쌍을 만들기 
interaction(ToothGrowth$supp, ToothGrowth$dose, sep=" ") 
# 보충제의 종류와 투여량의 조합쌍 모두를 계산 

plotmeans(len ~ interaction(supp, dose, sep=" "), data=ToothGrowth,
          col=c("red","green3"), xlab="Supplement and Dose Combination", 
          ylab="Tooth Growth", 
          main = "Means plot for Tootht growth of guinea pigs")

# connect 추가해서 같은 보충제끼리 연결하기 
plotmeans(len ~ interaction(supp, dose, sep=" "), data=ToothGrowth,
          col=c("red","green3"), xlab="Supplement and Dose Combination",
          connect = list(c(1,3,5), c(2,4,6)),
          ylab="Tooth Growth", 
          main = "Means plot for Tootht growth of guinea pigs") 

# 두 개의 집단변수가 있을 때, 하나의 집단 변수를 기준으로 나머지 집단변수와 종속변수간의 관계를 하나의 산점도로 

coplot(len ~ dose | supp, data = ToothGrowth, 
       col="steelblue", pch=19)

# 직선 추가 

coplot(len ~ dose | supp, data = ToothGrowth, 
       col="steelblue", pch=19,
       panel = panel.smooth, lwd=2, col.smooth="darkorange",
       xlab='Doae Level', ylab='Tooth Growth')

install.packages('HH')
library(HH)


반응형