Accessible version · How to use this site with a screen reader · Course home
Day 3 Morning · Accessible lecture notes

Mixed effects models

A screen-reader-friendly transcript of the lecture slides: all slide text, tables, figure descriptions, and speaker notes.

About these notes

Each section is one slide. Figures carry text descriptions where available. For the concepts with worked examples, also read the plain-language summary and lab for this session.

Slide 1

Mixed Effects Modeling

Speaker notes for this slide

a

Slide 2: What are Fixed and Random Effects?

Slide 3: Mixed Effects Models

Slide 4: A Simple Example

Slide 5: Three Options for Structured Data

Slide 6: Complete pooling: Fit of the Standard Linear Model (complete pooling of observations)

Figure 6.1. Scatterplot of fish density against stream from the complete-pooling linear model; all points share one fitted mean, so between-stream variation is visible in the scatter but unmodeled.

This analysis attributed all the error to within-stream variation. There is between-stream variation in density but (1) we have no way to comment on it, and (2) independence violated (are 1000 measurements from 2 streams just as informative about fish density in typical stream as 10 measurements from 200 streams)

Slide 7: No pooling: Fit a Model with a “Stream-Effect”

The residual pattern has been removed with fixed effects, but we have no overall mean and no way to comment on between-stream variance, i.e. we cannot say anything about the population of streams.

Slide 8: Partial pooling: A Mixed Effects Model or really random effect model in this simplified case

Within stream variation relative to stream mean

Variation in stream mean

yi,j = β + bi + εi,j

bi ~ N(0; σ2b)

εi,j ~ N(0; σ2)

Slide 9: Finally: A Mixed Effects Model (Some notes-I)

Slide 10: Finally: A Mixed Effects Model

The results from fitting the mixed effects model are:

What can we learn from this?

β = 78.63; σb = 15.79; σ = 6.1

Slide 11: Time for some R

Group

What varies by group

Slide 12: Packages

Slide 13: The LME Function

Slide 14: Linear Mixed Models (Notes)

Speaker notes for this slide

REML (Restricted Maximum Likelihood): Estimates variance components (random effects + residual variance) after removing the influence of fixed effects. This makes variance component estimates less biased, especially in small samples. However, because the likelihood is computed after conditioning out fixed effects, REML likelihood values are not comparable across models with different fixed effects structures. The “scale” of the REML likelihood changes with the fixed effects design matrix. ML (Maximum Likelihood): Fits both variance components and fixed effects together. ML likelihoods are comparable across models with different fixed effects, so you can use AIC (or likelihood ratio tests) to decide among competing fixed-effects structures.

Slide 15: Linear Mixed Models (Understanding the output)

The among-stream SD

The within-stream SD

The fixed effects

Slide 16: Fixed and Random Effects Together

Slide 17: A guide to model selection with fixed and random effects (Zuur et al)

Slide 18: Rule of thumb

Slide 19: Example: Fixed and Random Effects Together (Length-weight regressions)

W = aLb; ln Wi,j = ln ai + bi ln Li,j + εi,j

Note that weight and length have been log transformed in the data to make this power function linear

Slide 20: Where does the variation in weight arise from?

Figure 20.1. Two scatterplots of Weight (y, about 2 to 9) versus Length (x, 0.7 to 3.0). Left: open circles; right: same points coded by individual fish. Weight rises steadily with length.

Slide 21: Fixed and Random Effects Together (Length-weight regressions)

Figure 21.1. Scatterplot of Weight versus Length for 10 fish with the fitted mixed-effects regression line; individual subjects share a common slope but have random intercepts around the mean line.

Slide 22: Fixed and Random Effects Together (Length-weight regressions)

Slide 23: Tricks and Traps

Slide 24: Mixed Effects Models

Ben Bolker’s FAQ on GLMMs can help us:

https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html

Slide 25: Another Mixed Model Example

Figure 25.1. Diagram of the sampling design: three fish species (drawn as fish) measured for Length and Trophic Position, nested within Lake 1 through Lake 6, ten individuals per species per lake.

Slide 26: How we should not analyze this dataset

Figure 26.1. Scatterplot titled 'Species 1 in Lake 1': Trophic Position (y, 3.4 to 4.0) versus Length in mm (x, 100 to 400). Ten points, weak positive trend, much scatter.
Figure 26.2. Scatterplot titled 'All Data': Trophic Position (y, 3.3 to 5.0) versus Length in mm (x, 100 to 400). Points rise with length but cluster vertically.

Slide 27: Data exploration

Figure 27.1. Faceted scatterplots 'By Species' (S1, S2, S3) of Trophic Position versus Length in mm. All three species show positive trends; S1 sits lower overall than S2 and S3.

Slide 28: Data exploration

Figure 28.1. Faceted scatterplots 'By Lake' (L1 to L6) of Trophic Position versus Length in mm. Each lake shows a positive length trend with broadly similar intercepts across lakes.

Slide 29: Let’s look for structure in residuals

Figure 29.1. Two boxplots of standardized residuals from the complete-pooling model. Left, by species: S1 residuals sit well below zero, S2 and S3 above. Right, by lake: residuals roughly centered on zero.

Lots of structure in residuals grouped by species, but not as bad for lake

Speaker notes for this slide

Consequences if you ignore it Biased standard errors and confidence intervals (typically underestimated). Inflated type I error for tests of significance. Wrong effect estimates if slope varies strongly between groups and you force a single slope. Poor predictive performance.

Slide 30: Ignore structure in residuals?

An lm() assumes independent, identically distributed residuals. If fish from the same species or lake are more similar, residuals will be correlated and lm() SEs and p-values will be wrong

Slide 31: Allow intercept to vary by species

lmer(Z_TP~Z_Length + (1|Fish_Species))

Figure 31.1. Schematic: three parallel Trophic-Position-versus-Length lines, species 1 (blue, low), 2 (red), 3 (green, high), around a black species-level line; a normal curve shows intercepts varying about the mean.

Slide 32: Allow intercept to vary by species

Figure 32.1. Schematic: three parallel Trophic-Position-versus-Length lines, species 1 (blue, low), 2 (red), 3 (green, high), around a black species-level line; a normal curve shows intercepts varying about the mean.

Slide 33: Should we use fixed or random effects for groups?

Slide 34: Allow intercept to vary by lake

Figure 34.1. Random-intercept-by-lake schematic: six parallel colored lines (Lake 1 lowest to Lake 6 highest) offset around the black lake-level mean line; a normal curve shows intercept spread.

Slide 35: But what if the effect of length on TP varies by species or lake? All our prior models were “random intercept” models. Now also “random slope”

lmer(Z_TP~Z_Length + (1+Z_Length|Fish_Species)+(1+Z_Length|Lake))

Figure 35.1. Random-slope schematic: three lines fanning from a shared intercept (species 3 blue shallow, 2 green, 1 red steepest) around the black mean line; inset normal curve labeled 'Distribution of slopes'.

Slide 36: Borrowing strength

Figure 36.1. Schematic of shrinkage: the fitted line for species 1 (blue, three data points) is pulled toward the black species-level mean line rather than fitting its own points, illustrating borrowing strength.

Slide 37: Which model to use for prediction?

Figure 37.1. R code screenshot listing candidate models M0 to M8 for Z_TP on Z_Length: an lm plus lmer random-intercept and random-slope combinations of Fish_Species and Lake.

Model 8 (AICc=36.3) and Model 2 (AICc=39.6) much lower than others

REML to compare random effects

Slide 38: Crossed vs Nested random effects

Figure 38.1. nter image description here
Figure 38.2. nter image description here

Slide 39: Labeling matters! Remove ambiguity with distinct labelling

Slide 40: Glmer Example Proportions are Rarely Proportions

Figure 40.1. Journal article opening: Warton and Hui (2011), Ecology, 'The arcsine is asinine: the analysis of proportions in ecology', abstract arguing for logistic regression over the arcsine transform.

Slide 41: White Stork Example

Figure 41.1. Photo of a white stork with red bill and legs standing on a rock over water, head lowered; overlaid caption reads 'This is not a GLMM'.

Oliver Gimenez’s Example

Slide 42

White stork: Is breeding success affected by weather conditions?

286

Slide 43

Standard GLM on proportions

287

Slide 44: What if we have substantial additional interannual variation not explained by weather?

Standard GLM on proportions in R

288

Slide 45

GLMM on proportions with year as a random effect

289

Slide 46

GLMM on proportions with year as a random effect in R

290

Slide 47

GLMM on proportions with year as a random effect in R

291

Slide 48

Figure 48.1. Caterpillar (dot-and-line) plot of the estimated yearly random intercepts from the mixed model, from the R output labelled '$year'. The horizontal axis is the intercept deviation from the overall mean, running from about -1.0 to +1.3; each row is one year (years 1 to about 22). A blue dot marks each year's estimated random intercept and a horizontal line shows its confidence interval. Years are sorted from the highest intercept at the top (year 4, near +0.8) down to the lowest at the bottom (year 2, near -1.0). Most intervals overlap zero, showing how much the baseline response varies from year to year around the population mean.

Slide 49: Example 2 – Nonlinear mixed model for length and age

Figure 49.1. von Bertalanffy growth curve: Total Length in mm (y) versus Age (x, 0 to 12). Points scatter around a curve rising then leveling at asymptote L-infinity (dashed line); t0 marked at length zero.

ℓi,j = ℓ∞,i(1 − exp(−κ(ai,j − t0))) + εi,j

Slide 50: Example – length and age

ℓi,j = ℓ∞,i(1 − exp(−κ(ai,j − t0))) + εi,j

Slide 51: Example – length and age

The response variable

The random effect

The fixed effects

The covariate

ℓi,j = ℓ∞,i(1 − exp(−κ(ai,j − t0))) + εi,j

Residual variation

Slide 52: Fitting Length and Age Data (Background)

Slide 53: Fitting Length and Age Data (The basic data)

Figure 53.1. Scatterplot of the length-and-age data (Total Length versus Age) for the growth model where asymptotic size L-infinity varies by individual; vector graphic could not be rendered for exact values.

Let L-inf (saturation) vary by individual

Slide 54: Fitting Length and Age Data (Standard non-linear model-I)

Figure 54.1. Residual diagnostic plot from the standard nonlinear least-squares von Bertalanffy fit, showing model misspecification from within-animal correlation rather than heteroscedasticity; vector graphic could not be rendered for exact values.

Slide 55: Fitting Length and Age Data (Standard non-linear model-II)

boxplot(split(residuals(lm1),AgeLen$Subject),ylab="Residual",

xlab="Subject",csi=0.2)

Figure 55.1. Boxplots of residuals by Subject from the standard nonlinear fit. Residual medians shift from about -8 to +8 across subjects, showing within-animal correlation the model missed.

Slide 56: The NLME Function-I

Slide 57: The NLME Function-II

Slide 58: Fitting Length and Age Data (Non-linear mixed model-I)

Figure 58.1. Diagnostic figure for the nonlinear mixed-effects fit (random Linf by subject; fixed Linf, Kappa, Tzero); vector graphic could not be rendered for exact values.

Slide 59: Fitting Length and Age Data (Non-linear mixed model-II)

plot(lm2,Subject ~ resid(.),abline=0)

Figure 59.1. Plot of residuals by Subject from the nonlinear mixed model (plot(lm2, Subject ~ resid(.))), residuals now centered near zero across subjects; vector graphic could not be rendered for exact values.

Slide 60: Fitting Length and Age Data (Non-linear mixed model-III)

Slide 61: Fitting Length and Age Data (Non-linear mixed model-IV)

Figure 61.1. Plot of subject-specific asymptotic size estimates (population Linf plus each subject's random effect) as points; vector graphic could not be rendered for exact values.
Figure 61.2. Faceted plot of standardized residuals versus fitted values by Subject for the nonlinear mixed model, with a zero reference line; vector graphic could not be rendered for exact values.

Slide 62: Fitting Length and Age Data (Non-linear mixed model-V)

plot(augPred(lm2),csi=0.3,pch=16,lwd=2)

Figure 62.1. Faceted plot from augPred: observed length-at-age data with the fitted nonlinear mixed-model growth curves overlaid per subject; vector graphic could not be rendered for exact values.