library(janitor)
library(car) # Anova()
library(lme4)
library(lmerTest) # F-tests with Kenward-Roger df
library(performance) # icc(), r2()
library(emmeans)
library(tidyverse)
options(contrasts = c("contr.sum", "contr.poly"))Homework 6 — Linear Mixed Models
Covers Linear Mixed Models
📥 Crayfish growth data 📥 Answer sheet (.docx)
Original study: Sargent, L.W. & Lodge, D.M. (2014). Evolution of invasive traits in nonindigenous species: increased survival and faster growth in invasive populations of rusty crayfish (Orconectes rusticus). Evolutionary Applications 7: 949–961.
A common-garden experiment: young-of-year crayfish from native (Ohio) and invasive (Wisconsin) populations, grown in enclosures in three northern Wisconsin lakes in summer 2011. Not a textbook dataset.
You have seen these data before. In the Two-Way ANOVA Redux, lake was a fixed factor. Here it becomes a random effect, and the question changes from “which lakes differ?” to “does the invasive advantage hold across lakes in general?”
The data
84 crayfish, one row per crayfish.
| Column | What it is | Role |
|---|---|---|
growth_per_day |
growth rate (mm/day) | response |
range |
Native or Invasive |
fixed effect |
lake |
Big, High, Papoose |
random effect |
initial_length, final_length |
carapace length at start and end (mm) | |
days |
days in the enclosure | |
avg_temp |
mean water temperature (°C) |
How this homework works
The code is from the Linear Mixed Models skeleton. Reuse it.
- Start a new script,
scripts/hw6_crayfish.R, and put the data indata/. - Work through the Parts in order.
- Answer the ten ✏️ questions on the answer sheet. Q9 is your final figure.
Part 1 · The design
Read data/sargent_lodge_crayfish.csv into cray_df and make range (Native first) and lake factors. Count the crayfish per range and lake, and get the range of days for each range.
✏️ Q1. How many crayfish are in each range × lake cell? How many lakes are there, and what is the lecture’s rule of thumb for the fewest levels a random effect should have? What do you notice about days for native vs invasive crayfish, and why does that matter?
Part 2 · Look first
Plot growth_per_day by range, one panel per lake (facet_wrap(~ lake)), as boxplots with the points on top.
✏️ Q2. Do invasive crayfish grow faster in every lake? Is the advantage about the same size in all three?
Part 3 · Lake as a fixed factor
Fit lm(growth_per_day ~ range * lake) and run Anova(type = 3). Then fit the additive lm(growth_per_day ~ range + lake) and run Anova().
✏️ Q3. Report F, df and p for range:lake from the interaction model. Then report F, df and p for range from the additive model. What does the interaction tell you about Q2?
Part 4 · Lake as a random intercept
Fit lmer(growth_per_day ~ range + (1 | lake)). Run anova(model, ddf = "Kenward-Roger"), summary(), icc() and r2(). Check the residuals with plot(model) and a Q–Q plot of residuals().
✏️ Q4. Report F, df and p for range. Compare them with the additive fixed model in Part 3. What did making lake random change, and what did it not change?
✏️ Q5. Report the lake variance, the residual variance and the ICC. Say in one sentence what the ICC means for two crayfish from the same lake. How much do you trust a variance estimated from three lakes?
Part 5 · Let the advantage vary by lake
A random intercept alone draws parallel lines: it assumes the invasive advantage is identical in every lake. Fit lmer(growth_per_day ~ range + (1 | lake) + (1 | lake:range)), which lets it vary. Run anova(model, ddf = "Kenward-Roger") and summary().
✏️ Q6. Fill in the table:
model range F df p
lm(range + lake)
lmer (1 | lake)
lmer (1 | lake) + (1 | lake:range)
✏️ Q7. Why do the denominator df fall from about 80 to about 2? What is the replicate for the range effect in the last model?
✏️ Q8. Which model answers the question “do invasive crayfish grow faster in lakes generally?” What is the honest answer, and what would you need to make that claim with confidence?
Part 6 · The final figure
One publication-quality figure showing both levels: the raw growth rates by range, colored by lake, with the model means and 95% CIs from emmeans() on the Part 5 model drawn on top. Add axis labels with units and a theme. Save it with ggsave().
✏️ Q9. Paste your final figure into the answer sheet.
✏️ Q10. Write a 3–4 sentence results paragraph: the model you report and why, the range test (F, df, p), the invasive − native difference with its SE, and one sentence on the days confound.
What to turn in
- The answer sheet (Word) with Q1–Q10 answered and your figure pasted in at Q9.
- Your script,
hw6_crayfish.R. It must run top to bottom. It is not graded line by line, but an answer your script can’t reproduce earns no credit.
Points: 30, 3 per question. 3 great: correct and fully explained. 2 good: the right idea with a piece missing or a small error. 1 ok: attempted, but mostly wrong or unexplained. 0: missing.