Tutorials › Generalized Linear Mixed Models (GLMM) › Analyzing Repeated Ecological Counts

Software and Case Studies · Tutorial 679 of 698

Analyzing Repeated Ecological Counts

Learn to organize repeated ecological counts, fit a site-level Poisson GLMM with an effort offset, and explain what its habitat coefficient says about expected counts.

Intermediate 11 min read

What You'll Learn

  • Organize repeated count observations so that each row represents one site visit.
  • Specify a Poisson random-intercept model for repeated counts and unequal observation effort.
  • Translate an lme4 formula into the model it estimates.
  • Interpret a habitat coefficient as a conditional rate ratio with units and uncertainty.
  • Check whether clustering, exposure, and count-model assumptions are plausible.
  • Recognize when a GLMM result should be compared with other modeling approaches.

From Field Visits to a Repeated-Count Model

Ecological counts are often collected repeatedly at the same field sites: an observer visits each site several times and records how many animals are seen. The observations from one site are not generally independent, because sites can differ persistently in habitat quality, accessibility, or local abundance. Treating every visit as an independent row in an ordinary count regression can understate uncertainty and obscure that site-to-site variation.

A GLMM workflow makes the observational structure explicit. It begins with one row per site visit, records the count and the effort used to obtain it, and identifies site as the clustering unit. The model then represents repeated observations through a site random effect while estimating the association between habitat and the expected count rate.

Definition: For site \(i\) on visit \(j\), let \(Y_{ij}\) be the observed animal count and \(E_{ij}\) the observation effort, measured in a unit such as hours. A Poisson random-intercept model with a log link is $$ Y_{ij}\mid b_i \sim \operatorname{Poisson}(\mu_{ij}), \qquad \log(\mu_{ij})=\log(E_{ij})+\beta_0+\beta_1 H_i+\boldsymbol{\beta}_2^\top\mathbf{X}_{ij}+b_i, \qquad b_i\sim N(0,\sigma_b^2). $$ Here \(H_i\) is a habitat indicator, \(\mathbf{X}_{ij}\) contains any additional modeled predictors, and \(b_i\) is a site-specific random intercept. Conditional on the random effects and predictors, the counts are assumed independent and Poisson distributed. The random effects are assumed independent of the modeled predictors.

The offset \(\log(E_{ij})\) makes expected counts proportional to effort: doubling observation time doubles the expected count if the other model terms remain fixed. The remaining linear predictor describes the expected rate per unit of effort. The habitat coefficient \(\beta_1\) compares habitats while holding the modeled covariates and site random effect fixed. This comparison is conditional, not an assertion that habitat causes any difference.

Build and Check the Observation Table

The analysis table should have a clear unit of observation: one row per visit to a site. Site identifiers must be consistent across visits, counts must be nonnegative integers, and effort must be positive. Habitat should be recorded at the level at which it applies; if it is a site-level feature, it repeats across that site's visit rows. Visit-level variables such as season can vary from one row to another.

sitevisithabitatseasonhourscount
S011OpenSpring1.04
S012OpenSummer1.56
S021WoodlandSpring1.03
S022WoodlandSummer1.55

These four rows are illustrative, hypothetical observations, not a complete study. They show why effort belongs in the table: the second visit lasts 1.5 hours, so its count is not directly comparable to a one-hour count as a measure of rate. Before fitting, check that each site has the intended repeated visits, that habitat does not inadvertently change within a site, and that no missing or zero effort has been encoded as a valid exposure.

Worked Example: Checking Rates Before Modeling

For site S01, the two visits record 4 animals in 1.0 hour and 6 in 1.5 hours. The total count is \(4+6=10\), and total effort is \(1.0+1.5=2.5\) hours. Its crude observed rate is therefore \(10/2.5=4.00\) animals per hour. The second visit's rate is \(6/1.5=4.00\) animals per hour; comparing the raw counts 4 and 6 alone would ignore their different observation times.

This calculation is a descriptive check, not an estimate of the GLMM habitat coefficient. It also does not replace the repeated-visit model: visits from S01 share a site, and the model accounts for that dependence. Confirming that the effort unit and count are paired on the same row is a practical way to catch a common data-assembly error before fitting.

Specify the Model in Software

With lme4, a site random intercept and an effort offset can be written as follows. Assume that Open is the reference habitat and Spring is the reference season.

$$ \texttt{glmer(count \textasciitilde{} habitat + season + offset(log(hours) ) + (1 | site),} $$
$$ \texttt{family = poisson(link = "log"), data = ecology)} $$

In R code, this is normally supplied as one formula and a set of function arguments, for example:

$$ \texttt{glmer(count \textasciitilde{} habitat + season + offset(log(hours)) + (1 | site),} $$
$$ \texttt{family = poisson(link = "log"), data = ecology)} $$

The fixed part estimates habitat and season associations. The term \(\texttt{(1 | site)}\) gives each site its own intercept, modeled as a draw from a normal distribution with mean zero and estimated variance. The offset is included with coefficient fixed at one; it is not an estimated predictor effect. In practice, formula syntax is code rather than a mathematical display, so the essential specification is the model above: Poisson counts, log link, log-hours offset, and site random intercept.

Before interpreting output, verify the fitted model and its data: confirm the response is the count, the grouping variable is site, the effort column is positive, and the habitat reference category is the one intended. Also check that the software converged and that the estimated random-effect variance is not at a boundary without investigation.

Worked Example: Interpreting a Hypothetical Habitat Estimate

Suppose a hypothetical fit reports a woodland coefficient of \(\hat{\beta}_1=-0.405\), with \(SE=0.180\). Open is the reference habitat, effort is measured in hours, and season is included in the model. The conditional rate ratio for Woodland relative to Open is

$$ \exp(\hat{\beta}_1)=\exp(-0.405)=0.667. $$

At the same season, effort, and site random intercept, the fitted expected animal count per hour in Woodland is 0.667 times the expected rate in Open. This corresponds to an estimated \(1-0.667=0.333\), or 33.3%, lower conditional rate. It is an association, not evidence by itself that changing a site's habitat would cause its count rate to change.

For a Wald interval, first calculate \(-0.405\pm1.96(0.180)\), giving coefficient limits \(-0.758\) and \(-0.0522\). Exponentiating gives a rate-ratio interval of approximately \((0.469, 0.949)\). Under the model and the usual large-sample approximation, this interval procedure has 95% coverage across repeated samples; it does not mean there is a 95% probability that this fixed rate ratio is inside this particular interval.

The Wald statistic is \(z=-0.405/0.180=-2.25\), with a two-sided normal-approximation p-value of about 0.0244. At \(\alpha=0.05\), this is evidence against a zero woodland coefficient under the fitted model. The rate-ratio interval excludes 1, in agreement with that test. The interval and p-value describe uncertainty in the model-based habitat comparison; they do not eliminate possible confounding or model misspecification.

Why the Random Intercept and Offset Matter

The random intercept allows sites to have different baseline count rates. Its role can be seen from a short derivation. Conditional on site \(i\)'s random effect, the expected count is

$$ E(Y_{ij}\mid b_i)=E_{ij}\exp(\beta_0+\beta_1H_i+ \boldsymbol{\beta}_2^\top\mathbf{X}_{ij}+b_i). $$

For two visits \(j\) and \(k\) at the same site, conditional independence does not imply marginal independence: both expected counts contain the same random multiplier \(\exp(b_i)\). With fixed predictors and effort, the covariance induced by this shared random intercept is

$$ \operatorname{Cov}(Y_{ij},Y_{ik}) = a_{ij}a_{ik}\operatorname{Var}\{\exp(b_i)\}, \qquad a_{ij}=E_{ij}\exp(\beta_0+\beta_1H_i+ \boldsymbol{\beta}_2^\top\mathbf{X}_{ij}). $$

For \(b_i\sim N(0,\sigma_b^2)\), \(\operatorname{Var}\{\exp(b_i)\}=\exp(\sigma_b^2)\{\exp(\sigma_b^2)-1\}\), which is positive when \(\sigma_b^2>0\). Thus shared site heterogeneity produces positive dependence among repeated counts. The random intercept is not a separate habitat effect: it represents unobserved site-level differences after accounting for the fixed predictors.

For the same covariates and a common random-effect distribution across habitats, the conditional rate ratio for Woodland versus Open is \(\exp(\beta_1)\). If the random-effect distribution is the same in both habitats, integrating over that distribution also preserves this ratio in the marginal mean. Nevertheless, the fitted coefficient is conventionally interpreted as a conditional GLMM effect, and it should be reported that way.

Diagnostics, Alternatives, and Limitations

A successful software fit is not proof that the model describes the data adequately. Examine observed counts and fitted values by site and visit, including counts per unit effort. Check convergence messages, unusually influential sites, and whether the random-intercept variance is estimated plausibly. Compare observed and model-implied count variation; if variation remains greater than the Poisson model allows, investigate overdispersion and whether the mean structure or conditional distribution needs revision. Excess zeros or systematic within-site time patterns can also signal that the specified model misses important structure.

A Poisson GLM that omits site can be a simpler alternative, but it assumes observations are independent after conditioning on its fixed predictors. When visits within a site are correlated, that assumption can yield misleading uncertainty. A generalized estimating equation is another approach when the target is a population-averaged association rather than a site-conditional GLMM coefficient. A negative-binomial mixed model may be considered when a Poisson conditional distribution does not capture the residual count variation. These approaches answer related but not identical questions and rely on different assumptions.

The random-intercept model also has limits. It assumes that one shared site effect adequately represents the dependence; it does not automatically model serial correlation between visits or site-specific habitat associations. The normal random-effects distribution and independence between random effects and predictors are substantive assumptions. If habitat is a site-level characteristic, its coefficient is estimated from differences between sites, not from within-site changes. Unmeasured differences between habitats may therefore explain some or all of the observed association. An effort offset is appropriate only when expected counts scale proportionally with the recorded effort and the unit of effort has been measured consistently.

Common pitfall: “The woodland coefficient is negative, so woodland reduces animal abundance.” This overstates the result. The coefficient describes a model-based conditional association in expected counts per unit effort, given the included predictors and random intercept. It is not a causal conclusion, and a poorly fitting model can make even that association misleading.

A Practical Analysis Sequence

1
Define the observation.
Use one row per site visit; identify the count, effort, site, habitat, and visit-level predictors.
2
Validate the data.
Check integer nonnegative counts, positive effort, consistent site identifiers, and the intended habitat coding.
3
Fit the specified GLMM.
Use a Poisson log-link model, a log-effort offset, and a random intercept for site when the assumptions are scientifically reasonable.
4
Inspect fit and report meaning.
Check diagnostics and convergence, then report the habitat rate ratio, uncertainty, exposure unit, reference category, and conditional interpretation.

Check Your Understanding

Answer each question, then compare your reasoning with the brief solutions.

  1. Why should two visits to the same site not automatically be treated as independent observations?
  2. If the woodland coefficient is \(-0.223\), what is the fitted conditional rate ratio for Woodland relative to Open?
  3. What does the offset \(\log(E_{ij})\) assume about the relationship between effort and expected count?
  4. A site has 9 animals counted over 3 hours. What is its crude observed rate, and what does that rate not account for?

Solutions. (1) Visits share a site and may share unobserved site characteristics; a shared random intercept represents this source of dependence. (2) \(\exp(-0.223)=0.800\), so the fitted conditional rate is 0.800 times the Open rate, or 20.0% lower, holding modeled terms fixed. (3) It assumes expected count is proportional to effort, with the log of effort entering as a fixed-coefficient-one offset. (4) \(9/3=3.00\) animals per hour. This descriptive rate does not adjust for other predictors or account for repeated-visit dependence across sites.

Clinical Biostats