Skip to main content

GLM Playground

1. Premise

A health planner wants to know how many extra GP visits to budget for each extra illness a person reports. The Australian Health Survey has the data: 5190 adults, their visits to a doctor in the past two weeks, and their illnesses over the same period.

Why not just use lm()

OLS assumes that at each value of \(x\) the visits follow a Normal distribution around the fitted line, with the same spread for everyone. Pick a group and compare that assumption (red curve) with the people in the group (grey bars).

TipPredict

Among people with 0 illnesses, how much of the red Normal curve falls below zero visits? Is the spread at 5 illnesses the same as at 0?

  • Most people made no visits at all, so the data is inflated at zero and trail off to the right. A symmetric distribution can’t match that shape, and it also results into negative visit counts.
  • Variance grows with the mean, from about 0.1 at 0 illnesses to about 2.2 at 5. OLS uses one variance (about 0.6) for everyone, so it overstates the uncertainty for healthy people and understates it for sick ones.
  • The fitted line itself goes below zero for some people, and no one makes \(-0.2\) visits.

The planner’s question is about the mean number of visits, so we keep the regression idea, \(\beta_0 + \beta_1 x\), and fix the two parts that broke. A link function keeps the mean in the allowed range (section 2). A family gives the counts a distribution with the right shape and spread (sections 3 to 5) resulting in a generalised linear model.

3. Comparing models

We simulate visits from a known truth: \(\log \mu = \beta_0 + \beta_1 x\), with extra spread \(k\), so that \(\text{Var}(Y) = \mu + k\mu^2\). Setting \(k = 0\) gives the Poisson. The dashed black curve is the true mean, and each coloured curve is the corresponding estimate of it.

TipPredict
  1. Where will the OLS line go wrong first: at small \(x\) or at large \(x\)?
  2. Back-transformed \(\log(y+1)\) fits a curve. Will it sit above or below the truth?
  3. Push \(k\) up. Do the Poisson and negative binomial curves separate? Do their standard errors?
  • OLS fits a straight line through a curved mean, so it goes negative at small \(x\) and undershoots at large \(x\).
  • \(\exp(\widehat{E}[\log(Y+1)]) - 1\) is lesser than the true mean everywhere. \(E[\log Y] < \log E[Y]\), so the back-transform is smaller than the mean.
  • Raising \(k\) hardly moves the Poisson curve because its mean model is still correct. Its SE stays small while the NB SE grows.

4. Cost of wrong variance assumption

A 95% confidence interval should contain the true \(\beta_1\) in 95% of repeated samples. We rerun the experiment many times and count how often each model’s interval covers the truth. All three models share the correct mean model and differ only in their variance assumption.

Comparison of variance assumptions

Model \(\text{Var}(Y)\) Extra parameter Fitted with
Poisson \(\mu\) none, \(\phi = 1\) glm(..., family = poisson)
Quasi-Poisson \(\phi\,\mu\) \(\phi\), one number for everyone glm(..., family = quasipoisson)
Negative binomial \(\mu + \mu^2/\theta\) \(\theta\) (here \(\theta = 1/k\)) MASS::glm.nb(...)

Quasi-Poisson keeps the Poisson mean model, \(\log \mu = X\beta\), and only says the variance is some multiple \(\phi\) of the mean. It borrows the Poisson equations \(X^\top(y - \mu) = 0\), so the coefficients come out identical to the Poisson fit. Then it estimates \(\phi\) from the Pearson residuals,

\[ \hat\phi = \frac{1}{n - p} \sum_i \frac{(y_i - \hat\mu_i)^2}{\hat\mu_i}, \]

and multiplies every standard error by \(\sqrt{\hat\phi}\). When \(\hat\phi > 1\) the intervals widen and the p-values grow, and when \(\hat\phi < 1\) (underdispersion) they shrink. Because no distribution is named, there’s no likelihood, so R gives no AIC and you compare quasi models with F-tests (anova(m, test = "F")).

The cell below fits one simulated dataset (using the \(k\) slider further down) and does the quasi-Poisson correction by hand.

Quasi-Poisson is the cheapest model for fixing poisson when it fails, since it’s one extra number and the coefficients don’t change. It assumes the variance grows linearly with the mean, though. If the variance grows like \(\mu^2\), as it does in this simulation, the negative binomial describes the data better.

TipPredict

At \(k = 1\), what share of Poisson intervals will contain the truth: about 95%, 80%, or 50%? And will quasi-Poisson, which uses the wrong variance shape, still get close to 95%?

With overdispersed data the Poisson point estimate stays unbiased, but its intervals come out too narrow and its p-values are small. Quasi-Poisson widens the SE by a factor of \(\sqrt{\hat\phi}\), and the negative binomial adds a \(k\mu^2\) term to the variance. Either fix brings coverage back near 95%, and at \(k = 0\) the three models agree.

5. Back to the doctor-visit data

Back to the DoctorVisits data from the slides. The predictors stay fixed and only the family changes. Compare the standard errors across families and check whether each model reproduces the counts we observed.

TipPredict

About 80% of people made 0 visits. Which family will predict that share best? Which one will predict negative visits?

The grey bars are observed frequencies, and the red points are what the fitted model expects once you sum over all 5190 people.

  • The Poisson and negative binomial rate ratios are close, but their SEs differ.
  • Quasi-Poisson keeps the Poisson coefficients and multiplies the SEs by \(\sqrt{\hat\phi}\). It has no likelihood, so R reports no AIC.
  • The Poisson model expects about 220 fewer zeros than we observe, while the negative binomial lands within about 20 (4162 against 4141). You could use a zero-inflated model (pscl::zeroinfl) also here and compare.
  • The Gaussian model puts probability on negative visit counts.

6. Exercise

Edit the cell below and run it:

  • Add income to the formula. Does the rate ratio for illness change?
  • Fit illness * chronic. On the log scale, what does an interaction do to the rate ratios?
  • Fit family = poisson(link = "identity"). You may need starting values (start =). Why?