Skip to content

Instantly share code, notes, and snippets.

@marutter
Created March 17, 2011 15:48
Show Gist options
  • Select an option

  • Save marutter/874561 to your computer and use it in GitHub Desktop.

Select an option

Save marutter/874561 to your computer and use it in GitHub Desktop.
#
# Two Factor ANOVA
#
data <- read.csv("tomatoes.csv")
attach(data)
table(variety,density)
boxplot(yield~variety:density)
res1 <- lm(yield~variety*density)
anova(res1)
plot(res1,1)
interaction.plot(variety,density,yield)
TukeyHSD(aov(res1),which="variety",conf.level=1-.05/2)
TukeyHSD(aov(res1),which="density",conf.level=1-.05/2)
model.tables(aov(res1))
model.tables(aov(res1),type="means")
#
# Unbalanced ANOVA
#
data <- read.csv("unbalanced.csv")
attach(data)
table(seed,fert)
boxplot(growth~seed:fert)
#
# Type I SS
#
res1 <- lm(growth~seed*fert)
anova(res1)
res2 <- lm(growth~fert*seed)
anova(res2)
#
# Sequential Sum of Squares
#
anova(lm(growth~seed*fert))
anova(lm(growth~seed))
anova(lm(growth~seed+fert))
#
# Type III SS
#
library(car)
res <- lm(growth~fert*seed,contrasts=list(fert=contr.sum, seed=contr.sum))
Anova(res,type="III")
#
# Which are different
#
interaction.plot(seed,fert,growth)
TukeyHSD(aov(res),which="seed",conf.level=1-.05/2)
TukeyHSD(aov(res),which="fert",conf.level=1-.05/2)
#
# Finding the means
#
tapply(growth,seed,mean)
round(tapply(growth,seed:fert,mean),2)
(107.67+97)/2
(100.67+92)/2
model.tables(aov(res1),type="means")
model.tables(aov(res),type="means")
#
TukeyHSD(aov(res1),which="seed")
TukeyHSD(aov(res2),which="seed")
TukeyHSD(aov(res),which="seed")
#
TukeyHSD(aov(res2),which="fert:seed")
TukeyHSD(aov(res),which="fert:seed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment