Skip to main content

Homework 5 — Generalized Linear Models

Covers GLM and Logistic Regression

Social spider colonies: a linear model, a Poisson GLM and a negative binomial GLM fitted to the same counts, and three different answers. Work out which one you were entitled to.
Author

Bill Perry

📥 Social spider colony data 📥 Answer sheet (.docx)

Note📚 Where these data come from

Book source: Whitlock, M.C. & Schluter, D., The Analysis of Biological Data, Ch. 17, practice problem 15.

In class you analysed the Galápagos islands, a count where the Poisson failed and the negative binomial fixed it, and the New England ants, a count where the Poisson was already right. This is a third count, and it behaves differently from both.

The data

17 communal spider webs, one row per colony.

Column What it is Role
colony colony ID (1–17) label
height_cm height of the web above the ground (cm) predictor
n_spiders number of spiders in the colony response (a count)

The question: does colony size change with height above the ground?

Warning⚠️ Read this before you start

These data give three different answers depending on the model, and one of them is “there is no relationship.” Your job is to work out which model you were entitled to. Do not skip Part 2: the wrong model is part of the assignment.

How this homework works

The code is from the GLM and Logistic Regression skeleton. Reuse it.

  • Start a new script, scripts/hw5_spiders.R, and put the data in data/.
  • Work through the Parts in order.
  • Answer the ten ✏️ questions on the answer sheet. Q9 is your final figure.
library(MASS)          # glm.nb()
library(car)           # Anova()
library(performance)   # check_overdispersion()
library(DHARMa)        # simulateResiduals()
library(tidyverse)     # load last so dplyr::select() wins over MASS::select()

Part 1 · Look at the response

Read data/social_spider_colonies.csv into spider_df. Get the mean and variance of n_spiders, and their ratio. Make a histogram of n_spiders and a scatterplot of n_spiders against height_cm.

✏️ Q1. Report the mean, the variance and their ratio. For a Poisson count the ratio should be about what? Describe the shape of the histogram.


Part 2 · The wrong model, on purpose

Fit lm(n_spiders ~ height_cm). Look at summary(), the four diagnostic plots, and shapiro.test() on the residuals.

✏️ Q2. Report the slope’s p. What would you conclude from this model alone? Do the residuals let you trust it?


Part 3 · The Poisson GLM

Fit glm(n_spiders ~ height_cm, family = poisson). Look at summary() and run check_overdispersion().

✏️ Q3. Report the slope, its SE and its p. Report the dispersion ratio. What should it be, and what does this value mean for the p-value you just reported?


Part 4 · The negative binomial GLM

Fit glm.nb(n_spiders ~ height_cm). Look at summary(), run check_overdispersion(), and check it with DHARMa: plot(simulateResiduals(model)).

✏️ Q4. Report the slope, its SE, its p and θ. Is the overdispersion gone? Does DHARMa find a problem?

✏️ Q5. Fill in the table. The slope barely moves between the Poisson and the negative binomial. What does change, and by how much?

                   slope      SE        p
lm
Poisson
neg. binomial

✏️ Q6. Compare the Poisson and negative binomial models with AIC(). Which model are you entitled to report, and what two pieces of evidence justify it?


Part 5 · The influential colony

Get Cook’s distance for the negative binomial model with cooks.distance(), and find the colony with the largest value. Refit the model without that colony and compare the slope and p.

✏️ Q7. Which colony is it, and what is unusual about it? Does dropping it make the result weaker or stronger? Which result do you report, and what do you say about the other?


Part 6 · Read the slope on the count scale

The slope is on the log scale. exp(slope) is the multiplicative change in colony size per 1 cm, and exp(10 * slope) is the change per 10 cm.

✏️ Q8. By what percent does colony size change for every 10 cm of height? Why do you have to exp() a GLM slope and not an lm() slope?


Part 7 · The final figure

One publication-quality figure: the raw counts against height, with the negative binomial fitted curve and its 95% confidence band. Use predict(model, newdata, type = "link", se.fit = TRUE), build the band on the link scale, then exp() it. 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: which model and why (dispersion), the effect as a percent per 10 cm with p, and one sentence on the sensitivity check from Part 5.


What to turn in

  1. The answer sheet (Word) with Q1–Q10 answered and your figure pasted in at Q9.
  2. Your script, hw5_spiders.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.