If somebody can tell me what R commands I need to use for a repeated measures ANOVA, I'd really appreciate it. I have trouble with the random term. I've seen random=id, random=id/(treatment*group) and others.

Also can you please indicate to me what the formula for the Bonferroni adjusted intervals is?

  • $\begingroup$ What do you mean by Bonferroni adjusted intervals? Or rather, what do you mean by intervals? Why kind of intervals? Intervals around what? $\endgroup$
    – John
    Jul 24, 2011 at 23:18
  • $\begingroup$ Thank you for your answer. I mean when the repeated measures ANOVA detects a difference between the means, what are the Bonferroni intervals to know which time point and between which groups contributed to this difference. $\endgroup$
    – sara
    Jul 25, 2011 at 4:28
  • $\begingroup$ @sara Please, don't forget to register your account, vote on response(s) that were helpful (or not), and accept the one that fit with your original question. $\endgroup$
    – chl
    Jul 25, 2011 at 20:08

2 Answers 2


I agree with suncoolsu that it is difficult to tell exactly what you were looking for. And in general it's not recommended to do standard repeated measures ANOVAs anymore since there are generally better alternatives.

Nevertheless, perhaps you want to generate a simple stratified ANOVA. By stratified I mean that your effects are measured within another grouping variable, in your case the subject and thus a within subjects design. If your data frame is df and your response variable is y then you might have a within subjects predictor x1 and that crossed with a within subjects predictor x2, and perhaps a between subjects predictor z. To get the full model with all interactions you would use.

myModel <- aov( y ~ x1 * x2 * z + Error(id/(x1*x2)), data = df)

You'll note that within the Error term we are grouping x1, x2, and their interaction under id. Note that z is not in there because it is not a within subjects variable.

Keep in mind further that this data is laid out in long format and you probably need to aggregate it first to run this correctly since a repeated measures design often suggests more samples / subject than conditions in order to get good estimates of each subject's response value. Therefore, df above might be replaced with the following dfa.

dfa <- aggregate ( y ~ x1 + x2 + z + id, data = df, mean)

(BTW, suncoolsu gave a much more modern answer based on multi-level modelling. It's suggested you learn about that if you continue to do repeated measures designs because it is much more powerful, flexible, and allows one to ignore certain kinds of within subjects assumptions (notably sphericity). What I've described is how to do repeated measure ANOVA. You also might want to look at the car, or higher level ez packages in order to do it as well.)

As for your Bonferroni query... it should probably have been a separate question. Nevertheless, that's a bit of a hard one to answer with repeated measures. You could try ?pairwise.t.test. If you give the interactions of all your within variables as the group factor and set paired to true and the correction to bonf you're set. However, straight corrections like that probably are far too conservative. You state at the outset you're only going to use it if there is a significant effect, you probably also have a theoretical reason for making some comparisons, therefore it's not strictly the fishing expedition that Bonferroni (over) corrects for. So, something like...

with( df, pairwise.t.test(y, x1:x2, paired = TRUE, p.adj = 'bonf') )

will do what you want but that's probably not really what you want.

  • $\begingroup$ Thank you so much. I was actually trying to use the aov function and your response was of big help. $\endgroup$
    – sara
    Jul 25, 2011 at 4:25

You should provide more details about your data. From the limited details provided by you, assuming you have a data frame df which has response, trt, time, and subject information, then these are many ways to fit a LME model in R using lme4 package. However, I will illustrate three methods that I think will be useful for you.


# Random intercepts for different subjects but time and trt effects are fixed 
mmod1 <- lmer(response ~ time*trt + (1 | subject), df)

# Random intercepts and trt effects for different subjects, but the time effect is still fixed
mmod2 <- lmer(response ~ time*trt + (1 + trt | subject), df)

# Random intercepts, trt, and time effects for different subjects
mmod3 <- lmer(response ~ time*trt + (1 + trt + time | subject), df)

Once you have the p-values from the model fit above, you can use:

HPDinterval(mmod1, prob = 0.95, ...)
HPDinterval(mmod2, prob = 0.95, ...)
HPDinterval(mmod3, prob = 0.95, ...)

to obtain the 95% CI determined from MCMC sample. Since this CI is obtained from MCMC sampling, it takes into account of the random errors and you won't need to correct for multiple comparisons (I think so, please correct me if I am wrong).

  • $\begingroup$ Thank you so much. I have a data frame that has a response measured at different time points and different treatments. So response, time and treatment. Could you please help me when to choose a random intercept model and when a random intercept and treatment model? $\endgroup$
    – sara
    Jul 25, 2011 at 4:23
  • $\begingroup$ @sara. I think going with a random intercept and slope is a good choice, unless you have a reason that the slopes won't vary with the time. $\endgroup$
    – suncoolsu
    Jul 25, 2011 at 5:51
  • $\begingroup$ Hello again. How was the time taken into account here? Thanks $\endgroup$
    – sara
    Jul 25, 2011 at 18:31
  • $\begingroup$ I edited my answer and made the time dependence explicit. HTH. $\endgroup$
    – suncoolsu
    Jul 25, 2011 at 20:23
  • $\begingroup$ @suncoolsu I'd appreciate if you could provide a few words on why uncorrelated random effects within subjects is preferred over correlated random effects within subjects or just 3 uncorrelated random effects $\endgroup$
    – Matt
    Jul 26, 2011 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.