# Questions about how random effects are specified in lmer

I recently measured how the meaning of a new word is acquired over repeated exposures (practice: day 1 to day 10) by measuring ERPs (EEGs) when the word was viewed in different contexts. I also controlled properties of the context, for instance, its usefulness for the discovery of new word meaning (high vs. low). I am particularly interested in the effect of practice (days). Because individual ERP recordings are noisy, ERP component values are obtained by averaging over the trials of a particular condition. With the lmer function, I applied the following formula:

lmer(ERPindex ~ practice*context + (1|participants), data=base)


and

lmer(ERPindex ~ practice*context + (1+practice|participants), data=base)


I've also seen the equivalent of the following random effects in the literature:

lmer(ERPindex ~ practice*context + (practice|participants) +
(practice|participants:context), data=base)


What is accomplished by using a random factor of the form participants:context? Is there a good source that would allow someone with just cursory knowledge of matrix algebra understand precisely what random factors do in linear mixed models and how they should be selected?

I'm going to describe what model each of your calls to lmer() fits and how they are different and then answer your final question about selecting random effects.

Each of your three models contain fixed effects for practice, context and the interaction between the two. The random effects differ between the models.

lmer(ERPindex ~ practice*context + (1|participants), data=base)


contains a random intercept shared by individuals that have the same value for participants. That is, each participant's regression line is shifted up/down by a random amount with mean $0$.

lmer(ERPindex ~ practice*context + (1+practice|participants), data=base)


This model, in addition to a random intercept, also contains a random slope in practice. This means that the rate at which individuals learn from practice is different from person to person. If an individual has a positive random effect, then they increase more quickly with practice than the average, while a negative random effect indicates they learn less quickly with practice than the average, or possibly get worse with practice, depending on the variance of the random effect (this is assuming the fixed effect of practice is positive).

lmer(ERPindex ~ practice*context + (practice|participants) +
(practice|participants:context), data=base)


This model fits a random slope and intercept in practice (you have to do (practice-1|...) to suppress the intercept), just as the previous model did, but now you've also added a random slope and intercept in the factorparticipants:context, which is a new factor whose levels are every combination of the levels present in participants and context and the corresponding random effects are shared by observations that have the same value of both participants and context. To fit this model you will need to have multiple observations that have the same values for both participants and context or else the model is not estimable. In many situations, the groups created by this interaction variable are very sparse and result in very noisy/difficult to fit random effects models, so you want to be careful when using an interaction factor as a grouping variable.

Basically (read: without getting too complicated) random effects should be used when you think that the grouping variables define "pockets" of inhomogeneity in the data set or that individuals which share the level of the grouping factor should be correlated with each other (while individuals that do not should not be correlated) - the random effects accomplish this. If you think observations which share levels of both participants and context are more similar than the sum of the two parts then including the "interaction" random effect may be appropriate.

Edit: As @Henrik mentions in the comments, the models you fit, e.g.:

lmer(ERPindex ~ practice*context + (1+practice|participants), data=base)


make it so that the random slope and random intercept are correlated with each other, and that correlation is estimated by the model. To constrain the model so that the random slope and random intercept are uncorrelated (and therefore independent, since they are normally distributed), you'd instead fit the model:

lmer(ERPindex ~ practice*context + (1|participants) + (practice-1|participants),
data=base)


The choice between these two should be based on whether you think, for example, participants with a higher baseline than average (i.e. a positive random intercept) are also likely to have a higher rate of change than average (i.e. positive random slope). If so, you'd allow the two to be correlated whereas if not, you'd constrain them to be independent. (Again, this example assumes the fixed effect slope is positive).

• I don't wanna be picky, but doesn't the second model also contains the correlation between the intercept and the slopes. Just adding the slopes should be: lmer(ERPindex ~ practice*context + (1|participants) + (0 + practice|participants, data=base) Or am I wrong? (Unrelated: Excuse my small edit of your post. If you disagree with the clarification, just change it back) – Henrik Jul 4 '12 at 17:00
• @Henrik, yes you're right that it does also estimate the correlation between the two random effects. In writing this answer, I was trying to give a "big picture" idea of what's going on with these models, which didn't include mentioning the correlation between the random effects, which doesn't have a simple "two cent" description the way the slope and intercept do :) In any case, I don't think this omission makes the interpretation I did make in my answer incorrect. BTW, thanks for the edit. – Macro Jul 4 '12 at 17:03
• @Henrik, I added a note about the difference between making the random effects correlated vs. uncorrelated that I think improves the answer - thanks for pointing it out. – Macro Jul 4 '12 at 17:31
• Thanks. I am trying to get into the mixed modeling thing and also struggling with the question of how and when to use which random effects structure, that I just wanted to make sure. In sum, great answer (+1). – Henrik Jul 4 '12 at 18:03
• @pom, thank you for the compliment. Re: your comment, I've tested this on simulated data and I think you have it backwards. The second model under my edit has one fewer parameter than the first. This is because the second model constrains the correlation between the two random effects to be zero. Other than that the models are the same. I'm not sure what you're encountering but a reproducible example would help. Here's mine: x <-rnorm(1000); id <- rep(1:100,each=10); y <- rnorm(1000); g <- lmer(y ~ (1+x|id)); g2 <- lmer(y ~ (1|id) + (x-1|id)); attr(logLik(g),"df"); attr(logLik(g2),"df"); – Macro Aug 26 '13 at 17:57

@Macro has given a good answer here, I just want to add one small point. If some people in your situation are using:

lmer(ERPindex ~ practice*context + (practice|participants) +
(practice|participants:context), data=base)


I suspect they are making a mistake. Consider: (practice|participants) means that there is a random slope (and intercept) for the effect of practice for each participant, whereas (practice|participants:context) means that there is a random slope (and intercept) for the effect of practice for each participant by context combination. This is fine, if that's what they want, but I suspect they want (practice:context|participants), which means that there is a random slope (and intercept) for the interaction effect of practice by context for each participant.

In a random effects or mixed effects model, a random effect is used when you want to treat the effect that you observed as if it were drawn from some probability distribution of effects.

One of the best examples I can give is when modeling clinical trial data from a multicentered clinical trial. A site effect is often modeled as a random effect. This is done because the 20 or so sites that were actually used in the trial were drawn from a much larger group of potential sites. In practice, the selection may not have been at random, but it still may be useful to treat it as if it were.

While the site effect could have been modeled as a fixed effect, it would be hard to generalize results to a larger population if we didn't take into account the fact that the effect for a different selected set of 20 sites would be different. Treating it as a random effect allows for us to account for it that way.

• -1 because this answer does not address the actual questions here. – amoeba Aug 27 '16 at 22:25