---
title: "Generalized Linear Models"
description: "Generalized linear models in R: Gaussian, binomial, Poisson and negative binomial GLMs on insurance, admissions and Titanic data, with odds ratios and ROC."
author: ""
---
```{r options, echo=FALSE, warning=FALSE, message=FALSE}
directory<-if (rstudioapi::isAvailable()) paste0(dirname(rstudioapi::getActiveDocumentContext()$path),"/") else ""
source(paste0(directory, "R/code_options.R"))
source(paste0(directory, "R/code_rwf.R"))
```
```{r load, include=FALSE}
library(ggplot2)
library(grid) # plot_multiplot() calls viewport() and grid.layout()
library(MASS)
library(car)
library(pROC)
library(emmeans)
load(paste0(directory,"data/df_admission.rda"))
load(paste0(directory,"data/df_insurance.rda"))
load(paste0(directory,"data/df_titanic.rda"))
# Prepare Titanic
df_titanic <- df_titanic[!is.na(df_titanic$survived) &
!is.na(df_titanic$age) &
!is.na(df_titanic$sex) &
!is.na(df_titanic$pclass), ]
df_titanic$sex <- factor(df_titanic$sex)
df_titanic$pclass <- factor(df_titanic$pclass, labels=c("1st","2nd","3rd"))
# Prepare insurance
df_insurance$smoker <- factor(df_insurance$smoker, levels=c("no","yes"))
df_insurance$sex <- factor(df_insurance$sex, levels=c("female","male"))
df_insurance$region <- factor(df_insurance$region)
df_insurance$log_charges <- log(df_insurance$charges)
# Admission
df_admission$rank <- factor(df_admission$rank, labels=paste0("Rank",1:4))
```
---
## 1. Theoretical Framework
Generalized Linear Models (GLMs; Nelder & Wedderburn, 1972) extend ordinary least squares regression to outcome variables whose conditional distribution belongs to the **exponential family**. A GLM is defined by three components:
1. **Random component** — the conditional distribution of $Y | \mathbf{x}$ (e.g., Normal, Binomial, Poisson, Gamma)
2. **Systematic component** — a linear predictor $\eta_i = \mathbf{x}_i^\top \boldsymbol{\beta}$
3. **Link function** $g(\cdot)$ — a monotone differentiable function relating the conditional mean $\mu_i = E[Y_i|\mathbf{x}_i]$ to the linear predictor: $g(\mu_i) = \eta_i$
The model is estimated via **maximum likelihood**, and inference relies on asymptotic normality of the MLE. Parameters are tested with Wald statistics ($z = \hat\beta / \text{SE}$, asymptotically standard normal) or likelihood ratio tests (LRTs, asymptotically $\chi^2$).
| Family | Usual link | Use case |
|---|---|---|
| Gaussian | Identity | Continuous, symmetric outcome |
| Binomial | Logit | Binary / proportion outcome |
| Poisson | Log | Non-negative integer counts |
| Negative Binomial | Log | Overdispersed counts |
| Gamma | Inverse / Log | Positive skewed continuous |
The links are the canonical ones, except the log for the Negative Binomial and Gamma families.
---
## 2. Gaussian GLM — Insurance Charges
### 2.1 Data and Rationale
Medical insurance charges are right-skewed and strictly positive. A log transformation induces approximate normality, making a Gaussian GLM with identity link on $\log(\text{charges})$ appropriate. Alternatively, a Gamma GLM with log link directly models the original scale without requiring a transformation.
```{r insurance_eda, fig.height=3.5}
p1 <- ggplot(df_insurance, aes(x=charges)) +
geom_histogram(bins=50, fill="#2166ac", colour="white") +
theme_bw() + labs(title="Raw charges\n(right-skewed)", x="Charges (USD)")
p2 <- ggplot(df_insurance, aes(x=log_charges)) +
geom_histogram(bins=50, fill="#4dac26", colour="white") +
theme_bw() + labs(title="Log-transformed charges\n(approx. normal)", x="log(Charges)")
plot_multiplot(p1, p2, cols=2)
```
### 2.2 Model Specification and Estimation
We fit a full model with all main effects and the theoretically motivated `smoker × bmi` interaction (smoking amplifies the effect of obesity on health costs).
```{r gaussian_glm}
m_gauss_null <- glm(log_charges ~ 1,
family=gaussian(link="identity"),
data=df_insurance)
m_gauss_main <- glm(log_charges ~ age + sex + bmi + children + smoker + region,
family=gaussian(link="identity"),
data=df_insurance)
m_gauss_full <- glm(log_charges ~ age + sex + bmi + children + smoker + region +
smoker:bmi,
family=gaussian(link="identity"),
data=df_insurance)
summary(m_gauss_full)
```
### 2.3 Type II Analysis of Deviance
```{r gaussian_anova}
Anova(m_gauss_full, type=2)
```
### 2.4 Likelihood Ratio Test — Interaction Term
The deviance of a Gaussian model is its residual sum of squares. The $\chi^2$ statistic is the drop in deviance divided by the residual variance of the larger model: `anova()` uses it for the *p* value but prints only the drop.
```{r gaussian_lrt}
lrt_interaction <- anova(m_gauss_main, m_gauss_full, test="LRT")
lrt_interaction
p_text <- function(p) ifelse(p < 0.001, "p < 0.001", sprintf("p = %.3f", p))
cat(sprintf("\nLRT: χ²(%d) = %.3f, %s\n",
lrt_interaction$Df[2],
lrt_interaction$Deviance[2] / summary(m_gauss_full)$dispersion,
p_text(lrt_interaction$`Pr(>Chi)`[2])))
```
### 2.5 Model Fit
For a Gaussian model, the share of the null deviance that the model explains is the ordinary $R^2$.
```{r gaussian_fit}
cat("Null deviance :", round(m_gauss_null$deviance, 2), "\n")
cat("Residual deviance :", round(m_gauss_full$deviance, 2), "\n")
cat("R² :", round(1 - m_gauss_full$deviance/m_gauss_null$deviance, 4), "\n")
cat("AIC :", round(AIC(m_gauss_full), 2), "\n")
```
### 2.6 Variance Inflation Factors
VIF > 5 indicates problematic collinearity; VIF > 10 is severe.
```{r gaussian_vif}
vif(m_gauss_full)
```
The VIFs of `smoker` and `bmi:smoker` are about 25, but only because of the interaction: BMI is far from 0, so `bmi:smoker` is almost a multiple of `smoker`. Centering BMI removes this collinearity and leaves the fit unchanged:
```{r gaussian_vif_centred}
m_gauss_centred <- update(m_gauss_full, data=transform(df_insurance, bmi=bmi - mean(bmi)))
vif(m_gauss_centred)
all.equal(fitted(m_gauss_full), fitted(m_gauss_centred))
```
What changes is the meaning of the `smokeryes` coefficient. In the original model it is the effect of smoking at a BMI of 0 (`r round(coef(m_gauss_full)["smokeryes"], 2)`, *p* = .284), which nobody has. In the centered model it is the effect at the average BMI (`r round(coef(m_gauss_centred)["smokeryes"], 2)`, *p* < .001).
### 2.7 Diagnostics
```{r gaussian_diagnostics, fig.height=9}
par(mfrow=c(3,2))
plot(m_gauss_full, which=1:6)
par(mfrow=c(1,1))
```
### 2.8 Marginal Effects of Smoking by BMI
```{r gaussian_interaction, fig.height=4}
nd <- expand.grid(
age=mean(df_insurance$age),
sex="female",
bmi=seq(18, 53, length.out=100),
children=0,
smoker=c("no","yes"),
region="northeast"
)
nd$fit <- predict(m_gauss_full, newdata=nd, type="response")
ggplot(nd, aes(x=bmi, y=exp(fit), colour=smoker, fill=smoker)) +
geom_line(linewidth=1) +
scale_colour_manual(values=c("#2166ac","#d6604d")) +
theme_bw() +
labs(title="Predicted charges by BMI and smoking status",
subtitle="Marginal prediction at mean age, female, 0 children, northeast",
x="BMI", y="Predicted charges (USD)", colour="Smoker")
```
---
## 3. Binomial GLM — Graduate Admission
### 3.1 Data and Rationale
The `df_admission` dataset contains binary admission decisions (0/1) from a graduate program alongside GRE score, GPA, and institutional rank. A binomial GLM with logit link — **logistic regression** — models the log-odds of admission as a linear function of the predictors.
$$\log\frac{P(\text{admit}=1|\mathbf{x})}{1-P(\text{admit}=1|\mathbf{x})} = \beta_0 + \beta_1\,\text{gre} + \beta_2\,\text{gpa} + \beta_3\,\text{rank}$$
### 3.2 Descriptive Overview
```{r logistic_desc, fig.height=3.5}
p1 <- ggplot(df_admission, aes(x=gre, fill=factor(admit))) +
geom_density(alpha=0.5) +
scale_fill_manual(values=c("#2166ac","#d6604d"), labels=c("Rejected","Admitted")) +
theme_bw() + labs(title="GRE by admission", fill=NULL)
p2 <- ggplot(df_admission, aes(x=gpa, fill=factor(admit))) +
geom_density(alpha=0.5) +
scale_fill_manual(values=c("#2166ac","#d6604d"), labels=c("Rejected","Admitted")) +
theme_bw() + labs(title="GPA by admission", fill=NULL)
plot_multiplot(p1, p2, cols=2)
```
### 3.3 Model Estimation
```{r logistic_models}
m_admit_null <- glm(admit ~ 1,
family=binomial(link="logit"),
data=df_admission)
m_admit_main <- glm(admit ~ gre + gpa + rank,
family=binomial(link="logit"),
data=df_admission)
summary(m_admit_main)
```
### 3.4 Omnibus Likelihood Ratio Test
```{r logistic_lrt}
lrt_admit <- anova(m_admit_null, m_admit_main, test="LRT")
lrt_admit
cat(sprintf("\nOverall model: χ²(%d) = %.3f, %s\n",
lrt_admit$Df[2],
lrt_admit$Deviance[2],
p_text(lrt_admit$`Pr(>Chi)`[2])))
```
### 3.5 Odds Ratios and 95% Profile-Likelihood CIs
```{r logistic_or}
or_table <- cbind(
OR = exp(coef(m_admit_main)),
exp(confint(m_admit_main))
)
round(or_table, 3)
```
### 3.6 Model Fit Indices
```{r logistic_fit}
# McFadden pseudo-R²
pseudo_r2 <- 1 - m_admit_main$deviance / m_admit_null$deviance
# Nagelkerke pseudo-R²
n <- nrow(df_admission)
r2_cox <- 1 - exp((m_admit_main$deviance - m_admit_null$deviance) / n)
r2_nag <- r2_cox / (1 - exp(-m_admit_null$deviance / n))
cat("McFadden pseudo-R² :", round(pseudo_r2, 4), "\n")
cat("Nagelkerke pseudo-R²:", round(r2_nag, 4), "\n")
cat("AIC :", round(AIC(m_admit_main), 2), "\n")
cat("BIC :", round(BIC(m_admit_main), 2), "\n")
```
### 3.7 Discrimination — ROC Curve and AUC
```{r logistic_roc, fig.height=5}
pred_prob <- predict(m_admit_main, type="response")
roc_obj <- roc(df_admission$admit, pred_prob, quiet=TRUE)
plot(roc_obj, col="#2166ac", lwd=2,
main=sprintf("ROC Curve — Graduate Admission (AUC = %.3f)", auc(roc_obj)))
abline(a=0, b=1, lty=2, col="grey60")
```
### 3.8 Classification Table (threshold = 0.5)
```{r logistic_confusion}
pred_class <- ifelse(pred_prob >= 0.5, 1, 0)
conf_mat <- table(Predicted=pred_class, Observed=df_admission$admit)
conf_mat
cat(sprintf("\nAccuracy : %.3f\n", sum(diag(conf_mat))/sum(conf_mat)))
cat(sprintf("Sensitivity: %.3f\n", conf_mat[2,2]/sum(conf_mat[,2])))
cat(sprintf("Specificity: %.3f\n", conf_mat[1,1]/sum(conf_mat[,1])))
```
### 3.9 Predicted Probability Curves
```{r logistic_curves, fig.height=4}
nd2 <- expand.grid(
gre = seq(220, 800, length.out=200),
gpa = c(2.5, 3.0, 3.5, 4.0),
rank = factor("Rank2", levels=levels(df_admission$rank))
)
nd2$prob <- predict(m_admit_main, newdata=nd2, type="response")
ggplot(nd2, aes(x=gre, y=prob, colour=factor(gpa), group=factor(gpa))) +
geom_line(linewidth=1) +
scale_colour_brewer(palette="RdYlBu", direction=-1) +
theme_bw() +
labs(title="Predicted admission probability by GRE and GPA",
subtitle="Institutional rank fixed at Rank 2",
x="GRE score", y="P(admit = 1)", colour="GPA")
```
---
## 4. Poisson and Negative Binomial GLM — Insurance Children
### 4.1 Rationale
The number of dependent children is a non-negative integer — a natural candidate for Poisson regression. A key assumption of the Poisson model is **equidispersion** ($\text{Var}[Y] = \mu$). When the variance exceeds the mean (**overdispersion**), the Negative Binomial GLM provides a robust alternative by introducing a dispersion parameter $\alpha$ such that $\text{Var}[Y] = \mu + \alpha\mu^2$.
### 4.2 Checking for Overdispersion
```{r poisson_disp}
cat("Mean of children:", round(mean(df_insurance$children), 4), "\n")
cat("Var of children:", round(var(df_insurance$children), 4), "\n")
cat("Dispersion ratio :", round(var(df_insurance$children)/mean(df_insurance$children), 4), "\n")
```
```{r poisson_hist, fig.height=4}
ggplot(df_insurance, aes(x=children)) +
geom_bar(fill="#2166ac", colour="white") +
theme_bw() +
labs(title="Distribution of number of children", x="Children", y="Count")
```
### 4.3 Poisson Model
```{r poisson_model}
m_pois <- glm(children ~ age + sex + bmi + smoker + region,
family=poisson(link="log"),
data=df_insurance)
summary(m_pois)
```
**Rule of thumb for overdispersion:** the ratio of the residual deviance to its degrees of freedom; values well above 1 suggest overdispersion.
```{r poisson_disptest}
dispersion_ratio <- m_pois$deviance / m_pois$df.residual
cat("Residual deviance / df:", round(dispersion_ratio, 4), "\n")
```
The ratio points to overdispersion: with counts as small as these, it would be about 1.15 if the Poisson model were right. There is no exact cut-off, however, so section 4.5 tests it.
### 4.4 Negative Binomial Model
```{r nb_model}
m_nb <- glm.nb(children ~ age + sex + bmi + smoker + region,
data=df_insurance)
summary(m_nb)
cat("\nEstimated dispersion parameter (theta):", round(m_nb$theta, 4), "\n")
```
### 4.5 Poisson vs. Negative Binomial — Likelihood Ratio Test
The Negative Binomial model has one more parameter than the Poisson model, $\alpha$. With $\alpha = 0$ it is the Poisson model (`glm.nb()` reports $\theta = 1/\alpha$), so the two are nested and a likelihood ratio test can compare them:
$$LR = 2\,(\ell_{\text{NB}} - \ell_{\text{Poisson}})$$
Because $\alpha$ cannot be negative, the null hypothesis $\alpha = 0$ lies at the edge of its range. If it is true, the statistic is 0 about half of the time and follows $\chi^2_1$ otherwise, so the *p* value is half of the $\chi^2_1$ tail area (Lawless, 1987).
`anova(m_pois, m_nb, test="LRT")` does not give this test. It subtracts the two residual deviances, which are measured from different saturated models, and it has 0 degrees of freedom because it does not count $\alpha$.
```{r nb_lrt}
lr_nb <- as.numeric(2 * (logLik(m_nb) - logLik(m_pois)))
p_nb <- pchisq(lr_nb, df=1, lower.tail=FALSE) / 2
cat("AIC Poisson :", round(AIC(m_pois), 2), "\n")
cat("AIC Negative Binom :", round(AIC(m_nb), 2), "\n")
cat("Log-likelihood Pois:", round(logLik(m_pois), 2), "\n")
cat("Log-likelihood NB :", round(logLik(m_nb), 2), "\n")
cat("Likelihood ratio :", round(lr_nb, 2), "\n")
cat("p value :", format.pval(p_nb, digits=3, eps=0.001), "\n")
```
The test rejects the Poisson model (*LR* = `r round(lr_nb, 2)`, *p* < .001) and the AIC of the Negative Binomial model is `r round(AIC(m_pois) - AIC(m_nb), 2)` lower, so the counts are overdispersed, as the ratio in section 4.3 suggested. The coefficients of the two models are nearly the same; it is the standard errors that change. They are about 19% larger in the Negative Binomial model, because the Poisson model assumes less variance than the data have.
### 4.6 Incidence Rate Ratios (IRR)
```{r nb_irr}
irr_table <- cbind(
IRR = exp(coef(m_nb)),
exp(confint(m_nb))
)
round(irr_table, 3)
```
### 4.7 Predicted Counts by Age and Smoking Status
```{r nb_plot, fig.height=4}
nd3 <- expand.grid(
age = seq(18, 64, length.out=100),
sex = "female",
bmi = mean(df_insurance$bmi),
smoker = c("no","yes"),
region = "northeast"
)
nd3$predicted <- predict(m_nb, newdata=nd3, type="response")
ggplot(nd3, aes(x=age, y=predicted, colour=smoker)) +
geom_line(linewidth=1) +
scale_colour_manual(values=c("#2166ac","#d6604d")) +
theme_bw() +
labs(title="Predicted number of children by age and smoking status",
subtitle="Negative Binomial GLM — marginal at mean BMI, female, northeast",
x="Age", y="Predicted count", colour="Smoker")
```
---
## 5. Logistic Regression — Titanic Survival
### 5.1 Model with Interaction
The Titanic evacuation followed a "women and children first" protocol, motivating a `sex × pclass` interaction: the survival advantage of female passengers may differ by class.
```{r titanic_models}
m_titanic_main <- glm(survived ~ sex + pclass + age,
family=binomial(link="logit"),
data=df_titanic)
m_titanic_int <- glm(survived ~ sex * pclass + age,
family=binomial(link="logit"),
data=df_titanic)
anova(m_titanic_main, m_titanic_int, test="LRT")
```
```{r titanic_summary}
summary(m_titanic_int)
```
### 5.2 Odds Ratios
```{r titanic_or}
round(cbind(OR=exp(coef(m_titanic_int)), exp(confint(m_titanic_int))), 3)
```
### 5.3 Estimated Marginal Means (Survival Probability)
```{r titanic_emm, fig.height=4}
emm <- emmeans(m_titanic_int, ~ sex * pclass, type="response")
emm_df <- as.data.frame(emm)
ggplot(emm_df, aes(x=pclass, y=prob, colour=sex, group=sex)) +
geom_point(size=3) +
geom_line(linewidth=1) +
geom_errorbar(aes(ymin=asymp.LCL, ymax=asymp.UCL), width=0.15) +
scale_colour_manual(values=c("#d6604d","#2166ac")) +
theme_bw() +
labs(title="Estimated marginal survival probabilities",
subtitle="Titanic — sex × passenger class interaction (95% CI)",
x="Passenger class", y="P(survived = 1)", colour="Sex")
```
### 5.4 ROC and AUC
```{r titanic_roc, fig.height=5}
pred_titanic <- predict(m_titanic_int, type="response")
roc_titanic <- roc(df_titanic$survived, pred_titanic, quiet=TRUE)
plot(roc_titanic, col="#d6604d", lwd=2,
main=sprintf("ROC Curve — Titanic Survival (AUC = %.3f)", auc(roc_titanic)))
abline(a=0, b=1, lty=2, col="grey60")
```
---
## 6. Model Comparison and Selection
### 6.1 Information Criteria
AIC penalizes model complexity as $-2\ell + 2k$; BIC uses $-2\ell + k\ln n$ and imposes a heavier penalty for larger samples, favoring more parsimonious models. Here $\ell$ is the log-likelihood and $k$ the number of estimated parameters: the coefficients, plus the residual variance of a Gaussian model or $\theta$ of a Negative Binomial model.
```{r aic_table}
models <- list(
"Gaussian — main effects" = m_gauss_main,
"Gaussian — + smoker×bmi" = m_gauss_full,
"Logistic — admission" = m_admit_main,
"Poisson — children" = m_pois,
"Neg. Binom — children" = m_nb,
"Logistic — Titanic main" = m_titanic_main,
"Logistic — Titanic + int." = m_titanic_int
)
aic_df <- data.frame(
Model = names(models),
k = sapply(models, function(m) attr(logLik(m), "df")),
LogLik = sapply(models, function(m) round(as.numeric(logLik(m)), 2)),
AIC = sapply(models, function(m) round(AIC(m), 2)),
BIC = sapply(models, function(m) round(BIC(m), 2)),
stringsAsFactors = FALSE
)
knitr::kable(aic_df, row.names=FALSE)
```
AIC and BIC compare models of the same outcome fitted to the same observations. The table has three such pairs: the two Gaussian models, the two models of `children` and the two Titanic models. In each pair the second model has the lower AIC and BIC.
---
## 7. Assumptions and Diagnostics Summary
| Assumption | Gaussian GLM | Logistic GLM | Poisson GLM |
|---|---|---|---|
| Correct distributional family | Residuals ~ Normal | Binary outcome | Non-negative integer |
| Linear relationship on link scale | Partial residual plots | Log-odds linearity | Log-mean linearity |
| Independence of observations | Design assumption | Design assumption | Design assumption |
| No influential outliers | Cook's distance, leverage | Cook's distance | Cook's distance |
| No multicollinearity | VIF < 5 | VIF < 5 | VIF < 5 |
| Equidispersion | — | — | Deviance / df ≈ 1 |
```{r diagnostics_combined, fig.height=9}
par(mfrow=c(3,1))
plot(m_gauss_full, which=1, main="Gaussian GLM — residuals vs fitted")
plot(m_admit_main, which=1, main="Logistic GLM — residuals vs fitted")
plot(m_nb, which=1, main="Neg. Binom — residuals vs fitted")
par(mfrow=c(1,1))
```
---
## 8. References
Nelder, J. A., & Wedderburn, R. W. M. (1972). Generalized linear models. *Journal of the Royal Statistical Society: Series A*, *135*(3), 370–384.
McCullagh, P., & Nelder, J. A. (1989). *Generalized Linear Models* (2nd ed.). Chapman & Hall.
Agresti, A. (2015). *Foundations of Linear and Generalized Linear Models*. Wiley.
Venables, W. N., & Ripley, B. D. (2002). *Modern Applied Statistics with S* (4th ed.). Springer.
Lawless, J. F. (1987). Negative binomial and mixed Poisson regression. *Canadian Journal of Statistics*, *15*(3), 209–225.