# Anova oneway with Siegel data rm(list = ls()) setwd ("C:\\Users\\Dave\\Dropbox\\Webs\\methods8\\DataFiles ") data <- read.table("Tab12-1.dat", header = T) attach(data) Group <- factor(Group, labels = c("M-S", "M-M", "S-S", "S-M", "Mc-M")) grp.means = tapply(Time, Group, mean) cat ("The group means are = ", grp.means, '\n', "and the grand mean is = ", mean(Time),'\n') boxplot(Time ~ Group, main = "Response Times by Group", xlab = "Group Membership", ylab = "Response Time" ) contrasts(Group) = 'contr.treatment' model1 <- lm(Time ~ Group) summary.lm(model1) # Change the base group contrasts(Group) = 'contr.treatment'(levels(Group), base = 3) model1a <- lm(Time ~ Group) summary.lm(model1a) contrasts(Group) = 'contr.sum' model2 <- lm(Time ~ Group) summary.lm(model2) # Now go on to a different kind of contrast--User Specified Contrasts L <- matrix(c(-1/3, -1/3, -1/3, 1/2, 1/2, 0, 0, 0, 1, -1, 0, 1, -1, 0, 0), nrow = 5, ncol = 3) contrasts(Group) = L model3 <- lm(Time ~ Group) summary.aov(model3,split=list(Group=list( "First Three vs S-S and Mc-M" = 1, "S-M vs Mc-M"=2, "M-M vs S-S" = 3))) # Now I do this using inverse --get the same answer because these are orthogonal L <- matrix(c(1,1,1,1,1, -1/3, -1/3, -1/3, 1/2, 1/2, 0, 0, 0, 1, -1, 1/2, -1, 1/2, 0, 0, 0, 1, -1, 0, 0), nrow = 5) Linv <- solve(t(L)) new.contrasts <- Linv[,2:5] contrasts(Group) = new.contrasts model4 <- lm(Time~Group ) summary.aov(model4,split=list(Group=list("First 3 vs last 2"=1, "First two vs third" = 2, "First vs fifth"=3, "Fourth vs fifth" = 4))) # The following gives correct solution for nonorthogonal design # Notice that it uses the Linverse # I need to use the Linverse with nonorthogonal designs setwd ("C:\\Users\\Dave\\Dropbox\\Webs\\methods8\\DataFiles ") dat <- read.table("Tab12-1.dat", header = T) attach(dat) Group <- factor(Group) # I am specifying the contrast matrix as L and its inverse as LInv L <- matrix(c(1,1,1,1,1, -1/3, -1/3, -1/3, 1/2, 1/2, 1, 1, -2, 0, 0, 1, 0, 0, 0,-1, 0, 0, 0, -1, 1), nrow = 5) Linv <- solve(t(L)) new.contrasts <- Linv[,2:5] contrasts(Group) = new.contrasts model4 <- lm(Time~Group) summary.lm(model4,split=list(Group=list("First 3 vs last 2"=1, "First two vs third" = 2, "First vs Fifth"=3, "Fourth vs fifth" = 4))) # This will give the wrong answer contrasts(Group) = L[,2:5] # We need to get rid of that first row. model4 <- lm(Time~Group) summary.lm(model4,split=list(Group=list("First 3 vs last 2"=1, "First two vs third" = 2, "First vs Fifth"=3, "Fourth vs fifth" = 4)))