Skip to content
Prev 267 / 20628 Next

syntax lmer random effects

Nathan:

Here are a few small examples given what you have provided below. To use lmer with an intercept only and random intercepts:

fit1 <-  lmer(z ~ 1 + (1|ID), data, method='ML')

Now, to fit a model with a fixed effect, 'x', and to allow for the intercept and for the variable x to vary randomly, use the following:

fit2 <-  lmer(z ~ x + (x|ID), data, method='ML')

Notice that the random terms are always (x|ID), where x is the variable you want to have as the random effect, followed by a "pipe" (|) and then the grouping variable. In this example the grouping variable is ID. So, you have a random variable x, "given", or "conditional on" ID.

Also notice I use method='ML'. You said you wanted to compare models and these models differ in their fixed effects. REML is the default estimation procedure, and so you need this portion of code.