# How do I perform a repeated measures analysis of variance in R including the outputting of sphericity corrections?

There are rather confusingly at least three different procedures in R for performing repeated measures (including mixed anovas where one or more between subjects factors are present) analysis in R. These include aov(), anova and Anova(). Of these Anova() additionally produces output containing the Greenhouse-Geisser and Huynh-Feldt corrections for when the sphericity assumption (of equal correlations between pairs of combinations of within subjects factor levels) does not hold. More information on the sphericity assumption, a particular example of a more general assumption known as compound symmetry, may be found here.

The R syntax below if for two within subjects factors mood (which takes the values 'happy, 'sad' and 'angry') and time (2 time points). There is also a between subjects factor called group. The data is given in a SPSS data file here.

The R syntax for running the full model is given below. The sphericity correction factors and corrected p-values for the F ratios are given for each term involving a within subjects factor although the corrected degrees of freedom are not outputted and would need to be computed by multiplying the uncorrected values by the correction factors.

Note that you only need to run the first line once as this installs the Anova() procedure in your R library folder on your PC hard disk. It also assumes you have already got the foreign library on your hard drive.

install.packages(c('Anova','car')) library(car) library(foreign) y <- read.spss('U:\\R_Work\\rep_meas_formatted.sav') y <- data.frame(y) mood <- factor(rep(c('happy', 'sad', 'angry'), c(2,2,2)), levels=c('happy', 'sad', 'angry')) time <- ordered(rep(1:2,3)) idata <- data.frame(mood,time) mod.ok <- lm(cbind(y1, y2, y3, y4, y5, y6) ~ group, data=y) av.ok <- Anova(mod.ok, idata=idata, idesign=~mood*time, type='II') summary(av.ok,multivariate=FALSE)

aov can also be used for doing a repeated measures ANOVA but does not perform a check for sphericity. Notice the 'reshape' function which converts the 'broad' format with one subject per row (as used in SPSS, for exmaple) to the required 'long' format where there is only one response per row. In the example below three columns are created called subject, pairs and corr creating 84 rows for each of the 3 observations on each of 28 subjects.

install.packages("reshape") isr=read.csv("U://My Documents//pairsOnlyStrict.csv") library(reshape) mdata = melt(isr, id=c("subject")) colnames(mdata) = c("subject", "pairs", "corr") head(mdata) disr = data.frame(isr) mdata = data.frame(mdata) aov.out = aov(corr ~ pairs + Error(as.factor(subject)/pairs), data=mdata) summary(aov.out)

You can also use EZANOVA to fit repeated measures ANOVAs using R on data with large numbers of degrees of freedom which may need a substantial sphericity correction. Free downloads and further information are available from here.