
#have run ezANOVA first
#could use cbind to put just a few columns in y e.g. y1 y2 y4 y5 for a 2x2 ANOVA
# I use orthogonal contrasts below in this file
 
# for comparisons involving the group factor need to have group defined in the data

pairwise.t.test(mdata$score, mdata$mood2, paired=T, p.adjust.method="bonferroni")

#unpicking group x mood interaction (B x W1) doesn't agree with SPSS although is close in some cases

# gives close to p-values for paired t-tests in group 1 using SPSS which gives p-values of 0.37, 0.04, 0.19 compared to 0.31, 0.008 and 0.08 in R which
# is closer to using a pooled error variance from all three moods which gives a p-value of 0.41,  0.013, 0.05 for the 3 pairwise comparisons using
# the pooled error variance in SPSS

mdat <- mdata[mdata$group2=="controls",]
with(mdat, pairwise.t.test(score, mood, p.adjust.method="none", paired=T))
with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

mdat <- mdata[mdata$group2=="patients",]
with(mdat, pairwise.t.test(score, mood, p.adjust.method="none", paired=T))
with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

>>>>>>>>>> BELOW OF SECONDARY IMPORTANCE TO SHOW THE RM ANOVA IS POOLING
ORTHINGOAL CONTRASTS

# contrast mood A vs mood B (combined sad and angry moods) between groups agrees with SPSS with just two categories

mdata$mood2 <- recode(mdata$mood, "1='A'; 2='B'; 3='B'")
out <- ezANOVA(data=mdata, dv=.(score), wid=.(subject), within=.(mood2,time),between=.(group2),detailed=T,type=3)
# there is also a plot function we can use - looks confusing
ezPlot(data=mdata, dv=.(score), wid=.(subject), within=.(mood2,time),between=.(group2), x=.(time))
# this looks better using interaction.plot from the graphics library
interaction.plot(time,mood,score)

# creating orthogonal contrasts as in Field R book give same results in the ANOVA
# this just shows we have done things correctly in checking ezANOVA has inputted orthogonal contrasts to give us the correct
# SS for the factors such as mood

mdata$mood2 <- recode(mdata$mood, "1='A'; 2='B'; 3='C'")
mdata$mood3 <- factor(mdata$mood2)
con1 <- c(1, 1, -2)
con2 <- c(-1, 1, 0)
contrasts(mdata$mood3)<-cbind(con1, con2)
outp <- ezANOVA(data=mdata, dv=.(score), wid=.(subject), within=.(mood3,time),between=.(group2),detailed=T,type=3)

# as in Field page 594 we use pairwise.t.test to unpick the mood x time (W1 x W2) interaction
pairwise.t.test(mdata$score, mdata$var, paired=T, p.adjust.method="none")

#unpicking group x mood interaction (B x W1) doesn't agree with SPSS although is close in some cases

# gives close to p-values for paired t-tests in group 1 using SPSS which gives p-values of 0.37, 0.04, 0.19 compared to 0.31, 0.008 and 0.08 in R which
# is closer to using a pooled error variance from all three moods which gives a p-value of 0.41,  0.013, 0.05 for the 3 pairwise comparisons using
# the pooled error variance in SPSS

mdat <- mdata[mdata$group2=="controls",]
with(mdat, pairwise.t.test(score, mood, p.adjust.method="none", paired=T))
with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

mdat <- mdata[mdata$group2=="patients",]
with(mdat, pairwise.t.test(score, mood, p.adjust.method="none", paired=T))
with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

# ezANOVA is not very good at producing residuals to checking normality of residuals so we need to use aov instead to fit the mixed ANOVA
# aov doesn't produce sphericity output so we wouldn't use this for the ANOVA itself for mixed models

# aov also fits repeated measures using long format data entry
# make sure aov knows our group predictors are factors
sub <- as.factor(mdata$subject)
y$group = rep(1:2,each=9)
group2 <- rep(y$group,6)
gp2 <- as.factor(group2)
md <- as.factor(mood)
tm <- as.factor(time)
out2 <- aov(data=mdata,score~(gp2*md*tm)+Error(sub/(md*tm)))
summary(out2)
summary(print(model.tables(out2,"means"),digits=3))
summary(print(model.tables(out2,"effects"),digits=3))

# from http://stats.stackexchange.com/questions/6081/testing-the-normality-assumption-for-repeated-measures-anova-in-r quoting
#Page 284 of Venables and Ripley we do plots of lowest stratum in our case which is for the full model with a subjects x md x tm interaction (stratum 5)

library(MASS)
a.pr <- proj(out2)
a.pr2 <- a.pr[[5]][,"Residuals"]
plot(fitted(out2[[5]]), studres(out2[[5]]))
abline(h=0, lty=2)
out2.pr <- proj(out2)
qqnorm(out2.pr[[5]][, "Residuals"], ylab = "Stratum 5 residuals")
qqline(out2.pr[[5]][, "Residuals"])
