Introduction: Why Generalised Linear Models (GLMs)?
Welcome! If you have already mastered linear regression, you know it is a powerful tool for predicting a response variable. However, in the real world—especially in actuarial work—data often doesn't follow a neat "Normal" distribution. For example, claim counts are usually whole numbers (Poisson), and claim amounts are often positive and skewed (Gamma). This is where Generalised Linear Models (GLMs) come to the rescue!
In this chapter, we focus on the practical side of GLMs using R. We will learn how to tell the software which "family" of distributions to use, how to interpret the results, and how to decide if our model is actually any good. Don't worry if the theory felt heavy in Paper A; Paper B is all about "reading" the output and making sense of it.
Note: This chapter builds on your knowledge of "Fitting and interpreting linear regression output." If you're comfortable with \(y \sim x\) syntax, you're already halfway there!
1. The Anatomy of the glm() Function
In R, we fit GLMs using the glm() function. The syntax is very similar to lm(), but with one crucial addition: the family argument.
The basic code structure:
my_model <- glm(response ~ explanatory_variables, family = distribution(link = "link_function"), data = my_data)
Choosing the Family and Link
The family tells R the distribution of the response variable, and the link function tells R how the mean relates to the linear predictor. Under the CS1 syllabus, you should be familiar with these common pairings:
- Normal (Gaussian):
family = gaussian(link = "identity")— This is actually just standard linear regression! - Poisson:
family = poisson(link = "log")— Perfect for claim counts. - Binomial:
family = binomial(link = "logit")— Used for "Yes/No" outcomes (e.g., will a policy lapse?). - Gamma:
family = gamma(link = "inverse")orlink = "log"— Used for positive, skewed data like claim costs. - Exponential: A special case of the Gamma family.
Key Concept: The Canonical Link. Each distribution has a "natural" link function called the canonical link. For Poisson, it is log; for Binomial, it is logit. R uses these as defaults if you don't specify the link.
2. Understanding the Linear Predictor
The linear predictor, often denoted as \(\eta\) (eta), is the combination of your variables:
\(\eta = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ...\)
In R, we can include different types of terms:
- Variables: Continuous numbers (e.g., Age, Sum Assured).
- Factors: Categorical data (e.g., Region, Gender). R will automatically create "dummy variables" for these.
- Interaction Terms: Written as
x1:x2orx1*x2. This is used when the effect of one variable depends on the level of another.
Quick Tip: If you see RegionNorth in your output, it means the model is comparing the "North" region to a "Baseline" region (the reference level). The estimate shows the difference in the linear predictor compared to that baseline.
3. Interpreting the summary() Output
Once you run summary(my_model), R gives you a wealth of information. Let’s break down the most important parts for your exam.
The Coefficients Table
For each variable, R provides:
- Estimate: The calculated value of \(\beta\). Careful: These are on the scale of the link function! If you used a log link, the estimate is the change in the log of the mean.
- Std. Error: The uncertainty around the estimate.
- z value: The test statistic (\(Estimate \div Std. Error\)).
- Pr(>|z|): The p-value. If this is less than 0.05, the variable is usually considered statistically significant at the 5% level.
Deviance and Scaled Deviance
In GLMs, Deviance replaces the "Sum of Squares" used in linear regression. It measures the "distance" between your model and a perfect model (a saturated model).
- Null Deviance: How well the response is predicted by a model with only an intercept (no variables).
- Residual Deviance: How well the response is predicted by your model.
Rule of Thumb: We want the Residual Deviance to be as small as possible. If the Residual Deviance is much smaller than the Null Deviance, your variables are doing a good job!
AIC (Akaike Information Criterion)
AIC is a measure of model fit that penalises you for adding too many variables. Lower AIC = Better Model. If you are comparing two models, the one with the lower AIC is generally preferred.
4. Statistical Tests for Model Selection
How do we know if adding a variable actually improved the model? We use two main tests:
The Likelihood Ratio Test (LRT) / Analysis of Deviance
In R, we use the anova() function to compare two nested models (where one model is a simpler version of the other):
anova(model_simple, model_complex, test = "Chisq")
If the p-value is small (e.g., < 0.05), the more complex model is significantly better.
Pearson’s Chi-square Test
This is often used to check the Goodness of Fit. We compare the Pearson residuals to a \(\chi^2\) distribution. If the p-value is very small, it suggests the model does not fit the data well.
5. Checking Residuals
Even if the p-values look great, you must check if the model's assumptions hold. In GLMs, we primarily use two types of residuals:
- Pearson Residuals: Based on the difference between observed and predicted values, scaled by the standard deviation.
- Deviance Residuals: Based on the contribution of each point to the total deviance.
What to look for in plots:
Use plot(my_model). You are looking for a random scatter of points. If you see a distinct pattern (like a funnel shape or a curve), your model might be missing an interaction term or using the wrong link function.
Common Pitfalls to Avoid
- Mixing up Link and Response: Remember that GLM coefficients are on the link scale. To get a prediction on the original scale (e.g., actual claim counts), you usually need to use the
predict(..., type = "response")function in R. - Overfitting: Adding too many variables will lower your deviance but might make the model useless for new data. Always check the AIC.
- Ignoring Factors: If a factor has 5 levels, R will show 4 coefficients. The 1st level is hidden inside the "(Intercept)".
Quick Review Box
1. Function: glm(y ~ x, family = poisson, data = d)
2. Significance: Check Pr(>|z|) in the summary table.
3. Comparison: Lower AIC and Residual Deviance indicate a better fit.
4. Nested Models: Use anova(m1, m2, test = "Chisq") to see if the extra variables are worth it.
5. Scale: Results are on the link scale unless you specify type = "response" in predictions.
Summary Key Takeaway
Fitting a GLM is about choosing the right probability distribution for your data and a link function to connect it to your predictors. Interpretation involves looking for significant p-values, reducing deviance from the null model, and using AIC to balance accuracy with simplicity.