library(tidyverse)
library(janitor) # clean_names()
library(car) # Anova()
library(MASS) # glm.nb()
library(performance) # check_overdispersion(), r2()
g_df <- read_csv("data/galapagos_species_area.csv")
polis_df <- read_csv("data/polis.csv", na = c(".", "")) %>%
clean_names() %>%
drop_na(paratio, pa)Generalized Linear Models
Poisson, negative binomial, and logistic regression
📥 Download the R script 📊 galapagos_species_area.csv 📊 polis.csv
What a GLM does
A linear model assumes a normal response with constant variance. Counts and yes/no data break both assumptions. A generalized linear model keeps the linear predictor but adds:
- a family (error distribution) that matches the data, and
- a link function that connects the mean to the linear predictor.
| Response | Family | Link | R call |
|---|---|---|---|
| Continuous | gaussian |
identity | glm(..., family = gaussian) = lm() |
| Counts | poisson |
log | glm(..., family = poisson) |
| Overdispersed counts | negative binomial | log | MASS::glm.nb() |
| 0/1, presence/absence | binomial |
logit | glm(..., family = binomial) |
GLMs are tested with likelihood-ratio (LR) χ² tests instead of F-tests, and fit is compared with AIC (lower is better).
Step 1: Load packages and data
MASS has its own select(). If you load it after the tidyverse, write dplyr::select().
Part A: Counts — plant species on Galapagos islands
Step 2: Look at the counts
For a Poisson variable the variance equals the mean. A ratio far above 1 is overdispersion.
g_df %>%
summarise(mean_spp = mean(n_species),
var_spp = var(n_species),
ratio = var_spp / mean_spp)# A tibble: 1 × 3
mean_spp var_spp ratio
<dbl> <dbl> <dbl>
1 119. 12246. 103.
g_df %>%
ggplot(aes(log(area_km2), n_species)) +
geom_point() +
labs(x = "log(island area, km²)", y = "Number of plant species") +
theme_bw()
Step 3: Poisson GLM
poiss_model <- glm(n_species ~ log(area_km2),
data = g_df,
family = poisson(link = "log"))
summary(poiss_model)
Call:
glm(formula = n_species ~ log(area_km2), family = poisson(link = "log"),
data = g_df)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.433405 0.057440 59.77 <2e-16 ***
log(area_km2) 0.287518 0.009582 30.00 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 1553.09 on 16 degrees of freedom
Residual deviance: 574.44 on 15 degrees of freedom
AIC: 682.82
Number of Fisher Scoring iterations: 5
Step 4: Check for overdispersion
check_overdispersion(poiss_model)Registered S3 method overwritten by 'lme4':
method from
na.action.merMod car
# Overdispersion test
dispersion ratio = 39.723
Pearson's Chi-Squared = 595.842
p-value = < 0.001
Overdispersion detected.
A dispersion ratio near 1 is fine. Here it is far above 1, so the Poisson standard errors are too small and its p-values are too optimistic. Switch to a negative binomial.
Step 5: Negative binomial GLM
nb_model <- glm.nb(n_species ~ log(area_km2), data = g_df)
summary(nb_model)
Call:
glm.nb(formula = n_species ~ log(area_km2), data = g_df, init.theta = 2.669994465,
link = log)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.37281 0.28016 12.039 < 2e-16 ***
log(area_km2) 0.29884 0.06097 4.901 9.53e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(2.67) family taken to be 1)
Null deviance: 42.006 on 16 degrees of freedom
Residual deviance: 18.189 on 15 degrees of freedom
AIC: 187
Number of Fisher Scoring iterations: 1
Theta: 2.670
Std. Err.: 0.918
2 x log-likelihood: -180.996
check_overdispersion(nb_model)# Overdispersion test (using simulated residuals)
dispersion ratio = 0.684
p-value = 0.776
No overdispersion detected.
Step 6: Test and compare
Anova(nb_model, type = "III", test = "LR")Analysis of Deviance Table (Type III tests)
Response: n_species
LR Chisq Df Pr(>Chisq)
log(area_km2) 23.817 1 1.059e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
AIC(poiss_model, nb_model) df AIC
poiss_model 2 682.8233
nb_model 3 186.9956
The negative binomial has a much lower AIC, so it fits far better.
Step 7: Interpret the slope
Coefficients are on the log scale. Exponentiate to get a multiplicative effect:
exp(coef(nb_model)) (Intercept) log(area_km2)
29.160452 1.348291
Each 1-unit increase in log(area), i.e. a 2.7-fold larger island, multiplies the expected number of species by about 1.35.
Part B: Presence/absence — Uta lizards on islands
Step 8: Look at the data
pa is 1 if Uta lizards are present, 0 if absent. paratio is the island’s perimeter-to-area ratio.
polis_df %>% dplyr::select(island, paratio, pa)# A tibble: 19 × 3
island paratio pa
<chr> <dbl> <dbl>
1 Bota 15.4 1
2 Cabeza 5.63 1
3 Cerraja 25.9 1
4 Coronadito 15.2 0
5 Flecha 13.0 1
6 Gemelose 18.8 0
7 Gemelosw 31.0 0
8 Jorabado 22.9 0
9 Mitlan 12.0 0
10 Pata 11.6 1
11 Piojo 6.09 1
12 Smith 2.28 1
13 Ventana 4.05 1
14 Bahiaan 59.9 0
15 Bahiaas 63.2 0
16 Blanca 22.8 0
17 Pescador 23.5 0
18 Angeldlg 0.21 1
19 Mejia 2.55 1
count(polis_df, pa)# A tibble: 2 × 2
pa n
<dbl> <int>
1 0 9
2 1 10
Step 9: Logistic regression
lizard_model <- glm(pa ~ paratio,
data = polis_df,
family = binomial(link = "logit"))
summary(lizard_model)
Call:
glm(formula = pa ~ paratio, family = binomial(link = "logit"),
data = polis_df)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.6061 1.6953 2.127 0.0334 *
paratio -0.2196 0.1005 -2.184 0.0289 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 26.287 on 18 degrees of freedom
Residual deviance: 14.221 on 17 degrees of freedom
AIC: 18.221
Number of Fisher Scoring iterations: 6
Step 10: Test against a null model
The likelihood-ratio test compares the model with an intercept-only model.
null_model <- glm(pa ~ 1, data = polis_df, family = binomial(link = "logit"))
anova(null_model, lizard_model, test = "Chisq")Analysis of Deviance Table
Model 1: pa ~ 1
Model 2: pa ~ paratio
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 18 26.287
2 17 14.221 1 12.066 0.0005134 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Step 11: Odds ratios and fit
exp(coef(lizard_model)) # odds ratios(Intercept) paratio
36.8210344 0.8028734
exp(confint(lizard_model, "paratio")) # 95% CI for the odds ratio 2.5 % 97.5 %
0.6157322 0.9356727
r2(lizard_model) # pseudo-R²# R2 for Logistic Regression
Tjur's R2: 0.521
An odds ratio below 1 means the odds of presence drop as paratio rises: each 1-unit increase multiplies the odds by 0.8.
Step 12: Plot the fitted curve
polis_df %>%
ggplot(aes(paratio, pa)) +
geom_point() +
geom_smooth(method = "glm", method.args = list(family = binomial)) +
labs(x = "Perimeter:area ratio", y = "Probability Uta present") +
theme_bw()
Step 13: Write it up
nb_lr <- Anova(nb_model, type = "III", test = "LR")
liz_lr <- anova(null_model, lizard_model, test = "Chisq")Plant species richness increased with island area (negative binomial GLM, LR χ²1 = 23.8, p < 0.001); a Poisson model was strongly overdispersed.
The probability that Uta lizards were present declined as island perimeter:area ratio increased (logistic regression, LR χ²1 = 12.1, p < 0.001; odds ratio = 0.8 per unit ratio, n = 19 islands).
Report the family and link, the LR χ² with df and p, and the effect on the response scale (multiplier or odds ratio).