A GLM has a linear predictor, \(\beta_0 + \beta_1 x\); a link \(g\), with \(g(\text{mean of } y)\) equal to the linear predictor; and a family, the distribution of \(y\) around its mean.
The inverse link \(g^{-1}\) turns the line back into a mean, so the range of \(g^{-1}\) is the range of every prediction.
| Link | \(g(m)\) | Inverse | Range |
|---|---|---|---|
| Identity | \(m\) | \(\text{line}\) | any number |
| Logit | \(\log\frac{m}{1-m}\) | \(\frac{e^{\text{line}}}{1+e^{\text{line}}}\) | 0 to 1 |
| Log | \(\log m\) | \(e^{\text{line}}\) | above 0 |
| Inverse | \(1/m\) | \(1/\text{line}\) | above 0 where the line is above 0 |
Here \(m\) is the mean of \(y\) at a given \(x\), and log is the natural log.
With the normal family and the identity link, the coefficients that maximize the likelihood are the least-squares coefficients.
Each response type comes from a known model. The continuous response is \(2 + 1.5x\) plus normal noise. The binary response has log-odds \(-0.5 + 2.5x\). The counts have log mean \(0.3 + 0.5x\). The positive response is gamma with \(1/\text{mean} = 0.5 + 0.3x\). New data steps through four seeded data sets for each setting, and the plot runs 30% past the data on each side, where the line's predictions are read.
lm(y ~ x) and glm(y ~ x, family = gaussian) give the same line. family = binomial uses the logit link, family = poisson the log link, and family = Gamma the inverse link. Part 1 of The Script fits the binary and count data with lm() and glm().
\(\ell\) is the maximized log-likelihood and \(k\) the number of estimated parameters: the two coefficients, plus a spread parameter for the normal and gamma families. A gap of \(\Delta\) in AIC means the data are about \(e^{\Delta/2}\) times as likely under the lower-AIC model, after counting parameters.
The binomial and Poisson likelihoods are probabilities, and the normal and gamma likelihoods are densities. A probability is at most 1, but a density can be larger: on 0 or 1 data the straight line's normal density is above 1 at many of the points. So the page compares links within the family the data call for: logit against probit for 0 or 1 data, and log, square root, and identity links for counts. For positive decimals the gamma models and the straight line are all densities, so all three share one table.
The probit link uses the normal distribution's cumulative probability in place of the logistic curve, and its S shape is close to the logit's. The square root link sets \(\sqrt{\text{mean count}} = \beta_0 + \beta_1 x\). The identity link for counts sets the mean count equal to the line, which must then stay above 0 at every point.
R also prints the deviance, \(D = 2(\ell_{\text{saturated}} - \ell_{\text{model}})\), where the saturated model fits every point exactly. The saturated model differs by family, so deviances compare models within one family, and this tab compares AIC.
The log link and the gamma family need a positive mean, so they cannot fit data that include negative values. The identity link for counts fails when no straight line stays above 0 at every point. R stops with an error in each case, and the table gives the reason in place of an AIC.
The normal true model has mean \(2 + 1.5x\). The binomial has log-odds \(-0.5 + 2.5x\). The Poisson has log mean \(0.5 + 0.8x\). The gamma has \(1/\text{mean} = 0.08 + 0.5x\). Each setting has three seeded data sets.
glm(y ~ x, family = poisson(link = "sqrt")) fits another link in the same family, and AIC() on each fit gives the table. Part 2 of The Script compares the three Poisson links over two x ranges.
The slope of the curve is largest where \(p = 0.5\) and near zero where \(p\) is near 0 or 1, so one more unit of \(x\) moves \(p\) most in the middle of the curve.
Predict 1 when \(\hat p \ge t\). The false alarm rate is false alarms out of all outcomes with \(y = 0\), and a false alarm is a Type I error. The miss rate is misses out of all outcomes with \(y = 1\), and a miss is a Type II error.
R draws 150 \(x\) values uniformly at random from −4 to 4 with seed 42, then one outcome at each from the current curve with rbinom(). Each point's \(\hat p\) is the curve's height at its \(x\).
fit <- glm(y ~ x, family = binomial), then exp(coef(fit)) for odds ratios, plogis(b0 + b1 * x) or predict(fit, type = "response") for probabilities, and table(model = p_hat >= 0.5, actual = y) for the predictions table. Part 3 of The Script computes these numbers at β₀ = −1, β₁ = 0.8, and x = 1.
\(d_i\) is point \(i\)'s share of the deviance, and \(\hat y_i\) its fitted mean. \(\phi\) is the dispersion: 1 for the binomial and Poisson families, and estimated from the data for the normal and gamma. \(h_i\) is the point's leverage. Dividing by \(\sqrt{\phi(1 - h_i)}\) puts the residuals of every family on one scale, so the two plots and their averages compare directly. R's rstandard(fit) computes them.
With a 0 or 1 response, every model's residuals form two bands, one for each value of \(y\), so a residual plot says less about binary data. Group the points by \(x\) and compare the share of 1s in each group with the fitted curve instead.
When a straight line is fit to counts whose mean grows faster and faster, the residuals are positive at both ends and negative in the middle, and the line predicts negative counts at the low end. When a log-link curve is fit to data whose mean is a straight line, the residuals are negative at both ends and positive in the middle. An identity link on binary data predicts values below 0 and above 1.
The slider sets the slope on the link scale. In the positive scenario the slope is half the slider's value, so the gamma mean, \(1/(0.08 + \text{slope} \times x)\), falls half as fast across x. Each setting has two seeded data sets.
The points are ordered by \(x\) and split into three groups of nearly equal size, the way R's cut(seq_len(n), 3) splits them. The averages come from R's rstandard(fit). Steps 3 and 4 use the counts, where the wrong model's residuals show a clear pattern; free play shows the other scenarios.
plot(fitted(fit), rstandard(fit)) draws one of these plots. Part 4 of The Script computes the thirds for both models at two signal strengths.
| The y values | Family / link |
|---|---|
| Only 0 and 1 | Binomial / logit |
| Whole numbers from 0 up | Poisson / log |
| Positive decimals, with a long upper tail | Gamma / inverse |
| Decimals that include negative values | Normal / identity |
R picks one of the four families at random, a sample size from 80 to 200, and an intercept and slope from ranges that keep the mean valid: a probability between 0 and 1 for the binomial, and a positive mean for the Poisson and the gamma. There are 40 seeded rounds, and each visit starts at a random one.
A normal round whose values all happen to be positive looks like gamma data. Then the gamma's long upper tail is the sign to look for.
Part 5 of The Script draws one round with sample() and fits its family with glm().
| Call | What it returns |
|---|---|
glm(y ~ x, family = binomial) | The fit by maximum likelihood. Families: gaussian, binomial, poisson, Gamma, each with its usual link unless you name another, as in binomial(link = "probit"). |
summary(fit) | Coefficients on the link scale with standard errors, z or t values, and p-values, plus the deviance and AIC. |
AIC(fit), logLik(fit) | AIC, and the log-likelihood with its number of estimated parameters. |
predict(fit, newdata, type = "response") | Predictions on the response scale. Without type = "response" they are on the link scale (log-odds or log mean). |
exp(coef(fit)) | Odds ratios for a logit link, or ratios of mean counts for a log link. |
residuals(fit, type = "deviance") | Deviance residuals, the ones the Wrong Model tab plots. |
plogis(eta), qlogis(p) | The inverse logit and the logit. |
table(model = pred, actual = y) | The predictions table, with the truth across the top. |