FAQ/anovaR - CBU statistics Wiki

Revision 2 as of 2010-07-20 13:50:44

Clear message
location: FAQ / anovaR

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 may be found [:FAQ/mauchly 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 [attachment:rm.sav 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)