Mixed effect Models

Bio300B Lecture 10

Richard J. Telford (Richard.Telford@uib.no)

Institutt for biovitenskap, UiB

17 August 2025

Assumptions of Least Squares

Error in library(faux): there is no package called 'faux'
  1. Linear relationship between response and predictors.
  2. The residuals have a mean of zero.
  3. The residuals have constant variance (not heteroscedastic).
  4. The residuals are independent (uncorrelated).
  5. The residuals are normally distributed.
  • Spatial/temporal/phylogenetic autocorrelation
  • Clustered/hierarchical data

Clustered data

Observations not independent

  • clustered data
  • repeat measurements

Ignoring it causes pseudoreplication

Two tanks - bad design

Error in add_ranef(add_ranef(add_recode(add_between(add_random(subj = subj_n, : could not find function "add_ranef"
Error in `ggplot()`:
! `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`

Two tanks - bad models

mod_bad <- lm(Mass_g ~ condition, data = demo)
Error in model.frame.default(formula = Mass_g ~ condition, data = demo, : 'data' must be a data.frame, environment, or list
mod_bad2 <- lm(Mass_g ~ tank, data = demo)
Error in model.frame.default(formula = Mass_g ~ tank, data = demo, drop.unused.levels = TRUE): 'data' must be a data.frame, environment, or list
mod_bad
Error: object 'mod_bad' not found
mod_bad2
Error: object 'mod_bad2' not found

Better design - replicate tanks

Error in add_ranef(add_ranef(add_recode(add_between(add_random(subj = subj_n, : could not find function "add_ranef"
Error: object 'demo2' not found
Error: object 'outer_plot' not found

Better design, bit better model

mod_bit <- lm(Mass_g ~ condition + tank, data = demo2)
Error in eval(mf, parent.frame()): object 'demo2' not found
summary(mod_bit)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mod_bit' not found

Fixed effect and random effects

Fixed effect effect model

\[y_i = \color{red}{\beta_0} + \color{red}{\beta_1}x_i + \color{red}{\beta_2}tank_i +\color{blue}{ \epsilon_i}\]

Not interested in effect of tank

Mixed effect model

Use a Random effect

Assumes observed tanks from a population of possible tanks

\[y_{ij} = \color{red}{\beta_0} +\color{blue}{b_{0i}}+ \color{red}{\beta_1}x_i +\color{blue}{ \epsilon_{ij}}\]

Random effects

Residuals from a normal distribution \(\color{blue}{ \epsilon_{ij}} \sim N(0, \sigma_{ind})\)
Random effects from a normal distribution \(\color{blue}{b_{0i}} \sim N(0, \sigma_{clu})\)

Error: object 'demo2' not found
Error: object 'p1' not found

Fitting a mixed effect model

library(lme4)
mod1 <- lmer(Mass_g ~ condition + (1|tank), data = demo2)
Error: bad 'data': object 'demo2' not found
summary(mod1)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mod1' not found
library(lmerTest)
mod1a <- lmer(Mass_g ~ condition + (1|tank), data = demo2)
Error: bad 'data': object 'demo2' not found
summary(mod1a)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mod1a' not found

Alternative package

library(nlme)
mod2 <- lme(Mass_g ~ condition, data = demo2, random =  ~ 1|tank)
Error: object 'demo2' not found
summary(mod2)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mod2' not found

Fixed or random effects

Fixed effects factors:

  • Informative factor levels with regard to the hypothesis
  • Effect of each level interesting
  • The levels are deliberately chosen by experimenter
  • Increasing sample size doesn’t increase number of levels

Random effects factors:

  • Uninformative factor levels with regard to the hypothesis
  • Effect of each level uninteresting
  • Increasing sample size often increases number of levels
  • Part of a population of possible levels

“one modeler’s random effect is another modeler’s fixed effect.”

“Are there enough levels of the factor in the data on which to base an estimate of the variance of the population of effects? No, means fixed effects.”

Fixed or random effects?

  • snake id number
  • vaccinated or not
  • species
  • light level
  • litter of puppies
  • site

Outer and inner variables

Error in add_ranef(add_ranef(add_recode(add_within(add_random(subj = subj_n/2, : could not find function "add_ranef"
Error: object 'outer_plot' not found
Error: object 'outer_plot' not found
  • which design is more powerful?
  • which design is practical
mod3 <- lmer(Mass_g ~ condition + (1|tank), data = demo3)
Error: bad 'data': object 'demo3' not found
summary(mod3)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mod3' not found

Continuous inner predictor

data("sleepstudy", package = "lme4")

Random intercept

fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy, subset = Days>=2)

summary(fm1)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: Reaction ~ Days + (1 | Subject)
   Data: sleepstudy
 Subset: Days >= 2

REML criterion at convergence: 1430

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.6261 -0.4450  0.0474  0.5199  4.1378 

Random effects:
 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 1746.9   41.80   
 Residual              913.1   30.22   
Number of obs: 144, groups:  Subject, 18

Fixed effects:
            Estimate Std. Error      df t value Pr(>|t|)    
(Intercept)  245.097     11.829  30.617   20.72   <2e-16 ***
Days          11.435      1.099 125.000   10.40   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
     (Intr)
Days -0.511
library(broom.mixed)
Error in library(broom.mixed): there is no package called 'broom.mixed'
augment(fm1) |> 
  ggplot(aes(x = Days, y = Reaction, colour = Subject)) +
  geom_point() +
  geom_line(aes(y = .fitted)) +
  geom_line(aes(y = .fixed), colour = "black", size = 1.5) +
  theme(legend.position = "none")
Error in `augment()`:
! No `augment()` method for objects of class <lmerModLmerTest>.

Random slope

fm2 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy, subset = Days>=2)

summary(fm2)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: Reaction ~ Days + (Days | Subject)
   Data: sleepstudy
 Subset: Days >= 2

REML criterion at convergence: 1404.1

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-4.0157 -0.3541  0.0069  0.4681  5.0732 

Random effects:
 Groups   Name        Variance Std.Dev. Corr 
 Subject  (Intercept) 992.69   31.507        
          Days         45.77    6.766   -0.25
 Residual             651.59   25.526        
Number of obs: 144, groups:  Subject, 18

Fixed effects:
            Estimate Std. Error      df t value Pr(>|t|)    
(Intercept)  245.097      9.260  16.999  26.468 2.95e-15 ***
Days          11.435      1.845  17.001   6.197 9.74e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
     (Intr)
Days -0.454
Error in `augment()`:
! No `augment()` method for objects of class <lmerModLmerTest>.

Comparing models

anova(fm1, fm2)
Data: sleepstudy
Subset: Days >= 2
Models:
fm1: Reaction ~ Days + (1 | Subject)
fm2: Reaction ~ Days + (Days | Subject)
    npar    AIC    BIC  logLik -2*log(L)  Chisq Df Pr(>Chisq)    
fm1    4 1446.5 1458.4 -719.25    1438.5                         
fm2    6 1425.2 1443.0 -706.58    1413.2 25.332  2  3.156e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Model diagnostics

See ?plot.merMod

performance::check_model()

DHARMa package

performance package

performance::check_model(fm1)
Error: Package `see` required for model diagnostic plots.
  Please install it by running `install.packages("see")`.

Crossed random effects

Two random effects

Eggs from birds nests (first random effect - lay_nest) moved to other nests (second random effect - hatch_nest)

lmer(mass ~ treatment + (1|lay_nest) + (1|hatch_nest), data = data)

Nested random effects

Hierarchical random effects

  • Plot nested in site
  • sample nested in fish nested in tank
  • species nested in family
lmer(mass ~ treatment + (1|site/plot), data = data)

Convergence & other problems

Sometimes mixed effect models report errors.

Take them seriously.

May need to simplify the model

Having predictors on same scale can help

Beyond linear mixed effect models

Generalised linear mixed effect models

  • poisson
  • binomial

Fit with glmer()

Autocorrelated data

  • repeat measurements
  • fit with nlme::lme()

glmmTMB package

  • mixed effect models with wide range of distribution families
    • negative binomial
    • betabinomial
    • tweedie
  • zero inflation models
  • autocorrelation

Bayesian hierarchial models

Mixed effect models can be hard to fit

  • convergence problems

Bayesian model can help

Different statistical philosophy

  • rstan
  • nimble

Use prior information (or uninformative priors)

Resources

Bolker B (2021) GLMM FAQ

Harrison et al (2018) A brief introduction to mixed effects modelling and multi-model inference in ecology