#setwd(choose.dir()) ## set working directory source("S1 ANOVA-Venn Diagrams.r") source("S2 multichoose.r") library(lsr) #table 1 (m3=createUnbalancedMatrix12(10)) # dataset reproduced from Shaw & Mitchell-Olds (1993) Ecology 74(6), pp. 1638-1645 height = y <- c( 50, 57, 57, 71, 85, 91, 94, 102, 110, 105, 120 ) #height treatment <- c( rep( "0control", 2 ), rep( "1removal", 3 ), rep( "0control", 4 ), rep( "1removal", 2 ) ) initial.size <- c( rep( "0small", 2 ), rep( "0small", 3 ), rep( "1large", 4 ), rep( "1large", 2 )) # Shaw.MitchellOlds <- data.frame( height, treatment, initial.size ) # tapply( height, list(initial.size, treatment), mean ) # cell means # model.matrix(lm(height~treatment*initial.size, data=Shaw.MitchellOlds)) #check model matrix # Anova(lm(height~treatment*initial.size, data=Shaw.MitchellOlds), type=3) # wrong results # summary(lm(height~treatment*initial.size, data=Shaw.MitchellOlds)) #SS table 2 a=as.numeric(factor(treatment))-1 # use 0,1 coding for analysis b=as.numeric(factor(initial.size))-1 lmres= lm(y~a+b+a:b) anova(lmres) # type IA anova(lm(y~b+a+a:b)) # type IB Anova(lmres, type = 2) # type II Anova(lmres, type = 3) # type III #Figure 1 (comp=SSComp_A2xB2(y, a, b)) AnovaVenn_A2xB2(comp) #Table 3 a2 = 2*a - 1 # use -1,1 coding for analysis b2 = 2*b - 1 lmres= lm(y~a2+b2+a2:b2) anova(lmres) # type IA anova(lm(y~b2+a2+a2:b2)) # type IB Anova(lmres, type = 2) # type II Anova(lmres, type = 3) # type III #Figure 2 (comp2=SSComp_A2xB2(y, a2, b2)) AnovaVenn_A2xB2(comp2) # table 4 # run a simulation set.seed(12) sim=1000 # 0, 1 coding A1 = c(rep(0,500), rep(1,500)) B1= c(rep(0,250), rep(1,250), rep(0,250), rep(1,250)) # -1, +1 coding A2 = c(rep(-1,500), rep(1,500)) B2 = c(rep(-1,250), rep(1,250), rep(-1,250), rep(1,250)) lm1 = matrix(0, 1000, 5) lm2 = matrix(0, 1000, 5) lm3 = matrix(0, 1000, 5) lm4 = matrix(0, 1000, 5) an1=matrix(0, 1000, 4) an2=matrix(0, 1000, 4) an3=matrix(0, 1000, 4) an4=matrix(0, 1000, 4) for (i in 1:sim){ error = rnorm(1000,sd=10) Y1 = 10+4*A1+4*B1+4*A1*B1+error Y2 = 10+4*A2+4*B2+4*A2*B2+error # linear regression lm=lm(Y1~A1*B1) lm1[i,1:4] = lm$coefficients lm1[i,5]=summary(lm)$sigma an1[i,1:4]=anova(lm)$"Sum Sq" lm=lm(Y1~A2*B2) lm2[i,1:4] = lm$coefficients lm2[i,5]=summary(lm)$sigma an2[i,1:4]=anova(lm)$"Sum Sq" lm=lm(Y2~A1*B1) lm3[i,1:4] = lm$coefficients lm3[i,5]=summary(lm)$sigma an3[i,1:4]=anova(lm)$"Sum Sq" lm=lm(Y2~A2*B2) lm4[i,1:4] = lm$coefficients lm4[i,5]=summary(lm)$sigma an4[i,1:4]=anova(lm)$"Sum Sq" } round(colMeans(lm1),1) round(colMeans(an1),0) round(colMeans(lm2),1) round(colMeans(an2),0) round(colMeans(lm3),1) round(colMeans(an3),0) round(colMeans(lm4),1) round(colMeans(an4),0) #code01=F # please choose F (congruous) or T (incongruous) code01=F # choose simulation method 0,1 or -1,1. docentralize=F # choose to centralize dependent variable (or not) after simulation filename = ifelse(!docentralize, ifelse(code01,"S6 simdata01.txt" , "S7 simdata-11.txt") , ifelse(code01,"S8 simdata01c.txt" , "S9 simdata-11c.txt")) simdata = read.table(filename, header=T) # cells are identified as X00, X01, X10 and X11 table(simdata$I) #colnames(simdata) # Table 5 (code01=F, docentralize=F) and Table 7 (code01=T, docentralize=F) # and Table XX (code01=T, docentralize=T) (not in manuscript) # centralizing the dependent varaible does not solve anything; the results are # similar to the uncentralized version.) # percentages of H0 rejections effects=c("effAb1","effBb1","effABb1","effAu2","effBu2","effABu2","effAu3","effBu3","effABu3") rbind(round(colMeans(simdata[(simdata$I==0),effects]),3), round(colMeans(simdata[(simdata$I==4),effects]),3), round(colMeans(simdata[(simdata$I==8),effects]),3)) m4=simdata[(simdata$I==0),] m5=simdata[(simdata$I==4),] m6=simdata[(simdata$I==8),] # Table 6 (code01=F) and Table 8 (code01=T) # interaction = 0 SS II higher1=m4$effAu2 > m4$effAb1 sum(higher1); round(sum(higher1)/nrow(m4)*100,1) higher2=m4$effBu2 > m4$effBb1 sum(higher2); round(sum(higher2)/nrow(m4)*100,1) higher3=m4$effABu2 > m4$effABb1 sum(higher3); round(sum(higher3)/nrow(m4)*100,1) lower1=m4$effAu2 < m4$effAb1 sum(lower1); round(sum(lower1)/nrow(m4)*100,1) lower2=m4$effBu2 < m4$effBb1 sum(lower2); round(sum(lower2)/nrow(m4)*100,1) lower3=m4$effABu2 < m4$effABb1 sum(lower3); round(sum(lower3)/nrow(m4)*100,1) # interaction = 0 SS III higher4=m4$effAu3 > m4$effAb1 sum(higher4); round(sum(higher4)/nrow(m4)*100,1) higher5=m4$effBu3 > m4$effBb1 sum(higher5); round(sum(higher5)/nrow(m4)*100,1) higher6=m4$effABu3 > m4$effABb1 sum(higher6); round(sum(higher6)/nrow(m4)*100,1) lower4=m4$effAu3 < m4$effAb1 sum(lower4); round(sum(lower4)/nrow(m4)*100,1) lower5=m4$effBu3 < m4$effBb1 sum(lower5); round(sum(lower5)/nrow(m4)*100,1) lower6=m4$effABu3 < m4$effABb1 sum(lower6); round(sum(lower6)/nrow(m4)*100,1) #interaction = 4 SS II higher7=m5$effAu2 >m5$effAb1 sum(higher7); round(sum(higher7)/nrow(m4)*100,1) higher8=m5$effBu2 >m5$effBb1 sum(higher8); round(sum(higher8)/nrow(m4)*100,1) higher9=m5$effABu2 >m5$effABb1 sum(higher9); round(sum(higher9)/nrow(m4)*100,1) lower7=m5$effAu2 < m5$effAb1 sum(lower7); round(sum(lower7)/nrow(m4)*100,1) lower8=m5$effBu2 < m5$effBb1 sum(lower8); round(sum(lower8)/nrow(m4)*100,1) lower9=m5$effABu2 < m5$effABb1 sum(lower9); round(sum(lower9)/nrow(m4)*100,1) #interaction = 4 SS III higher10=m5$effAu3 >m5$effAb1 sum(higher10); round(sum(higher10)/nrow(m4)*100,1) higher11=m5$effBu3 >m5$effBb1 sum(higher11); round(sum(higher11)/nrow(m4)*100,1) higher12=m5$effABu3 >m5$effABb1 sum(higher12); round(sum(higher12)/nrow(m4)*100,1) lower10=m5$effAu3 < m5$effAb1 sum(lower10); round(sum(lower10)/nrow(m4)*100,1) lower11=m5$effBu3 < m5$effBb1 sum(lower11); round(sum(lower11)/nrow(m4)*100,1) lower12=m5$effABu3 < m5$effABb1 sum(lower12); round(sum(lower12)/nrow(m4)*100,1) #interaction = 8 SS II higher13=m6$effAu2 >m6$effAb1 sum(higher13); round(sum(higher13)/nrow(m6)*100,1) higher14=m6$effBu2 >m6$effBb1 sum(higher14); round(sum(higher14)/nrow(m6)*100,1) higher15=m6$effABu2 >m6$effABb1 sum(higher15); round(sum(higher15)/nrow(m6)*100,1) lower13=m6$effAu2 < m6$effAb1 sum(lower13); round(sum(lower13)/nrow(m6)*100,1) lower14=m6$effBu2 < m6$effBb1 sum(lower14); round(sum(lower14)/nrow(m6)*100,1) lower15=m6$effABu2 < m6$effABb1 sum(lower15); round(sum(lower15)/nrow(m6)*100,1) #interaction = 8 SS III higher16=m6$effAu3 >m6$effAb1 sum(higher16); round(sum(higher16)/nrow(m6)*100,1) higher17=m6$effBu3 >m6$effBb1 sum(higher17); round(sum(higher17)/nrow(m6)*100,1) higher18=m6$effABu3 >m6$effABb1 sum(higher18); round(sum(higher18)/nrow(m6)*100,1) lower16=m6$effAu3 < m6$effAb1 sum(lower16); round(sum(lower16)/nrow(m6)*100,1) lower17=m6$effBu3 < m6$effBb1 sum(lower17); round(sum(lower17)/nrow(m6)*100,1) lower18=m6$effABu3 < m6$effABb1 sum(lower18); round(sum(lower18)/nrow(m6)*100,1) # delta's # interaction 0 SSII round(mean(m4[higher1,"effAu2"]-m4[higher1,"effAb1"]), 3) round(mean(m4[higher2,"effBu2"]-m4[higher2,"effBb1"]), 3) round(mean(m4[higher3,"effABu2"]-m4[higher3,"effABb1"]), 3) round(mean(m4[lower1,"effAu2"]-m4[lower1,"effAb1"]), 3) round(mean(m4[lower2,"effBu2"]-m4[lower2,"effBb1"]), 3) round(mean(m4[lower3,"effABu2"]-m4[lower3,"effABb1"]), 3) # interaction 0 SSIII round(mean(m4[higher4,"effAu3"]-m4[higher4,"effAb1"]), 3) round(mean(m4[higher5,"effBu3"]-m4[higher5,"effBb1"]), 3) round(mean(m4[higher6,"effABu3"]-m4[higher6,"effABb1"]), 3) round(mean(m4[lower4,"effAu3"]-m4[lower4,"effAb1"]), 3) round(mean(m4[lower5,"effBu3"]-m4[lower5,"effBb1"]), 3) round(mean(m4[lower6,"effABu3"]-m4[lower6,"effABb1"]), 3) # interaction 4 SSII round(mean(m5[higher7,"effAu2"]-m5[higher7,"effAb1"]), 3) round(mean(m5[higher8,"effBu2"]-m5[higher8,"effBb1"]), 3) round(mean(m5[higher9,"effABu2"]-m5[higher9,"effABb1"]), 3) round(mean(m5[lower7,"effAu2"]-m5[lower7,"effAb1"]), 3) round(mean(m5[lower8,"effBu2"]-m5[lower8,"effBb1"]), 3) round(mean(m5[lower9,"effABu2"]-m5[lower9,"effABb1"]), 3) # interaction 4 SSIII round(mean(m5[higher10,"effAu3"]-m5[higher10,"effAb1"]), 3) round(mean(m5[higher11,"effBu3"]-m5[higher11,"effBb1"]), 3) round(mean(m5[higher12,"effABu3"]-m5[higher12,"effABb1"]), 3) round(mean(m5[lower10,"effAu3"]-m5[lower10,"effAb1"]), 3) round(mean(m5[lower11,"effBu3"]-m5[lower11,"effBb1"]), 3) round(mean(m5[lower12,"effABu3"]-m5[lower12,"effABb1"]), 3) # interaction 8 SSII round(mean(m6[higher13,"effAu2"]-m6[higher13,"effAb1"]), 3) round(mean(m6[higher14,"effBu2"]-m6[higher14,"effBb1"]), 3) round(mean(m6[higher15,"effABu2"]-m6[higher15,"effABb1"]), 3) round(mean(m6[lower13,"effAu2"]-m6[lower13,"effAb1"]), 3) round(mean(m6[lower14,"effBu2"]-m6[lower14,"effBb1"]), 3) round(mean(m6[lower15,"effABu2"]-m6[lower15,"effABb1"]), 3) # interaction 8 SSIII round(mean(m6[higher16,"effAu3"]-m6[higher16,"effAb1"]), 3) round(mean(m6[higher17,"effBu3"]-m6[higher17,"effBb1"]), 3) round(mean(m6[higher18,"effABu3"]-m6[higher18,"effABb1"]), 3) round(mean(m6[lower16,"effAu3"]-m6[lower16,"effAb1"]), 3) round(mean(m6[lower17,"effBu3"]-m6[lower17,"effBb1"]), 3) round(mean(m6[lower18,"effABu3"]-m6[lower18,"effABb1"]), 3) # tools for further interpretation higherBi= which(m5$Bi > 0) ; length(higherBi) higherAi = which(m5$Ai > 0); length(higherAi) m5[higher7,] m5[higherBi,] m5[higher8,] m5[higherAi,] m6[higher13,] m6[higherBi,] m6[higher14,] m6[higherAi,] round(cor(simdata[,c("effAu2", "Ai","Bi","ABi2","I","rAB")]), 2) summary(lm(effAu2~Ai+Bi+ABi2+effABu2, data=simdata)) summary(lm(effBu2~Ai+Bi+ABi2+effABu3, data=simdata)) summary(lm(effAu3~Ai+Bi+ABi2+effABu2, data=simdata)) summary(lm(effBu3~Ai+Bi+ABi2+effABu3, data=simdata)) summary(simdata[,c("Ai","Bi","ABi2","effABu2","rAB")]) #round(cor(simdata),2) summary(lm(effAu2~Ai+Bi+I, data=simdata)) summary(lm(effBu2~Ai+Bi+I, data=simdata)) summary(lm(effAu3~Ai+Bi+I, data=simdata)) summary(lm(effBu3~Ai+Bi+I, data=simdata)) summary(lm(effAu2~Ai+Bi+effABu2, data=simdata)) summary(lm(effBu2~Ai+Bi+effABu2, data=simdata)) summary(lm(effAu3~Ai+Bi+effABu3, data=simdata)) summary(lm(effBu3~Ai+Bi+effABu3, data=simdata)) # additional analysis round(etaSquared(lm(effAu2~Ai+Bi+ABi2+effABu2, data=simdata), anova=T),2) round(etaSquared(lm(effBu2~Ai+Bi+ABi2+effABu2, data=simdata), anova=T),2) round(etaSquared(lm(effAu3~Ai+Bi+ABi2+effABu2, data=simdata), anova=T),2) round(etaSquared(lm(effBu3~Ai+Bi+ABi2+effABu2, data=simdata), anova=T),2) #additional analysis dAub=simdata$effAu2-simdata$effAb1 p=as.numeric(dAub>0) sum(p)/length(p) simdata2=cbind(simdata,dAub) round(etaSquared(lm(dAub~Ai+Bi+ABi2+X11+ABi+effABu2, data=simdata2), anova=T),2) dBub=simdata$effBu2-simdata$effBb1 p=as.numeric(dBub>0) sum(p)/length(p) simdata2=cbind(simdata,dBub) round(etaSquared(lm(dBub~Ai+Bi+ABi2+X11+effABu2, data=simdata2), anova=T),2) dAub3=simdata$effAu3-simdata$effAb1 p=as.numeric(dAub3>0) sum(p)/length(p) simdata2=cbind(simdata,dAub3) summary(lm(dAub3~Ai+Bi+X11+ABi2+effABu3, data=simdata2)) round(etaSquared(lm(dAub3~Ai+Bi+X11+ABi2+effABu3, data=simdata2), anova=T),2) dBub3=simdata$effBu3-simdata$effBb1 summary(dBub3) p=as.numeric(dBub3>0) sum(p)/length(p) simdata2=cbind(simdata,dBub3) round(etaSquared(lm(dBub3~Ai+Bi+X11+ABi2+effABu3, data=simdata2), anova=T),2)