# Repeated-measures ANCOVA in R?

experts,

I would like to ask for advice regarding the analysis of a dataset I am currently working on.

In the experiment subjects were tested in a reaction time task (RT as dependent variable). In random order every subject was tested in two treatment conditions (factor 'A': placebo vs. drug). Also for subject in a third session the level of a blood value was measured (a numeric covariate 'B'). This covariate 'B' is stable within a subject and believed to be a predictor for the effect of factor 'A'. Moreover subjects were grouped according to their genotype (factor 'C'). Also I coded the first or second testing in factor 'D' in order to model potential learning effects.

Please find below some sample code.

# some example data
subject <- c(1,1,2,2,3,3,4,4)
A <- rep(c('placebo', 'drug'), 4) # factor A
B <- c(1,1,4,4,8,8,12,12) # covariate B
C <- c('x','x','y','y','x','x','y','y')
D <- c('1st','2nd','1st','2nd','2nd','1st','2nd','1st')
RT <- c(2,12,1,16,2,26,3,39)

data <- as.data.frame(cbind(subject, A, B, C, D, RT))
data$B <- as.numeric(as.character(data$B))
data$RT <- as.numeric(as.character(data$RT))


I would know how to estimate the overall effect of the covariate 'B' using lm() and how to estimate the overall effect of factor 'A' using aov().

# Model effect of covariate B using lm()
data.lm <- lm(RT~B, data=data)
summary(data.lm)

# Model effect of factor A using aov()
data.aov <- aov(RT~A+Error(subject/A),data=data)
summary(data.aov)


From the comments I understood that the correct ANCOVA model to test effects of A, B and their interaction would be as followed:

# ANCOVA for effects of A, B and their interaction
data.aov2 <- aov(RT~ B * A + Error(subject/A), data = data)
summary(data.aov2)


However if I would like to look at B, C and their interaction the output from summary() does not provide significances anymore which makes me assume the model is not correct:

# ANOVA for effects B, C and their interaction
data.aov3 <- aov(RT ~ B * C + Error(subject), data=data)
summary(data.aov3)


Also of course I would like to know whether it is possible to estimate the most comprehensive model including all factors (A, B, C, D) and their interaction (?). Probably the dataset is too small to estimate this. But how can I know whether my data is sufficient to allow for the estimation of such a complicated model?

# ANOVA for effects B, C and their interaction
data.aov4 <- aov(RT ~ A * B * C * D + Error(subject/A), data=data)
summary(data.aov4)


Also I would like to know whether the same models can be implemented in the same way in case there are repeated-measures of the dependent variable 'RT'? E.g.:

data2 <- rbind(data, data, data, data, data)
data2\$RT <- rnorm(nrow(data2))

# ANOVA for effects B, C and their interaction
data.aov5 <- aov(RT ~ A * B * C * D + Error(subject/A), data=data2)
summary(data.aov5)


Any help regarding this (including hints for an implementation in R as well as literature) is highly appreciated.

Many thanks! Jokel

• How do you have two different values for B per person when B was only taken on the third session? – Matt Albrecht Apr 5 '12 at 3:39
• Sorry for the mistake in the dummy data - I now corrected the data so that B only includes the same value for every subject! – jokel Apr 5 '12 at 5:28
• Also I added two more factors to the data set and some questions related to the modelling of those factors – jokel Apr 5 '12 at 6:15

## 1 Answer

It seems like you're saying that covariate B is correlated with predictor A. In that case, that is not a situation where you can use an ANCOVA. In an ANCOVA your factor B would have to be correlated only with the RT, not the other predictors. If you ever do find a situation where an ANCOVA is appropriate it would just be...

data.aov <- aov(RT~ B + A + Error(subject/A), data = data)

• thanks for the hint! What would be an appropriate model in case A & B are correlated? – jokel Apr 5 '12 at 14:06
• You could try analyzing B as a predictor, you just have to be careful. Some reading up on issues with multiple regression would be very handy to you at this point. – John Apr 5 '12 at 19:52
• Could you recommend some literature? Especially regarding the implementation of mixed-models in R? – jokel Apr 15 '12 at 19:45
• Thank you John for the suggestion. You propose the ANCOVA model: data.aov <- aov(RT~ B + A + Error(subject/A), data = data) I was wondering whether it is at all possible to model a random-effect 'subject' in this case as there is only one measurement of 'B' for each subject? – jokel Apr 18 '12 at 9:17