12  Mixed-Effects Models

Week 12: Accounting for sites, repeated measurements and other sampling structure with random effects

So far, every model has assumed that each observation is independent. In real ecological studies (including your project) that is rarely true: we measure the same animal more than once, take several quadrats from each site, or put several seedlings in each pot.

This week you will learn mixed-effects models, which add random effects to a linear model to account for this structure. Workshop materials are available in the GitHub repository ECS200.

TipLearning outcomes

By the end of this workshop you should be able to:

  1. Recognise non-independent (grouped or repeated) data and explain why it matters.
  2. Explain the difference between fixed and random effects.
  3. Fit and check a random-intercept model with lmerTest::lmer().
  4. Interpret the fixed effects and the variance components, and write up the results.

Before we start

lizard_repeated.csv pie_crab.csv

We need two new packages. lme4 fits mixed models, and lmerTest is a small add-on that gives lme4 models p-values.

Code
# Week 12 workshop - Mixed-effects models
# Written by Nicholas Wu, 1234578, 13/05/2027

# install.packages("lme4")      # run once in the console if needed
# install.packages("lmerTest")

library(tidyverse)   # data import, wrangling and plotting
library(lme4)        # mixed-effects models
library(lmerTest)    # p-values for mixed-effects models
library(performance) # checking model assumptions
library(see)         # draws the plots for performance
library(ggeffects)   # predictions from models for plotting

# setwd("C:/Users/YOUR-NAME/Documents/ECS200")

Study context

In Week 5 every lizard was raced only once. That needs a lot of lizards. A more efficient design is to race the same lizard at several temperatures. Here, 16 dragons were each raced at 18, 22, 26 and 30 °C (in random order, with a rest day between trials), giving 64 sprint speeds.

Question: How does sprint speed change with temperature?

Code
lizard_rm <- readr::read_csv("data/lizard_repeated.csv")

dplyr::glimpse(lizard_rm)
Rows: 64
Columns: 4
$ lizard_id    <chr> "L01", "L01", "L01", "L01", "L02", "L02", "L02", "L02", "…
$ mass_g       <dbl> 10.8, 10.8, 10.8, 10.8, 13.7, 13.7, 13.7, 13.7, 12.5, 12.…
$ temperature  <dbl> 18, 22, 26, 30, 18, 22, 26, 30, 18, 22, 26, 30, 18, 22, 2…
$ sprint_speed <dbl> 1.10, 1.10, 1.36, 1.71, 1.64, 1.84, 1.95, 2.32, 1.35, 1.4…
Code
# Each lizard appears 4 times, once per temperature
lizard_rm %>%
  dplyr::count(lizard_id)
# A tibble: 16 × 2
   lizard_id     n
   <chr>     <int>
 1 L01           4
 2 L02           4
 3 L03           4
 4 L04           4
 5 L05           4
 6 L06           4
 7 L07           4
 8 L08           4
 9 L09           4
10 L10           4
11 L11           4
12 L12           4
13 L13           4
14 L14           4
15 L15           4
16 L16           4

1. Background

Why independence matters

Some lizards are simply faster than others, at every temperature, perhaps because they are bigger or in better condition. The four measurements from the same lizard are therefore more similar to each other than to measurements from other lizards. They are not independent.

If we ignore this and fit lm(sprint_speed ~ temperature), R treats the data as 64 independent lizards. Treating repeated or grouped measurements as independent replicates is called pseudoreplication (Hurlbert, 1984). It is one of the most common mistakes in ecology, and it usually makes p-values too small.

Grouping structure appears everywhere:

Design Grouping (random effect)
The same individual measured repeatedly Individual ID
Several quadrats or traps within each site Site
Several plants in each pot, tank or plot Pot / tank / plot
Surveys repeated over several years Year
Offspring from the same parents Family

Another example is spatial autocorrelation: samples taken close together tend to give similar results, for example because they share similar soil (a problem you will meet on The Islands). Randomisation matters!

Neighbouring sites tend to be similar (positive spatial autocorrelation), which is another reason observations are not independent.

Fixed and random effects

A mixed-effects model contains both:

  • Fixed effects are the predictors your question is about (here, temperature). You estimate and report their coefficients, exactly as in Weeks 5–11.
  • Random effects describe the grouping structure (here, lizard ID). You are not interested in the difference between lizard L03 and lizard L11. You want to account for the fact that lizards differ.

The simplest random effect is a random intercept: each lizard gets its own baseline speed, and those baselines are assumed to come from a normal distribution with a standard deviation that the model estimates:

\[ \text{speed}_{ij} = \beta_0 + \beta_1\,\text{temperature}_{ij} + u_j + \varepsilon_{ij}, \qquad u_j \sim N(0, \sigma^2_{\text{lizard}}), \quad \varepsilon_{ij} \sim N(0, \sigma^2) \]

The model splits the unexplained variation into two parts: between lizards (\(\sigma^2_{\text{lizard}}\)) and within lizards (\(\sigma^2\), the residual).

In R the random intercept is written (1 | lizard_id), which reads “a separate intercept for each lizard”:

lmerTest::lmer(sprint_speed ~ temperature + (1 | lizard_id), data = lizard_rm)

Use the app to compare a model that ignores lizard identity with one that has a random intercept for each lizard.

App: random intercepts

Because every lizard was raced at every temperature, the random intercept removes the lizard-to-lizard differences from the noise, so the temperature effect is estimated more precisely (a smaller standard error). This is the same idea as a paired t-test. When the predictor only varies between groups (e.g. one latitude per marsh), it works the other way: ignoring the grouping makes you too confident. You will see that with the crab data at the end of this workshop.

How many groups do I need?

The model estimates the variation among groups from the groups you sampled, so it needs enough of them. A common guideline is at least 5 levels, and preferably more, for a random effect (Harrison et al., 2018). With only 2–3 sites, include site as a fixed effect instead.

2. Visualise

Plot each lizard as its own line (“spaghetti plot”). This shows the grouping structure immediately.

Code
ggplot2::ggplot(lizard_rm, ggplot2::aes(x = temperature, y = sprint_speed, group = lizard_id)) +
  ggplot2::geom_line(alpha = 0.5) +   # one line per lizard
  ggplot2::geom_point(alpha = 0.7) +
  ggplot2::labs(x = "Test temperature (°C)", y = "Sprint speed (m/s)") +
  ggplot2::theme_classic()

Every lizard gets faster as temperature increases, the lines are roughly parallel (similar slopes), and some lizards are consistently faster than others (different heights). That is exactly the pattern a random intercept describes.

3. Check assumptions

A linear mixed model has the same assumptions as a linear model (linearity, equal variance and normal residuals) plus one more: the random effects are normally distributed. The independence assumption now only applies after accounting for the grouping.

Code
# Random intercept model
lizard_lmm <- lmerTest::lmer(sprint_speed ~ temperature + (1 | lizard_id),
                             data = lizard_rm)

performance::check_model(lizard_lmm)

Assumption Panel Here
Linearity Linearity ✅ Flat reference line
Equal variance Homogeneity of variance ✅
Normal residuals Normality of residuals ✅
Normal random effects Normality of random effects (lizard_id) ✅ Dots close to the line
Independence, given the grouping Study design ✅ Lizards are different animals, tested in random order
Situation What to do
Groups differ in their slope as well as their intercept (the spaghetti lines are not parallel) Add a random slope: (1 + temperature \| lizard_id). This needs plenty of data per group
Groups within groups, e.g. quadrats within sites within regions Nested random effects: (1 \| region/site)
Two separate groupings, e.g. site and year Crossed random effects: (1 \| site) + (1 \| year)
Counts or binary responses with grouping Generalised linear mixed model: lme4::glmer(count ~ x + (1 \| site), family = poisson)
Fewer than about 5 groups Fit the grouping as a fixed effect, e.g. lm(y ~ x + site)
Message boundary (singular) fit The random effect variance is estimated as zero, often because there are too few groups or little variation among them. Check the data. The model may not need that random effect
Measurements close in time are more similar than distant ones Temporal autocorrelation (see the optional Week 6 on time series)

4. Model

Code
summary(lizard_lmm)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: sprint_speed ~ temperature + (1 | lizard_id)
   Data: lizard_rm

REML criterion at convergence: -51.7

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-1.50424 -0.64597 -0.05299  0.52411  2.32986 

Random effects:
 Groups    Name        Variance Std.Dev.
 lizard_id (Intercept) 0.095692 0.30934 
 Residual              0.008365 0.09146 
Number of obs: 64, groups:  lizard_id, 16

Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
(Intercept)  0.432250   0.099376 34.937588    4.35 0.000113 ***
temperature  0.044281   0.002556 47.000000   17.32  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr)
temperature -0.617

The output gives everything you need:

  • Linear mixed model fit by REML: REML (restricted maximum likelihood) is the default fitting method. t-tests use Satterthwaite's method means lmerTest calculated the degrees of freedom and p-values.
  • Scaled residuals: a quick summary of the residuals. The median should be near 0 and the minimum and maximum roughly symmetrical.
  • Random effects: how the unexplained variation is split.
    • lizard_id (Intercept): the Variance and Std.Dev. of the baseline speeds between lizards. A large value means lizards differ a lot.
    • Residual: the variation within a lizard, left over after temperature and lizard identity.
  • Number of obs: 64, groups: lizard_id, 16: check these match your design (16 lizards × 4 temperatures).
  • Fixed effects: interpreted exactly like lm().
    • Estimate: (Intercept) is the predicted speed of the average lizard at 0 °C (outside the data, so not meaningful on its own). temperature is the change in sprint speed per °C for the average lizard.
    • Std. Error: the uncertainty in each estimate, now accounting for the repeated measures.
    • df: degrees of freedom from Satterthwaite’s method (usually a decimal).
    • t value and Pr(>|t|): the test statistic and p-value.
  • Correlation of Fixed Effects: how strongly the estimates are related to each other. You can usually ignore it.
Code
# 95% confidence intervals for the fixed effects (Wald method, fast and fine here)
confint(lizard_lmm, parm = "beta_", method = "Wald")
                 2.5 %     97.5 %
(Intercept) 0.23747698 0.62702302
temperature 0.03927091 0.04929159
Code
# Intraclass correlation (ICC): proportion of the unexplained variance due to lizards
performance::icc(lizard_lmm)
# Intraclass Correlation Coefficient

    Adjusted ICC: 0.920
  Unadjusted ICC: 0.665
Code
# R-squared: marginal = fixed effects only; conditional = fixed + random effects
performance::r2(lizard_lmm)
# R2 for Mixed Models

  Conditional R2: 0.942
     Marginal R2: 0.277

Compare this with the model that ignores lizard identity:

Code
lizard_lm <- lm(sprint_speed ~ temperature, data = lizard_rm)

# Same slope, different standard error
summary(lizard_lm)$coefficients["temperature", ]
    Estimate   Std. Error      t value     Pr(>|t|) 
0.0442812500 0.0088815988 4.9857296120 0.0000052628 
Code
summary(lizard_lmm)$coefficients["temperature", ]
    Estimate   Std. Error           df      t value     Pr(>|t|) 
4.428125e-02 2.556342e-03 4.700000e+01 1.732211e+01 4.883421e-22 

Plot the predictions for the average lizard, with the raw data.

Code
lizard_pred <- ggeffects::predict_response(lizard_lmm, terms = "temperature")

plot(lizard_pred, show_data = TRUE) +
  ggplot2::labs(title = NULL, x = "Test temperature (°C)", y = "Sprint speed (m/s)")

5. Interpret

  • Temperature: sprint speed increased by 0.044 m/s per °C (95% CI 0.039–0.049), i.e. about 0.53 m/s across the 12 °C range tested.
  • Random effects: lizards differed in their baseline speed with an SD of 0.31 m/s, while the residual SD within a lizard was 0.09 m/s. The ICC of 0.92 means 92% of the unexplained variation was due to consistent differences between individual lizards.
  • R2: temperature alone explains 0.28 of the variation (marginal R2), and temperature plus lizard identity explain 0.94 (conditional R2).

Methods sentence: Because each lizard was tested at all four temperatures, the effect of temperature on sprint speed was analysed with a linear mixed-effects model with temperature as a fixed effect and lizard identity as a random intercept, fitted with lme4 and lmerTest in R (p-values from Satterthwaite’s degrees of freedom). Significance was set at α = 0.05.

Results sentence:

Sprint speed increased with temperature by 0.044 m/s per °C (95% CI 0.039–0.049; t47 = 17.3, p < 0.001; Fig. 1). Individual lizards differed consistently in speed (SD among lizards = 0.31 m/s; ICC = 0.92).

Revisiting Week 7: crabs in marshes

Atlantic marsh fiddler crab (Minuca pugnax).

In Week 7 we regressed fiddler crab size on latitude using 392 crabs, but they came from only 13 marshes, and every crab in a marsh has the same latitude. The predictor varies only between marshes, so the real replication for the latitude effect is closer to 13 than to 392. A random intercept for marsh (site) fixes this.

Code
crab_data <- readr::read_csv("data/pie_crab.csv")

# Week 7 model: treats 392 crabs as independent
crab_lm  <- lm(size ~ latitude, data = crab_data)

# Mixed model: crabs are grouped within marshes
crab_lmm <- lmerTest::lmer(size ~ latitude + (1 | site), data = crab_data)

# Compare the latitude estimate and its standard error
summary(crab_lm)$coefficients["latitude", ]
    Estimate   Std. Error      t value     Pr(>|t|) 
4.851191e-01 3.359292e-02 1.444111e+01 3.602897e-38 
Code
summary(crab_lmm)$coefficients["latitude", ]
    Estimate   Std. Error           df      t value     Pr(>|t|) 
4.888768e-01 7.964087e-02 1.105760e+01 6.138517e+00 7.163163e-05 

The slope barely changes, but its standard error increases from 0.034 to 0.08 and the degrees of freedom drop from 390 to about 11, which is close to the number of marshes. The evidence for Bergmann’s rule is still strong (p < 0.001), but the mixed model gives an honest measure of the uncertainty. With a weaker effect, the simple lm() could easily have produced a “significant” result that the data did not support.

Assessment

Task to complete before the end of the workshop.

week12_assessment.R seagrass_meadows.csv

Picasso triggerfish swimming over seagrass meadow at Green Island. Photo: Dieter Tracey 2020.

Seagrass shoot length and water depth. Seagrasses need light, and light decreases with depth. To test whether seagrass shoots are shorter in deeper water, divers sampled 8 seagrass meadows. In each meadow they placed 12 quadrats at different depths and measured the mean shoot length (cm) in each quadrat.

  1. Import the data and check its structure. Which variable is the grouping (random) effect, and why should it be random rather than ignored?
  2. Plot shoot length against depth, with a different colour (or line) for each meadow. Describe the pattern within meadows and among meadows.
  3. Fit a linear model that ignores meadow (lm()), and a mixed model with a random intercept for meadow (lmerTest::lmer()).
  4. Check the assumptions of the mixed model with performance::check_model().
  5. Report the depth effect (estimate, 95% CI, p-value) from the mixed model. How do the estimate and its standard error differ from the lm()?
  6. Report the among-meadow SD, the residual SD and the ICC. What do they tell you?
  7. Plot the predicted shoot length against depth with ggeffects::predict_response().
  8. Write a results sentence.
  9. For next year’s survey, the team can afford about 100 quadrats. Would you recommend 5 meadows × 20 quadrats or 20 meadows × 5 quadrats if the aim is to make conclusions about seagrass meadows in general? Explain in one or two sentences.

Submit one R script (.R) before the end of the class. Write your answers to written questions as # comments next to the code that produced them.

Score What it looks like
0 No submission or no attempt.
0.25 Submitted, but a very poor attempt (e.g. incomplete, or lots of gaps).
0.5 Most questions answered, or all answered but most annotations missing.
0.75 All questions answered, but some annotations missing or some answers incorrect.
1 Everything completed, with annotations on all code so the marker can follow it.

Tip: an annotation is a short # comment that says what the line does and why. For example: # remove the one plant with a stem dry mass of 7.4 g (data entry error).

Additional resources

  • Coding Club: Introduction to linear mixed models, a friendly, ecology-based tutorial (dragons included).
  • Harrison et al. (2018) A brief introduction to mixed effects modelling and multi-model inference in ecology: the go-to practical guide (focus on sections 1–4).
  • Hurlbert (1984) Pseudoreplication and the design of ecological field experiments: the classic paper that defined pseudoreplication.
  • Pedersen et al. (2019) Hierarchical generalized additive models in ecology: what to do when relationships are wiggly as well as grouped (advanced).
  • Mixed models with R (M. Clark): a clear next step if you want to learn random slopes and GLMMs.