Ecology & Environmental Science
Field ecology survey: sites → plots → species counts
Eight sites along an elevation gradient, five plots per site in different habitats, and counts of six species per plot. Expected abundance depends on canopy cover (opposite signs for odd and even species) and a site effect; counts are gamma–Poisson (negative binomial) with extra zeros.
sites (8) → plots (5) → counts (6 species)
Data structure
Nested site/plot designs with overdispersed, zero-heavy counts are the standard case for GLMMs in ecology and the most common reason Poisson models fail there.
- sitesone row = one survey site
- Region, elevation (200–1800 m) and mean temperature from a −6.5 °C/km lapse rate.
- plotsone row = one plot
- Habitat type and canopy cover clipped to 0–100 %.
- countsone row = one species in one plot
- Expected abundance and the observed count: zero-inflated (15 %) negative binomial (shape 1.5).
Generate the field ecology counts tables
The preset is a starting point: change any formula, noise step or row count and the preview, ER diagram and relation check update.
Key relationships
expected = exp(1.2 - 0.25*k + 0.03*(parent.canopy_pct - 40)*if(mod(k,2)==0, 1, -1) + group_effect(sites.site_id, 0.4))Log-linear abundance with a site random effect read through two levels.
count = if(rbern(0.15), 0, rpois(rgamma(1.5, expected / 1.5)))A gamma–Poisson mixture is a negative binomial; the Bernoulli switch adds structural zeros.
Common pitfalls
- Fitting a Poisson GLM to overdispersed counts, which makes standard errors far too small.
- Treating plots as independent when they share a site (spatial pseudoreplication).
- Ignoring imperfect detection: a zero can mean absent or present but missed.
- Using log(count + 1) with linear regression on zero-heavy data.
Typical analysis
Negative-binomial GLMM (glmmTMB) with canopy × species and a random intercept for site, optionally with a zero-inflation component. Check dispersion with simulated residuals (DHARMa).
library(glmmTMB)
s <- read.csv("sites.csv", comment.char = "#"); p <- read.csv("plots.csv", comment.char = "#"); c <- read.csv("counts.csv", comment.char = "#")
d <- merge(merge(c, p, by = "plot_id"), s, by = "site_id")
fit <- glmmTMB(count ~ species * canopy_pct + (1 | site_id),
ziformula = ~1, family = nbinom2, data = d)
summary(fit)Further reading
- Zuur AF, Ieno EN, Walker NJ, Saveliev AA, Smith GM (2009). Mixed Effects Models and Extensions in Ecology with R. Springer.
- Brooks ME et al. (2017). glmmTMB balances speed and flexibility among packages for zero-inflated generalized linear mixed modeling. The R Journal 9(2):378–400.
Other relational presets
- Schools / classes / students : schools (20) → classes (2–4) → students (Poisson ≈ 22, 10–32)
- A/B test analytics : experiments (4) ⟷ exposures ⟷ users (1 500); users → sessions (Poisson 3) → events (Poisson, duration-dependent)
- Chemical kinetics : runs (12) → timepoints (15)
- All presets and the formula reference