There are a number of ways to perform repeated measures analysis in R. Here, we'll just examine two - the univariate method using ANOVA and that using linear mixed effects analysis.
aov()
if the "circularity" property (the equivalence of
variances of the differences between repeat observations) is met. For two
repeats, of course, this is not a problem. Assume you have a data frame like
this:
subid age test1 test2 S001 23 17 21 S002 32 16 24 ...
The function make.rm()
will reformat a data frame that contains repeats as multiple columns so that a
call of the form:
myexpr.df<-make.rm(constant=c("subid","age"),repeated=c("test1","test2"),data=myexp.df)
summary(aov(repdat~age*contrasts+Error(subid),myexpr.df))
will perform a simple repeated measures analysis.
Analyzing repeated measures using ANOVA involves stretching out the matrix of
observations in a way that aov()
is able to appropriately partition
the variance for this problem. It is not always appropriate to use this
method, and the reader should not just blindly plug their repeated measures
problem into the cookbook method outlined here and expect to get the correct
answer.
nlme
package. These functions expect the
data to be in the "stretched-out" form produced by make.rm(), so
that you may use this function to transform data that is in a "subject x repeat"
format. Try the example using the data set ergoStool
as follows:
> library(nlme) > data(ergoStool) > summary(lme(effort ~ Type, data = ergoStool, random = ~ 1 | Subject))
This shows that there are differences between type T1 (the reference category) and types T2 and T3, but not T4. Perhaps the contrast of interest to you is whether types T1 and T4, made by manufacturer X, are different from types T2 and T3, made by manufacturer Y.
> XvY<-ifelse(ergoStool$Type=="T1",1,0)+ + ifelse(ergoStool$Type=="T4",1,0)+ + ifelse(ergoStool$Type=="T2",-1,0)+ + ifelse(ergoStool$Type=="T3",-1,0) > summary(lme(effort ~ XvY, data = ergoStool, random = ~ 1 | Subject))
While not as "good" a model as the one in the example, this may be more informative in a particular case.
For a much more detailed treatment of ANOVAs and other methods, get the VR package or Notes on the use of R..." in the Contributed documentation page.