Add random effects to a model formula

The structure of a basic linear model (not a linear mixed model) is:

lm(outcome ~ predictor, data = mydata)

In order to fit an LMM, we need to use the package lme4.

library(lme4)

lme4 gives us the function lmer() for modelling a continuous outcome and glmer(family = binomial) for modelling a binary outcome.

First, the following code shows a model with random intercepts by grouping variable (aka, intercept adjustments by grouping variable).

lmer(
  outcome ~ predictor + (1 | grouping variable), 
  data = mydata
)

The following code shows a model with random intercepts by grouping variable (aka, intercept adjustments by grouping variable) as well as random slopes over predictor by grouping variable (aka, adjustments to the slope over predictor by grouping variable).

lmer(
  outcome ~ predictor + (1 + predictor | grouping variable), 
  data = mydata
)

To model more than one grouping variable—imagine we now have groupvar1 and groupvar2—we add on more than one parenthetical random effect term.

lmer(
  outcome ~ predictor + (1 + predictor | groupvar1) + (1 | groupvar2),
  data = mydata
)

Imagine gathering data from many children, some of whom attend School A, others attend School B, etc. In this case, the “child” grouping variable is nested within the “school” grouping variable.

Another example: imagine gathering data from elderly dementia patients, some of whom are treated at Hospital A, others at Hospital B … Here, the “patient” grouping variable is nested within the “hospital” grouping variable.

To model nested structures like this, an extra bit of syntax is required in the random effect specification. When specifying that child is nested within school, in place of the grouping variable we can write child:school:

lmer(
  outcome ~ predictor + (1 + predictor | child:school),
  data = mydata
)

And since school is its own grouping variable, it probably gets its own random effect term as well, potentially with a random slope over predictor (always make this decision based on the affordances of your data!).

lmer(
  outcome ~ predictor + (1 + predictor | child:school) + (1 + predictor | school),
  data = mydata
)

Linked flash cards