#### Example 1: one-way ANOVA ####
outcome <- c( 1.4,2.1,3.0,2.1,3.2,4.7,3.5,4.5,5.4 ) # data
treatment1 <- factor( c( 1,1,1,2,2,2,3,3,3 )) # grouping variable
anova1 <- aov( outcome ~ treatment1 ) # run the ANOVA
summary( anova1 ) # print the ANOVA table
EtaSq( anova1 ) # effect size
#### Example 2: two-way ANOVA ####
treatment2 <- factor(c(1,2,3,1,2,3,1,2,3)) # second grouping variable
anova2 <- aov(outcome ~ treatment1 + treatment2) # run the ANOVA
summary(anova2) # print the ANOVA table
EtaSq(anova2) # effect size
# define other contrasts
dfMD <- data.frame(IV1=factor(rep(1:3, c(3+5+7, 5+6+4, 5+4+6))),
IV2=factor(rep(rep(1:3, 3), c(3,5,7, 5,6,4, 5,4,6))),
DV=c(c(41, 43, 50), c(51, 43, 53, 54, 46), c(45, 55, 56, 60, 58, 62, 62),
c(56, 47, 45, 46, 49), c(58, 54, 49, 61, 52, 62), c(59, 55, 68, 63),
c(43, 56, 48, 46, 47), c(59, 46, 58, 54), c(55, 69, 63, 56, 62, 67)))
dfMD$IV1s <- C(dfMD$IV1, "contr.sum")
dfMD$IV2s <- C(dfMD$IV2, "contr.sum")
dfMD$IV1t <- C(dfMD$IV1, "contr.treatment")
dfMD$IV2t <- C(dfMD$IV2, "contr.treatment")
op <- options(contrasts=c("contr.treatment", "contr.poly"))
fitT <- aov(DV ~ IV1*IV2, data=dfMD)
fitTs <- aov(DV ~ IV1s*IV2s, data=dfMD)
options(contrasts=c("contr.sum", "contr.poly"))
fitS <- aov(DV ~ IV1*IV2, data=dfMD)
fitSt <- aov(DV ~ IV1t*IV2t, data=dfMD)
options(op)
dfBal <- data.frame(IV1=factor(rep(1:2, times=8*3)),
IV2=factor(rep(1:3, each=8*2)),
DV=rnorm(8*2*3, 0, 1))
fitB <- aov(DV ~ IV1*IV2, data=dfBal)
Run the code above in your browser using DataLab