> #have run ezANOVA first
> # 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")

        Pairwise comparisons using paired t tests 

data:  mdata$score and mdata$mood2 

  A     B    
B 0.026 -    
C 0.015 1.000

P value adjustment method: bonferroni 
> 
> # 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)
Warning: Converting "subject" to factor for ANOVA.
Warning: Converting "mood2" to factor for ANOVA.
Warning: Converting "time" to factor for ANOVA.
Warning: Converting "group2" to factor for ANOVA.
Warning: Collapsing data to cell means. *IF* the requested effects are a subset of the full design, you must use the "within_full" argument, else results may be inaccurate.
> # 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))
Warning: Converting "subject" to factor for ANOVA.
Warning: Converting "mood2" to factor for ANOVA.
Warning: Converting "time" to factor for ANOVA.
Warning: Converting "group2" to factor for ANOVA.
Warning: Collapsing data to cell means. *IF* the requested effects are a subset of the full design, you must use the "within_full" argument, else results may be inaccurate.
Warning message:
Mixed within-and-between-Ss effect requested; FLSD is only appropriate for within-Ss comparisons (see warning in ?ezStats or ?ezPlot). 
> # 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)

># THIS GIVES IDENTICAL OUTPUT TO RM ANOVA USING MOOD AS A FACTOR BECAUSE
># I HAVE USED TWO ORTHOGONAL CONTRASTS TO EXPLAIN A 3 LEVEL FACTOR (MOOD)
># ANY K-1 ORTHOGONAL CONTRASTS CAN BE USED TOGETHER TO EXPLAIN SS IN ANOVA FOR A K
> # LEVEL FACTOR

> # TO LOOK A THESE CONTRASTS INDIVIDUALLY COULD SIMPLY TAKE THE DIFFERENCE SCORE
> AND RUN A ONE WAY ANOVA (BW INTERACTION E.G. GROUP BY TIME) OR ONE SAMPLE T-TEST (MAIN EFFECT
> OF TIME); ALSO MAY BE ABLE TO USE BESPOKE CONTRASTS LIKE HELMERT ONES.

> outp <- ezANOVA(data=mdata, dv=.(score), wid=.(subject), within=.(mood3,time),between=.(group2),detailed=T,type=3)
Warning: Converting "subject" to factor for ANOVA.
Warning: Converting "time" to factor for ANOVA.
Warning: Converting "group2" to factor for ANOVA.

> outp <- ezANOVA(data=mdata, dv=.(score), wid=.(subject), within=.(mood3,time),between=.(group2),detailed=T,type=3)
Warning: Converting "subject" to factor for ANOVA.
Warning: Converting "time" to factor for ANOVA.
Warning: Converting "group2" to factor for ANOVA.
> outp
$ANOVA
             Effect DFn DFd          SSn      SSd            F            p
1       (Intercept)   1  16 1020.5925926 18.03704 905.33059548 1.637243e-15
2            group2   1  16   13.3703704 18.03704  11.86036961 3.336341e-03
3             mood3   2  32   19.9074074 45.96296   6.92989525 3.158542e-03
5              time   1  16   23.1481481 37.37037   9.91080278 6.219390e-03
4      group2:mood3   2  32    0.1296296 45.96296   0.04512490 9.559388e-01
6       group2:time   1  16    0.1481481 37.37037   0.06342914 8.043623e-01
7        mood3:time   2  32    2.9074074 61.74074   0.75344931 4.789078e-01
8 group2:mood3:time   2  32    0.6851852 61.74074   0.17756449 8.381263e-01
  p<.05         ges
1     * 0.862202753
2     * 0.075760756
3     * 0.108772640
5     * 0.124279181
4       0.000794101
6       0.000907441
7       0.017512549
8       0.004183154

$`Mauchly's Test for Sphericity`
             Effect         W          p p<.05
3             mood3 0.9848657 0.89192368      
4      group2:mood3 0.9848657 0.89192368      
7        mood3:time 0.7082474 0.07522939      
8 group2:mood3:time 0.7082474 0.07522939      

$`Sphericity Corrections`
             Effect       GGe       p[GG] p[GG]<.05       HFe       p[HF]
3             mood3 0.9850914 0.003325405         * 1.1223634 0.003158542
4      group2:mood3 0.9850914 0.954246131           1.1223634 0.955938799
7        mood3:time 0.7741421 0.449280837           0.8414513 0.458946950
8 group2:mood3:time 0.7741421 0.782942944           0.8414513 0.801527298
  p[HF]<.05
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")

        Pairwise comparisons using paired t tests 

data:  mdata$score and mdata$var 

   y1      y2      y3      y4      y5     
y2 0.15478 -       -       -       -      
y3 0.02181 0.10753 -       -       -      
y4 0.06088 0.57033 0.35043 -       -      
y5 0.00017 0.01182 0.13426 0.03022 -      
y6 0.00084 0.03822 0.22556 0.09086 0.61361

P value adjustment 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))

        Pairwise comparisons using paired t tests 

data:  score and mood 

  1     2    
2 0.068 -    
3 0.053 0.881

P value adjustment method: none 
> with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

        Pairwise comparisons using paired t tests 

data:  score and mood 

  1    2   
2 0.20 -   
3 0.16 1.00

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

        Pairwise comparisons using paired t tests 

data:  score and mood 

  1     2    
2 0.069 -    
3 0.052 0.579

P value adjustment method: none 
> with(mdat, pairwise.t.test(score, mood, p.adjust.method="bonferroni", paired=T))

        Pairwise comparisons using paired t tests 

data:  score and mood 

  1    2   
2 0.21 -   
3 0.16 1.00

P value adjustment method: bonferroni 
> 
