Tutorials › Exam PA - Predictive Analytics › Worked Poisson GLM For Event Counts

Exam PA · GLM Families and Model Construction · Tutorial 329 of 966

Worked Poisson GLM For Event Counts

Learn to fit and check a Poisson model for counts, and recognize when unequal exposure makes an unadjusted count model inappropriate.

Intermediate 12 min read

What You'll Learn

  • State the assumptions behind a Poisson GLM for event counts.
  • Fit an intercept-only or predictor-based Poisson model in R.
  • Derive and calculate the intercept-only fitted mean.
  • Check fitted means and convergence without treating convergence as proof of model adequacy.
  • Explain why unequal observation periods require an exposure adjustment.
  • Recognize the boundary case when every observed count is zero.

When a Poisson GLM Is Appropriate

A count records how many times an event occurs in a defined observational unit and period: for example, the number of service calls in a week or the number of incidents at a location during a month. A Poisson generalized linear model (GLM) relates the expected count to predictors while ensuring that fitted means are positive. It is appropriate only when the count definition and observation opportunities are comparable and the distributional assumptions are credible.

Definition: For observation \(i\), let \(Y_i\) be a nonnegative integer count, and let \(\mu_i=E[Y_i\mid x_i]\) be its conditional mean given predictors \(x_i\). A Poisson GLM assumes that the \(Y_i\) are conditionally independent and \(Y_i\mid x_i\sim\operatorname{Poisson}(\mu_i)\), so \(\operatorname{Var}(Y_i\mid x_i)=\mu_i\). With the log link, the model is \(\log(\mu_i)=\eta_i=x_i^\mathsf{T}\beta\), or equivalently \(\mu_i=\exp(x_i^\mathsf{T}\beta)\).

The vector \(x_i\) contains the model terms for observation \(i\), including an intercept unless the model is deliberately specified without one. The log link is useful because its inverse, the exponential function, produces a positive fitted mean for any finite linear predictor. A Poisson GLM does not require all observations to have the same mean: predictors may make \(\mu_i\) differ. It does, however, assume a Poisson distribution conditional on those predictors. In particular, its conditional variance equals its conditional mean.

For the basic setup here, each observation must cover the same length of time or the same relevant opportunity for events to occur. Equal calendar duration alone may not be enough if exposure to risk differs substantially. If the observation periods or opportunities vary, modeling the raw counts as if they had equal exposure can distort the fitted mean structure; an exposure adjustment is needed.

Deriving the Intercept-Only Estimate

An intercept-only model has one common conditional mean \(\mu\) for all \(n\) observations. The following derivation shows why, when the total observed count is positive, the maximum-likelihood estimate is the sample mean.

1
Write the likelihood.
For independent observations \(y_1,\ldots,y_n\) from a Poisson distribution with mean \(\mu>0\), the likelihood is the product of their Poisson probabilities.
2
Take logarithms and differentiate.
After removing terms that do not depend on \(\mu\), the log-likelihood is \(\ell(\mu)=\sum_i y_i\log(\mu)-n\mu+\text{constant}\). Its derivative is \(\frac{d\ell}{d\mu}=\frac{\sum_i y_i}{\mu}-n\).
3
Solve the score equation.
If \(\sum_i y_i>0\), setting the derivative to zero gives \(\hat{\mu}=\sum_i y_i/n=\bar{y}\). The second derivative is \(-\sum_i y_i/\mu^2<0\), so this stationary point is a maximum.

This argument depends on a positive total count. If every count is zero, then \(\ell(\mu)=-n\mu+\text{constant}\), which increases as \(\mu\) decreases toward zero. Since the Poisson mean is restricted to \(\mu>0\), the likelihood has a supremum as \(\mu\to0^+\), but no maximum at a permitted value of \(\mu\). It is therefore inaccurate to apply the interior estimate and claim a positive fitted mean in this boundary case.

Worked Example: An Intercept-Only Count Model

A hypothetical monitoring program records event counts over six equal one-week periods. The counts are \(1,0,2,1,3,1\). Fit an intercept-only Poisson model by hand, and calculate the fitted probability of no events in one week.

Method and assumptions. Each period has equal exposure, and the model assumes independent Poisson counts with one common mean \(\mu\). The total count is positive, so the interior maximum-likelihood result derived above applies.

Calculate the estimate. The total is \(1+0+2+1+3+1=8\), and there are six periods. Thus \(\hat{\mu}=8/6=4/3\), or approximately \(1.3333\) events per week. The fitted mean for each week is \(4/3\); the fitted total across six weeks is \(6(4/3)=8\), matching the observed total.

Calculate the requested probability. For a Poisson variable with mean \(4/3\), \(P(Y=0)=e^{-4/3}\approx0.2636\), rounded to four decimal places.

Check. The fitted mean is positive, the fitted total equals the observed total, and the probability is between zero and one. The calculation avoids the mistake of treating \(e^{-4/3}\) as a fitted count; it is a probability, while \(4/3\) is the fitted mean count.

Fitting and Inspecting the Model in R

In R, specify the Poisson family and log link with family = poisson(link = "log"). For an intercept-only model, a formula containing only the intercept is count ~ 1. For a model with a categorical predictor, set its reference level deliberately when that choice matters to the coefficient display. The following example uses one row per equal-duration period.

Worked Example: Adding a Group Predictor in R

A hypothetical operator records event counts during four equal one-day periods at each of two types of monitoring site, A and B. The counts are A: \(1,2,1,2\), and B: \(3,4,3,4\). Fit a Poisson GLM with a group effect, describe its fitted means, and identify checks to make after fitting.

Set up the data and model. Create a data frame with columns named count and group, using the eight counts in the order given and assigning the first four rows to A and the last four to B. Set group as a factor with levels A and B, in that order. Fit glm(count ~ group, family = poisson(link = "log"), data = dat). This model has a separate mean for each group, represented through an intercept and a group contrast.

Calculate the fitted group means. The four counts for A sum to \(1+2+1+2=6\), so its fitted mean is \(6/4=1.5\). The four counts for B sum to \(3+4+3+4=14\), so its fitted mean is \(14/4=3.5\). Because this model gives each group its own mean, the fitted values for the rows are \(1.5,1.5,1.5,1.5,3.5,3.5,3.5,3.5\).

Relate the means to the log-link coefficients. With A as the reference level, the fitted intercept is \(\log(1.5)\approx0.4055\), and the fitted group coefficient is \(\log(3.5)-\log(1.5)=\log(7/3)\approx0.8473\). Adding these on the linear-predictor scale gives \(0.4055+0.8473\approx1.2528\) for group B, whose exponential is \(3.5\) up to rounding.

Inspect the fit. In R, examine the model’s convergence indicator and any warnings, then inspect the fitted values with fitted(model) and the coefficients with coef(model). The fitted values should agree with the group means just calculated, allowing for numerical precision. The model has converged only if its fitting procedure reports convergence; that fact alone does not establish that the Poisson assumptions are suitable.

Check. The fitted group totals are \(4(1.5)=6\) for A and \(4(3.5)=14\) for B, matching the observed totals. This group-specific check catches a common setup error: accidentally fitting the intercept-only model, which would assign the same mean to both groups.

For a more general predictor structure, the fitted mean for row \(i\) is \(\hat{\mu}_i=\exp(x_i^\mathsf{T}\hat{\beta})\). It is not generally equal to the observed count \(y_i\), nor should a fitted mean be rounded before computing other quantities. Check that fitted values are finite and positive and that the design matrix represents the intended predictors and reference levels. Earlier material on convergence warnings, rank deficiency, deviance, and overdispersion provides additional diagnostic context; convergence is a computational property, not a guarantee of model adequacy.

When comparing nested Poisson models, R can report a change in deviance using an analysis-of-deviance comparison, for example with anova(model_small, model_large, test = "Chisq"). Any reported reduction should be calculated from the underlying, unrounded deviances and rounded only at the end. Subtracting displayed deviances that have already been rounded can produce a different last digit.

Unequal Exposure Changes the Mean Structure

Suppose one row represents one event-count observation, but the rows cover different periods or amounts of opportunity. A larger count could then reflect more exposure rather than a higher underlying event rate. An unadjusted Poisson model with the same predictor structure for all rows treats those counts as comparable observations; that setup is not justified merely because each response is an integer count.

Key result: If observation \(i\) has known exposure \(t_i>0\) and a common event rate \(\lambda_i\) per unit of exposure, the expected count is \(\mu_i=t_i\lambda_i\). Taking logarithms gives \(\log(\mu_i)=\log(t_i)+\log(\lambda_i)\). Thus the log of exposure is an offset in a Poisson GLM for counts. Omitting it when exposure varies can confound exposure with the predictors of interest.

Worked Example: Why Equal-Period Counts Matter

A hypothetical service records two event counts: 2 events during one unit of exposure and 4 events during two units. Suppose the underlying rate is assumed constant across these observations. Compare the unadjusted intercept-only mean per row with the rate estimate that accounts for exposure, and state the appropriate model form.

Unadjusted calculation. Treating the counts as equal-exposure observations gives an intercept-only mean of \((2+4)/2=3\) events per observation. But the observations do not cover the same exposure: one has one unit and the other has two.

Exposure-adjusted calculation. The combined exposure is \(1+2=3\) units, and the combined count is \(2+4=6\). The estimated rate is therefore \(6/3=2\) events per exposure unit. The implied fitted means are \(1(2)=2\) and \(2(2)=4\), respectively.

Model form and check. For an exposure-adjusted Poisson model, use \(\log(\mu_i)=\log(t_i)+\beta_0\) when the rate is constant, so in R the exposure term is supplied as offset(log(exposure)). The fitted counts sum to \(2+4=6\), matching the observed total. The common mistake is to call the raw mean of 3 a rate; it is a mean count per row, not a mean count per unit of exposure.

Common Mistakes and Practical Checks

  • Assuming any count data are Poisson. Check whether the counts are conditionally independent and whether the conditional variance assumption is plausible. If variability is much greater than the fitted mean, a basic Poisson model may not describe the data adequately.
  • Ignoring exposure. Different observation windows or opportunities can create different expected counts even when the underlying rates are identical. Use an exposure adjustment rather than interpreting raw count differences as rate differences.
  • Equating convergence with a good model. Convergence means the numerical fitting procedure met its stopping criteria. It does not confirm the distributional assumptions, the predictor specification, or the quality of the data.
  • Claiming a positive interior estimate for all-zero data. When every count is zero, the likelihood’s supremum is approached as \(\mu\to0^+\); there is no maximizing \(\mu>0\). A boundary estimate or warning should be investigated rather than reported as an ordinary positive fitted mean.

A useful sequence is to define exactly what one row and one count represent, verify that exposures are comparable, specify the Poisson family with the intended link, and then inspect convergence and fitted means. After these checks, assess whether the model’s assumptions and fit are reasonable for the application.

Check Your Understanding

Work each problem before checking its answer.

  1. Five independent observations cover equal periods and have counts \(0,1,2,1,1\). Under an intercept-only Poisson model, calculate the maximum-likelihood fitted mean and the fitted probability of zero events, rounding the probability to four decimal places. (Answer: \(\hat{\mu}=1\); \(P(Y=0)=e^{-1}\approx0.3679\).)
  2. A Poisson GLM with a separate mean for each of two groups is fitted to equal-exposure observations. Group A has counts \(0,2,1\), and group B has counts \(2,4,3\). Calculate the fitted mean for each group and the intercept and group coefficient when A is the reference level. (Answer: \(\hat{\mu}_A=1\), \(\hat{\mu}_B=3\), intercept \(=0\), group coefficient \(=\log(3)\approx1.0986\).)
  3. Two observations have counts 5 and 3 and exposures 2.5 and 1.5 units, respectively. Assuming a common rate, calculate the rate estimate and the two implied fitted counts. (Answer: rate \(=(5+3)/(2.5+1.5)=2\) per unit; fitted counts \(5\) and \(3\).)
  4. An intercept-only Poisson dataset contains four zero counts. Does the likelihood have an interior maximum at a positive mean? State what happens to the likelihood as the mean approaches zero. (Answer: No; its supremum is approached as \(\mu\to0^+\), but there is no maximum for \(\mu>0\).)
  5. A model reports convergence, but the observation periods vary and no exposure term was included. Is convergence enough to justify interpreting the fitted means as comparable event rates? Give the needed adjustment in model form. (Answer: No; include the known exposure as an offset, \(\log(t_i)\), in \(\log(\mu_i)=\log(t_i)+\log(\lambda_i)\).)
Clinical Biostats