9.2 Generalized linear models
Generalized linear models (GLMs) extend the linear model to non-Gaussian outcomes by adding a link function. The link function transforms the linear predictor into the natural scale of the outcome.
9.2.1 The GLM framework
A GLM has three components:
- Likelihood: The outcome distribution from the exponential family (Gaussian, binomial, Poisson, etc.).
- Linear predictor: (same as before).
- Link function: , where .
The link function maps from the parameter's natural scale to the real line:
| Outcome | Likelihood | Link | Name | | ------------------- | ---------- | -------------- | ------------------- | | Continuous | Gaussian | Identity | Linear regression | | Binary | Binomial | Logit | Logistic regression | | Count | Poisson | Log | Poisson regression | | Positive continuous | Gamma | Log or inverse | Gamma regression |
# Demonstrate different GLM link functions
set.seed(42)
n <- 200
x <- runif(n, 0, 10)
# 1. Gaussian (identity link)
y_gauss <- 2 + 1.5 * x + rnorm(n, sd = 2)
m_gauss <- lm(y_gauss ~ x)
# 2. Binomial (logit link)
p_true <- 1 / (1 + exp(-(0 + 0.5 * x)))
y_bin <- rbinom(n, 1, p_true)
m_logistic <- glm(y_bin ~ x, family = binomial)
# 3. Poisson (log link)
lambda_true <- exp(0.2 + 0.15 * x)
y_pois <- rpois(n, lambda_true)
m_pois <- glm(y_pois ~ x, family = poisson)
cat("Gaussian (identity link):\n")
cat(" Intercept:", round(coef(m_gauss)[1], 3), " Slope:", round(coef(m_gauss)[2], 3), "\n")
cat("\nBinomial (logit link):\n")
cat(" Intercept:", round(coef(m_logistic)[1], 3), " Slope:", round(coef(m_logistic)[2], 3), "\n")
cat(" (on logit scale; use plogis() for probabilities)\n")
cat("\nPoisson (log link):\n")
cat(" Intercept:", round(coef(m_pois)[1], 3), " Slope:", round(coef(m_pois)[2], 3), "\n")
cat(" (on log scale; use exp() for counts)\n")9.2.2 The logit link (logistic regression)
For binary outcomes, the logit link maps probabilities to the real line:
The inverse is the logistic function:
# Demonstrate logistic regression
set.seed(42)
n <- 200
x <- runif(n, 0, 10)
# True relationship
p_true <- 1 / (1 + exp(-(-2 + 0.5 * x)))
y <- rbinom(n, 1, p_true)
# Fit logistic regression
m <- glm(y ~ x, family = binomial)
cat("Logistic regression:\n")
cat(" Intercept:", round(coef(m)[1], 3), "\n")
cat(" Slope:", round(coef(m)[2], 3), "\n")
# Convert to probability scale
cat("\nPredicted probabilities at x = 0, 5, 10:\n")
for (x_val in c(0, 5, 10)) {
logit_p <- coef(m)[1] + coef(m)[2] * x_val
p_val <- 1 / (1 + exp(-logit_p))
cat(sprintf(" x = %2d: p = %.3f\n", x_val, p_val))
}
# Plot
x_seq <- seq(0, 10, length.out = 200)
logit_pred <- coef(m)[1] + coef(m)[2] * x_seq
p_pred <- 1 / (1 + exp(-logit_pred))
plot(x, y, pch = 16, col = "steelblue",
main = "Logistic regression: binary outcome",
xlab = "x", ylab = "p(y = 1)")
lines(x_seq, p_pred, lwd = 2, col = "firebrick")
abline(h = 0.5, lty = 2, col = "gray50")9.2.3 The log link (Poisson regression)
For count outcomes, the log link ensures the expected count is always positive:
# Demonstrate Poisson regression
set.seed(42)
n <- 200
x <- runif(n, 0, 10)
# True relationship: lambda = exp(0.5 + 0.2 * x)
lambda_true <- exp(0.5 + 0.2 * x)
y <- rpois(n, lambda_true)
# Fit Poisson regression
m <- glm(y ~ x, family = poisson)
cat("Poisson regression:\n")
cat(" Intercept:", round(coef(m)[1], 3), "\n")
cat(" Slope:", round(coef(m)[2], 3), "\n")
# Convert to count scale
cat("\nPredicted counts at x = 0, 5, 10:\n")
for (x_val in c(0, 5, 10)) {
log_lambda <- coef(m)[1] + coef(m)[2] * x_val
cat(sprintf(" x = %2d: lambda = %.3f\n", x_val, exp(log_lambda)))
}
# Plot
x_seq <- seq(0, 10, length.out = 200)
log_pred <- coef(m)[1] + coef(m)[2] * x_seq
lambda_pred <- exp(log_pred)
plot(x, y, pch = 16, col = "steelblue",
main = "Poisson regression: count outcome",
xlab = "x", ylab = "y (count)")
lines(x_seq, lambda_pred, lwd = 2, col = "firebrick")The link function is the bridge. The linear model lives on the real line — it can produce any value from to . But outcomes live on different scales: probabilities are in , counts are in . The link function bridges these scales. It transforms the linear predictor to the outcome's natural scale. This is not a hack — it is the natural consequence of choosing the maximum entropy likelihood and then connecting it to the linear model. The link function is what makes GLMs general.
Summary of Section 9.2
- GLMs extend linear models with a link function: .
- Logit link for binary outcomes: .
- Log link for count outcomes: .
- Identity link for continuous outcomes: (ordinary linear regression).
- The link function bridges the linear predictor to the outcome's natural scale.