## April 12, 2007 550.413 Section ### More on the popcorn data ### # Prepping the data popcorn = read.table("/Users/Elizabeth/Documents/popcorn.txt", header=T) attach(popcorn) names(popcorn) #Make sure all factors are treated as categorical variables. PopcornBrand=as.factor(PopcornBrand) Power = as.factor(Power) OvenBrand = as.factor(OvenBrand) PoppingTime = as.factor(PoppingTime) # Create new variable which indicates which treatment an observation is in. Treatment = as.factor(paste(PopcornBrand, OvenBrand, sep="")) # note: this new variable allows us to turn a multi-way ANOVA into one-way cell means ANOVA popcorn=data.frame(popcorn, Treatment) popcorn ## Model 1 ## PopMod1 = aov(PctEdible ~ PopcornBrand*OvenBrand) anova(PopMod1) set1 = c("PopcornBrand", "OvenBrand") TukeyHSD(PopMod1, set1, conf.level = .9) #visualize plots and where estimate falls in relation to 0 par(mfcol=c(1,2)) plot(TukeyHSD(PopMod1, set1, conf.level = .9)) # All pairwise comparisons within treatments # ordered = T gives all positive estimates. TukeyHSD(PopMod1, "PopcornBrand:OvenBrand", conf.level = .9, ordered = T) # The default is all factors TukeyHSD(PopMod1, conf.level = .9) ## Model 2 ## PopMod2 = aov(PctEdible ~ Treatment) anova(PopMod2) TukeyHSD(PopMod2, conf.level = .9) detach(popcorn) ### Looking at the mouse data ### mouse = read.table("/Users/Elizabeth/Documents/mousegrowth.txt", header = T,sep=",") Hormone = as.factor(Hormone) Diet = as.factor(Diet) Treatment = as.factor(Treatment) attach(mouse) names(mouse) m1 = aov(Weight.Gain ~ Hormone*Diet) m2 = aov(Weight.Gain ~ Treatment) #The following 2 give the same thing TukeyHSD(m1, conf.level = .9) TukeyHSD(m2, conf.level = .9) #orders the factors level differences so they are all positive. TukeyHSD(m2, conf.level = .9, ordered = T) ## using scheffe.R # Note: only for one-way ANOVA!! #register function source("/Users/Elizabeth/Documents/scheffe.R") #all pairwise comparisons scheffe(m2,"allpairs",.95) #define matrix using c(), number of rows = 6 = num treatment levels, num columns = 4 = num contrasts defined contmatrix = matrix(c(1,0,0,-1,0,0, -1/3,-1/3,-1/3,1/3,1/3,1/3, 0,0,0,1,-.5,-.5, -1,0,0,0,0,1), 6,4) contmatrix #contrasts scheffe(m2,contmatrix,.95) detach(mouse)