R배우기(반복있는 이원 분산분석1)_0516
< 반복있는 이원배치 분산분석_R 실습_ 0516 >
library(doBy) ; # doBy::summaryBy : 포뮬러에 따라 데이터를 그룹으로 묶고 요약한 결과를 반환한다.#
----------------------------------------
# (2) two-way ANOVA when there are more than one observation per cell (different treatment groups)
# (the number of observations in each cell must be equal)
gender.fac <- as.factor(c(rep("M", 9), rep("F", 9)))
gender.fac
class <- c("class_1", "class_1", "class_1", "class_2", "class_2", "class_2", "class_3", "class_3", "class_3")
class.fac <- as.factor(c(rep(class, 2)))
class.fac
score_stats <- c(71, 77, 78, 76, 77, 78, 71, 70, 69, 80,
76, 80, 79, 78, 77, 73, 71, 70)
# summary statistics
score.df <- data.frame(gender.fac, class.fac, score_stats)
score.df
summaryBy(score_stats ~ class.fac,
data=score.df, FUN = c(mean, sd, min, max))
summary(score_stats, data=score.df)
# box plot and interaction plot
par(mfrow = c(2, 2))
plot(score_stats ~ gender.fac, main="box plot by gender")
plot(score_stats ~ class.fac, main="box plot by class")
interaction.plot(gender.fac, class.fac, score_stats, bty='l', main="interaction effect plot")
interaction.plot(class.fac, gender.fac, score_stats, bty='l', main="interaction effect plot")
# two-way ANOVA : aov()
# replicates, interaction effect
aov_model = aov(score_stats ~ gender.fac + class.fac + gender.fac:class.fac)
summary(aov_model)