# 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)
```

# 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

# 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.

### 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)

### Fixed effects

Just add the variable name, nothing fancy. Here’s age as the fixed effect.

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

### 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

# 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

# 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
```

## Plot chickweights by diet

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

## Plot it smooth

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