# Higher order repeated measures designs & MANOVA

## Two factor repeated measures ANOVA

First let’s load in the sample data. We have organized the data file in a column-wise format, where each row is a subject. This is in preparation for a multivariate approach to the ANOVA. Here we won’t both with the univariate approach, since we are interested in the sphericity tests and the corrected values of the F test.

fname <- "http://www.gribblelab.org/stats2019/data/2wayrepdata.csv"
mydata
##   subject a1b1 a1b2 a1b3 a2b1 a2b2 a2b3
## 1       1  420  420  480  480  600  780
## 2       2  480  480  540  660  780  780
## 3       3  540  660  540  480  660  720
## 4       4  480  480  600  360  720  840
## 5       5  540  600  540  540  720  780

Let’s extract the data matrix (just the numeric values) from the data frame:

dm <- as.matrix(mydata[1:5, 2:7])
dm
##   a1b1 a1b2 a1b3 a2b1 a2b2 a2b3
## 1  420  420  480  480  600  780
## 2  480  480  540  660  780  780
## 3  540  660  540  480  660  720
## 4  480  480  600  360  720  840
## 5  540  600  540  540  720  780

Now let’s create a multivariate linear model object:

mlm1 <- lm(dm ~ 1)

Next we are going to use the Anova() command in the car package, so we have to first load the package. We also have to define a data frame that contains the within-subjects factors. If you don’t have the car package installed, just type install.packages("car") and R will download it and install it.

library(car)
## Loading required package: carData
af <- factor(c("a1","a1","a1","a2","a2","a2"))
bf <- factor(c("b1","b2","b3","b1","b2","b3"))
myfac <- data.frame(factorA=af,factorB=bf)
myfac
##   factorA factorB
## 1      a1      b1
## 2      a1      b2
## 3      a1      b3
## 4      a2      b1
## 5      a2      b2
## 6      a2      b3

Now we will define the anova using Anova():

mlm1.aov <- Anova(mlm1, idata = myfac, idesign = ~factorA*factorB, type="III")
summary(mlm1.aov, multivariate=FALSE)
## Warning in summary.Anova.mlm(mlm1.aov, multivariate = FALSE): HF eps > 1
## treated as 1
##
## Univariate Type III Repeated-Measures ANOVA Assuming Sphericity
##
##                   Sum Sq num Df Error SS den Df  F value    Pr(>F)
## (Intercept)     10443000      1    33600      4 1243.214 3.861e-06 ***
## factorA           147000      1    33600      4   17.500  0.013881 *
## factorB           138480      2    39120      8   14.159  0.002354 **
## factorA:factorB    67920      2    23280      8   11.670  0.004246 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Mauchly Tests for Sphericity
##
##                 Test statistic p-value
## factorB                0.76047 0.66317
## factorA:factorB        0.96131 0.94254
##
##
## Greenhouse-Geisser and Huynh-Feldt Corrections
##  for Departure from Sphericity
##
##                  GG eps Pr(>F[GG])
## factorB         0.80676   0.005291 **
## factorA:factorB 0.96275   0.004857 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##                   HF eps  Pr(>F[HF])
## factorB         1.271242 0.002354093
## factorA:factorB 1.838414 0.004245732

Note how the formula uses the same names as in the myfac data frame we defined.

The first part of the output lists the omnibus F tests for the main effects and the interaction effect. We then see the Mauchly tests of sphericity. We see tests for the main effect of factorB, and the factorA:factorB interaction effect. We don’t see a test of the main effect of factorA, because in this case, factorA has only two levels … and so there is no variances of differences-between-groups… since there are only two levels, there is only a single variance of differences (between the two levels). We then see Greenhouse-Geisser and Huynh-Felft corrections.

### Simple main effects

The factorA:factorB interaction is significant, so we want to conduct so-called simple main effects analyses. This would be testing the effects of one factor (e.g. factorB), separately within each level of factorA (or vice-versa). In a between-subjects two-factor ANOVA, simple main effects are evaluated by doing separate one-way ANOVAs, but using the MSerror term from the overall two-factor analysis as the error term. For within-subjects designs it’s probably better to use separate error terms for each analysis, since the sphericity assumption is likely not true, and repeated measures ANOVA Is sensitive (more so than between-subjects ANOVA) to violations of the sphericity assumption. Therefore we can in fact literally run separate single-factor repeated measures ANOVAs, with one factor, within levels of the other factor.

### Pairwise tests & linear contrasts

The approach for computing linear contrasts (including pairwise tests) is the same as for a single-factor repeated measures design. We can either compute F ratios by taking the appropriate MSerr term from the ANOVA output (this approach assumes sphericity), or we can simple compute differences scores and perform t-tests (this doesn’t assume sphericity). Correcting for Type-I error is up to you — you could use a Bonferroni adjustment, or compute Tukey probabilities, etc.

## Split plot designs

A split plot design is a mixed design in which there are some repeated measures factor(s) and some between-subjects factor(s). Let’s load in some sample data for a study with one repeated measures and one between subjects factor:

fname <- "http://www.gribblelab.org/stats2019/data/splitplotdata.csv"
mdata
##    subject  a1  a2  a3 gender
## 1        1 420 420 480      f
## 2        2 480 480 540      f
## 3        3 540 660 540      f
## 4        4 480 480 600      f
## 5        5 540 600 540      f
## 6        6 439 434 495      m
## 7        7 497 497 553      m
## 8        8 555 675 553      m
## 9        9 492 496 615      m
## 10      10 555 617 555      m

We have three levels of a repeated measures factor (a1, a2, a3) and two levels of a between-subjects factor, gender (m,f), and 10 subjects.

First as before we extract the data corresponding to the dependent variable from the data frame:

dm <- as.matrix(mdata[1:10, 2:4])
dm
##     a1  a2  a3
## 1  420 420 480
## 2  480 480 540
## 3  540 660 540
## 4  480 480 600
## 5  540 600 540
## 6  439 434 495
## 7  497 497 553
## 8  555 675 553
## 9  492 496 615
## 10 555 617 555

Then we formulate our multivariate model:

mlm <- lm(dm ~ 1 + gender, data=mdata)

Note how now dm depends not just on a constant but also on gender. Next we design a data frame that contains the design of the repeated measures factor:

af <- factor(c("a1","a2","a3"))
myfac <- data.frame(factorA=af)
myfac
##   factorA
## 1      a1
## 2      a2
## 3      a3

Now we use the Anova() function to perform the split plot anova:

mlm.aov <- Anova(mlm, idata=myfac, idesign = ~factorA, type="III")
summary(mlm.aov, multivariate=FALSE)
##
## Univariate Type III Repeated-Measures ANOVA Assuming Sphericity
##
##                 Sum Sq num Df Error SS den Df  F value    Pr(>F)
## (Intercept)    4056000      1    71368      8 454.6592 2.461e-08 ***
## gender            1733      1    71368      8   0.1942    0.6711
## factorA           6240      2    40655     16   1.2279    0.3191
## gender:factorA       4      2    40655     16   0.0007    0.9993
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Mauchly Tests for Sphericity
##
##                Test statistic   p-value
## factorA               0.15097 0.0013368
## gender:factorA        0.15097 0.0013368
##
##
## Greenhouse-Geisser and Huynh-Feldt Corrections
##  for Departure from Sphericity
##
##                 GG eps Pr(>F[GG])
## factorA        0.54082     0.3031
## gender:factorA 0.54082     0.9840
##
##                   HF eps Pr(>F[HF])
## factorA        0.5590067  0.3043066
## gender:factorA 0.5590067  0.9858722

### Followup tests

The rules and approach for further tests following significant omnibus ANOVA test(s) are no different than before.