20: Linear and Generalised Linear Mixed Models
Task N
Practice Task
Work through these exercises after reading the Mixed Models chapter, using the vegan pyrifos dataset (data(pyrifos)) — aquatic invertebrate communities from 12 mesocosm ditches sampled repeatedly over 11 weeks following insecticide (chlorpyrifos) dosing. Four exercises are hands-on calculations and two are short conceptual questions.
Reconstruct the design factors (
ditch,weekanddose; see?pyrifos) and build a univariate response, for example total abundance (rowSums(pyrifos)) or richness (vegan::specnumber(pyrifos)). Fit a naive model that ignores ditch (anlm(), or aglm()for a count response, of the response ondose), and inspect the residuals for the repeated-measures structure.Fit a mixed model with
lme4::lmer()(orglmer()for a count response) that adds(1 | ditch)to account for the repeated sampling of each ditch. Compare the fixed-effect estimates and their standard errors with those of the naive model.Compute the intraclass correlation (ICC) for ditch with
performance::icc()(or by hand from the variance components). How much of the variation is between ditches?Add
weekto the model and test whether adose-by-weekinteraction, or a random slope for week within ditch, is supported. Watch for and report any singular fits.Explain pseudoreplication in this design: why are repeated samples from the same ditch not independent, and what does the random effect fix?
Explain what the ICC represents, and how adding the random-effect structure changes the standard errors and the inferences you can draw relative to the naive model.
Assessment Criteria
This Task is not formally assessed. It is built around four hands-on analyses (Exercises 1–4) and two short conceptual questions (Exercises 5–6); work through all six and bring your annotated Quarto document to class for discussion.
Reuse
Citation
@online{smit2026,
author = {Smit, A. J.},
title = {20: {Linear} and {Generalised} {Linear} {Mixed} {Models}},
date = {2026-06-13},
url = {https://tangledbank.netlify.app/BCB743/tasks/Task_N.html},
langid = {en}
}
