I have a dataset of a psychological experiment with two within-subject factors. For simplicity, let's assume I'm collecting reaction time (RT) for the stimulus factors of color (red/blue/green) and shape (square/triangle). I'm interested both in the main effects and in the interaction.

For each subject, only the mean RT of each the six conditions is available (i.e. 6 data points per subject). Traditionally, such a dataset would have be analyzed by a repeated measures ANOVA with two factors. However, many of the observations are missing at random, so I want to use a mixed-effects model instead. Naively translating the repeated measures anova as I understand it to mixed effects terms, I get (lmer syntax)

Y ~ color*shape + 1|subject

However, I suspect that this model isn't maximal in the sense implied here. I can add color and shape random slopes:

Y ~ color*shape + color|subject + shape|subject + 1|subject

but the their meaning and justification in this context of discrete-levels factors isn't clear to me at all.

  1. I'd appreciate a principled explanation of what is the right maximal model for this simple case and why, or at least a helpful reference for that 'why'.

  2. If I'd add a third BETWEEN-subjects factor (e.g. subjects' sex), would modelling that require a different logic?


2 Answers 2


The maximal structure would need to include also a random effect for the interaction between color and shape, that is:

Y ~ color * shape + (color + shape + color:shape | subject)

This will result in all your predictors (color, shape and their interaction) having a fixed effect (constant for all subjects), and a random effect (individual fluctuations around the estimated fixed effect). In this sense the model is the maximal one. Note that it might not be fully equivalent to a repeated-measures ANOVA as it doesn't make equally strict assumptions on the correlational structure (see Tom's answer).

If you don't include the interaction in the random effect part of the formula, individual variation in the interaction effect will not be considered as "random", and the model will not be equivalent to a repeated-measures ANOVA. Of course, the variance of the random deviates for the interaction (or any other random effect) might be so small that including it in the model do not improve much the fit. You can check this not only with the AIC, but with a likelihood ratio test, as model with vs without one random effect are nested one another. In principle if the likelihood ratio test is not significant, it means that you can safely remove that random effect. Simplifying the random effect structures by removing negligible components would be an example of what in the article you linked is called data-driven approach.

You can simplify the model in this way, and it would still be equivalent to a repeated-measures ANOVA:

Y ~ color*shape + (1|subject) + (0+color|subject) + (0+shape|subject) + (0+color:shape|subject)

This syntax tells lmer to not estimate the correlations of random deviates across subjects. The drawback here is that, for example, you won't be able to tell whether subjects that have a large effect of color tend to have also a larger effect of shape (or smaller effect, in case of negative correlation).

You can easily include a between-subjects predictor, the only difference is that you can't add a random effect for it. "gender" for example cannot have a random effect grouped according to subject, but it can interact with the other fixed effects, e.g.:

Y ~ color * shape * gender + (color + shape + color:shape | subject)
  • $\begingroup$ I don't think that the lmer model above would be exactly analogous to the repeated measures one, as it would not enforced compound symmetry, which you do in a repeated measures analysis. In lme you can specify such a correlation structure using option correlation=corCompSymm(form=~1|id) but not in lmer I believe... See also my post here stats.stackexchange.com/questions/122717/… $\endgroup$ Nov 12, 2015 at 11:10
  • $\begingroup$ Right, indeed I don't know if and how you could enforce it in lmer. I edited my response to acknowledge it. However, I am not clear with why you would want to enforce compound symmetry in this case (or similar ones) ? $\endgroup$
    – matteo
    Nov 14, 2015 at 14:41
  • 1
    $\begingroup$ No true - enforcing compound symmetry would only be a means to exactly replicate the results of a repeated measures ANOVA, or perhaps to fit fewer parameters in your analysis, and would be OK as long as compound symmetry would actually be met. The advantage of analysing it as a linear mixed model, especially with lme then, is that you could fit much more flexible dependency structures (e.g. a general correlation structure in lme using option correlation=corSymm(form = ~ 1|id) ). I am not totally sure in fact which variance-covariance matrix lmer would use for the models above. $\endgroup$ Nov 14, 2015 at 15:35
  • $\begingroup$ Ha and the flexlambda branch of lmer can apparently enforce compound symmetry if you would like that, github.com/lme4/lme4/blob/flexLambda/misc/ex_flexLambda.Rmd $\endgroup$ Nov 14, 2015 at 15:35
  • 2
    $\begingroup$ Note that compound symmetry is for the variance of the outcome, not the random effects, the second link given by @tom-wenseleers is actually for random effects. The compound symmetry structure is similar to the variance structure of random-intercept-only model. If there are random slopes in the model, the variance structure would not be compound symmetry. Some descriptions about repeated ANOVA can be found here. $\endgroup$
    – Randel
    Nov 15, 2015 at 17:41

First I should say that if your aim was to formulate a mixed model that was exactly analogous to a repeated measures ANOVA you would also have to enforce compound symmetry, which in lme would be done as follows

fit=lme(Y ~ color*shape, random=~1|subject, 
            correlation=corCompSymm(form=~1|id), weights=NULL, 
Y ~ color*shape + 1|subject

(you could also use a general correlation structure to relax the assumption of compound symmetry)

Adding random slopes in lmer can sometimes improve your fit, but not always. Best is to check the Aikaike Information Criterion (AIC(fit)) and see if it is actually better than a simpler random intercept model.

Difference in interpretation would basically be that in a random intercept model, all that you add to the model is some random per subject variation in mean reaction time. If you add random slopes then this will also allow the effect of color and/or shape on reaction time to vary across subjects. Note also that you could allow correlated or uncorrelated random intercepts & slopes.

A random intercept model (1|subject) would merely include random variation in mean reaction time across subjects

A correlated intercept & slope model (color|subject) = (1+color|subject) would have a random effect of color on reaction time for each subject (so that the effect of color on reaction time is different across subjects) and would include a correlated estimate of a per-subject intercept (ie so that mean reaction time is different per subject and that this difference could be correlated to some extent with the difference in response to each color)

A random slope model (0+color|subject) = (-1+color|subject) would allow for a random effect of color on reaction time (so that the effect of color on reaction time is different across subjects) but would force the mean intercept to be the same for all subjects (ie so that the mean reaction time of all subjects would be the same (after correcting for the effect of color and shape if you would include those as fixed terms))

Finally you could also fit a random slope & intercept model with uncorrelated slopes & intercepts using (1|subject) + (0+color|subject) as this would allow random intercepts over subjects (ie so that mean reaction time is different per subject) and allow for uncorrelated random variation in the effect of color on reaction time per subject (so that the effect of color on reaction time is different across subjects)

So I supposed a full model would be Y ~ color*shape + (color|subject) + (shape|subject) (with correlated random slopes and intercepts) or Y ~ color*shape + (1|subject) + (0+color|subject) + (0+shape|subject) (with uncorrelated random slopes and intercepts)

In lme you could also still fit different types of correlation and variance structures as well though. Best to use AIC to compare the fit of those.

  • 1
    $\begingroup$ uncorrelated random effects can also be specified using ||, e.g. (1+color||subject) instead of (1|subject) + (0+color|subject) $\endgroup$ Mar 17, 2019 at 10:40

Your Answer

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

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