Lesson 8 of 13

Poisson and Negative Binomial Regression

In the last lessons you let a smooth curve bend through your data and learned to check that it was flexible enough. Every one of those models still described a continuous measurement. Now the thing we predict changes shape entirely: it becomes a count.

Meet Amara, who runs a neighbourhood bike-repair co-op. Every evening she writes down a single number: how many bikes came in for repair that day. On a slow Tuesday it is 0. Most days it is 2 or 3. But when the roads are wet, or a weekend race rolls through town, the number jumps, 8, 13, once a frantic 21.

That daily tally is a count, and counts do not behave like the heights and temperatures you have been modelling. They are whole numbers, never negative, bunched near zero with a long tail to the right. Fit an ordinary straight line to them and it will cheerfully predict "-1.4 repairs on a cold Monday" and assume every day is equally noisy, both nonsense here.

This lesson gives counts the model they deserve. The bars below show the shape count data takes, the same lopsided pile-up Amara sees each month: a stack of low days, extra zeros, a thin tail of busy ones. The line is a model trying to trace it. By the end you will know why that first model misses at both ends, and the small change that fixes it.

By the end of this lesson you will be able to:

  • Say why ordinary linear regression is the wrong tool for a count
  • Fit a Poisson regression in R and read its coefficients as rate ratios
  • Spot overdispersion, and switch to a negative binomial model when a Poisson is too confident

Prerequisites: you can fit and read a linear model, and you have met the GLM idea and a link function (the logit) in the earlier regression lessons. Comfortable with logs and exponents (exp is always positive).

Why the straight line fails

A count is not a measurement

The outcome here, repairs, is a count: 0, 1, 2, 3, and so on. It has three habits that a straight line cannot respect:

  • It is never negative. You cannot repair minus two bikes, yet a regression line happily runs below zero.
  • It moves in whole steps. There is no such thing as 3.7 repairs.
  • Its spread grows with its average. Quiet days (a mean near 2) barely wobble; busy rainy days (a mean near 5) swing far more. Ordinary regression assumes the noise is the same size everywhere.

Here is Amara's actual record, two months of daily counts, built right here so you can run everything on this page in interactive R:

RInteractive R
set.seed(1) n <- 150 rain <- rbinom(n, 1, 0.4) # 0 = dry day, 1 = rainy day mu <- exp(1.0 + 0.55 * rain) # the true average repairs per day repairs <- rnbinom(n, mu = mu, size = 2.0) # each day's actual count bikes <- data.frame(rain = factor(rain, labels = c("dry", "rainy")), repairs = repairs) c(days = n, zeros = sum(bikes$repairs == 0), busiest = max(bikes$repairs)) #> days zeros busiest #> 150 27 21

  

The histogram below is those 150 numbers. Notice the pile-up at zero (27 of the 150 days), the peak at 2 to 3, and the thin tail stretching out to 21. That shape, floored at zero and skewed hard to the right, is the fingerprint of count data, and it is exactly what a straight line gets wrong.