Pedro Alcocer

CV

Personal

Code


Repeated measures ANOVAs in R


Repeated measures ANOVAs in R are performed with the aov() function. The aov() function requires fully balanced data to work correctly, i.e., there can be no missing values in the data. In order to use aov() on data with missing values (most experimental data has excluded values), one needs to average over subjects or items.

Below, we assume a data frame data with at least the columns subject, factor1, factor2, and logRT. The code below averages over subjects, returning the data frame data.subject.

data.subject <- aggregate(data$logRT, list(data$subject, data$factor1, data$factor2), mean)
colnames(data.subject) <- c("subject", "factor1", "factor2", "logRT")

An alternative is to use the excellent reshape package.

library(reshape)
data.subject <- recast(data, SUBJ + factor1 + factor2 ~ variable, mean, measure.var="logRT")

Once you've created the data.subject data frame using one of the methods above, you can then perform the repeated measures ANOVA on data.subject.

summary(aov(logRT ~ factor1 * factor2 + Error(subject/(factor1 * factor2)), data=data.subject))

A by-items analysis is performed in the same way. You'd simply need to replace subject with item where appropriate.


This page gets a lot of search hits, so I'd like to make it as useful as possible. If you find this post useful or confusing, or have questions about it, or have suggestions on how to improve it, please make use of the comments.

View Comments


blog comments powered by Disqus