1 Introduction

Rambling notes on deciphering linear mixed effects models using lme4 in R.

Here are the libraries we will be using.

library(readxl)
library(tidyverse)
library(ggplot2)
library(lme4)
library(lmerTest)

2 Notation & nomenclature

lm=linear model, lmer = linear mixed effect

tilde (~) = “predicted by”. For example, lm(IQ ~ Age) examines the linear relationship of IQ as predicted by Age.

REML = restricted maximum likelihood estimation; the default in lme4

3 Random vs. Fixed Effects

A fixed effect is a variable of interest. If you are interested in modeling a specific variable’s contribution to the model, enter it as a fixed effect.

A variable that is controlled/blocked is a random effect. Its variance will still be computed, but you won’t get a parameter estimate in the summary statistics. So if you wanted to manipulate dose of Tylenol on headache severity while controlling for sex, time of day, etc – Tylenol Dose is the fixed effect.

3.0.1 Random effects

The notation (1|___) denotes a random effect. The “1” means a random intercept. For example, here’s a model with a single fixed effect (Age), controlling for the random effect of where you are from (City):
how.smart <- lmer(IQ ~ Age + (1 | City), data=XX)

Another way of specifying the effects of individual participant(s) over time. (1 + Time | Person)

3.0.2 Fixed effects

Just add the variable name, nothing fancy. Here’s age as the fixed effect.
how.smart <- lmer(IQ ~ Age + (1 | City))

3.0.3 Correlated fixed effects: modeled as correlated random slopes

Let’s say you are interested in Age and Income as fixed effects, but these variables are correlated. You need to build the correlation into the model as a random effect slope since you are not interested specifically in the correlation itself as a meaningful fixed effect but you do need to partial its variance.

how.smart <- lmer(IQ ~ Age + Income + (Age | Income) + (1| City))

3.0.4 Uncorrelated fixed effects: modeled

R assumes that the fixed effects are correlated. If they’re not, you should model as uncorrelated random effects slope using the double pipe ||

Let’s say, for example, that you have two uncorrelated fixed effects – Age, ApoE4 allele presence. Age has nothing to do with APOE4 presence. Model the null correlation as an uncorrelated random slope:
how.demented <- lmer(DementiaSeverity ~ Age + ApoE4 + (Age || ApoE4))

3.0.5 Interactions among fixed effects

m <- lmer(IQ ~ Age * Income + (1 + Age | PersonID), data=XX, REML=F)
Age * Income specifies all main effects and interactions. You could also do this as: Age + Income + Age:Income

4 Summary/output of lmer

summary(m) produces a bunch of useful detail about your model
coef(m) produces parameter estimates/coefficients
fixef(m) produces parameter estimates for the fixed effects
ranef(m) extracts random effect coefficients
confint(m) produces confidence intervals

5 Explore ChickWeight

head(ChickWeight, n = 10)
## Grouped Data: weight ~ Time | Chick
##    weight Time Chick Diet
## 1      42    0     1    1
## 2      51    2     1    1
## 3      59    4     1    1
## 4      64    6     1    1
## 5      76    8     1    1
## 6      93   10     1    1
## 7     106   12     1    1
## 8     125   14     1    1
## 9     149   16     1    1
## 10    171   18     1    1

5.1 Plot chickweights by diet

ggplot(data = ChickWeight, aes(Time, weight, group = Chick, color = as.factor(Chick))) + 
    geom_line() + facet_wrap(~Diet)

5.2 Plot it smooth

ggplot(data = ChickWeight, aes(Time, weight, group = Diet, color = as.factor(Diet))) + 
    geom_smooth(method = "glm")