Introduction to Fitting and Interpreting Linear Regression in R

Welcome! In this chapter, we transition from the theoretical formulas of Paper A to the practical application of Linear Regression using R for Paper B. Regression is one of the most powerful tools in an actuary's toolkit—it allows us to understand the relationship between variables (like how a driver's age affects their insurance claim amount) and make predictions about the future.

For Paper B, the examiners aren't just looking for a "correct number"; they want to see that you can use the software to fit a model, interpret the summary output, and validate whether the model is actually appropriate for the data. Don't worry if the R code feels intimidating at first; we will break down the output line by line.

1. Fitting the Model: The lm() Function

In R, we fit a linear regression model using the lm() function (which stands for "linear model"). The most important thing to remember is the formula syntax:

\(response \sim explanatory\)

  • Response Variable (\(y\)): The thing you are trying to predict (e.g., Total Claim Cost).
  • Explanatory Variable (\(x\)): The factor you think influences the response (e.g., Number of Policyholders).

Simple vs. Multiple Linear Regression

In a Simple Linear Regression, we have only one explanatory variable:
\(model1 <- lm(y \sim x, data = my_data)\)

In a Multiple Linear Regression, we have several explanatory variables:
\(model2 <- lm(y \sim x1 + x2 + x3, data = my_data)\)

Quick Tip: R uses the Method of Least Squares to calculate the line of best fit. This method minimizes the sum of the squared differences between the actual data points and the values predicted by the line.

2. Interpreting the Summary Output

Once you fit a model (e.g., \(fit <- lm(y \sim x)\)), you use the command summary(fit) to see the results. This output is where most Paper B questions are focused.

The Coefficients Table

This table provides the Least Squares Estimates for the intercept and the slope(s):

  • (Intercept) Estimate (\(\hat{\beta}_0\)): The predicted value of \(y\) when all \(x\) variables are zero.
  • Variable Estimate (\(\hat{\beta}_1\)): The change in the response variable for every 1-unit increase in that explanatory variable, assuming all other variables stay constant.

Statistical Inference on the Slope

The output also provides a p-value (usually labeled Pr(>|t|)) for each coefficient. This is used to test the Null Hypothesis \(H_0: \beta_i = 0\):

  • Small p-value (typically \(p < 0.05\)): We reject \(H_0\). The explanatory variable is statistically significant—it has a meaningful relationship with the response.
  • Large p-value (\(p > 0.05\)): We fail to reject \(H_0\). The variable may not be a useful predictor in the presence of the other variables.

Key Takeaway: Always look at the stars (***) in the R output. They are a quick visual guide to significance, but in your exam, always quote the actual p-value to support your conclusion.

3. Assessing Goodness of Fit

How well does our model actually explain the data? We use two main measures from the R output:

Multiple R-squared (\(R^2\))

\(R^2\) tells us the proportion of the total variation in the response variable that is explained by the explanatory variables. It ranges from \(0\) to \(1\):

  • \(R^2 = 0.85\) means the model explains 85% of the variance in the data.
  • \(R^2 = 1.00\) means a perfect fit (rare in real-world actuarial data!).

Adjusted R-squared

When you add more explanatory variables, \(R^2\) will almost always increase, even if the new variables are useless. Adjusted R-squared penalizes you for adding unnecessary variables. When comparing two models with different numbers of variables, the one with the higher Adjusted R-squared is generally preferred.

4. Using Residuals to Check Model Validity

A "residual" is the difference between the actual observed value and the value predicted by the model: \(e_i = y_i - \hat{y}_i\). To ensure our linear regression is valid, we check these residuals using R's diagnostic plots, typically via plot(fit).

  • Residuals vs Fitted plot: We want to see a random "cloud" of points. If there is a clear pattern (like a curve or a funnel shape), the linear model might be unsuitable, or the variance might not be constant (heteroscedasticity).
  • Normal Q-Q plot: The points should roughly follow a straight diagonal line. This confirms that the residuals are normally distributed, which is an assumption required for our p-values and confidence intervals to be valid.

Common Mistake: Don't just say "the model is good." Use the residual plots to prove the assumptions of linearity, constant variance, and normality are met.

5. Prediction and Confidence Limits

Once we have a fitted model, we use the predict() function to estimate values for new data. In CS1, you must distinguish between two types of intervals:

Confidence Interval (Mean Response)

This is the interval for the average response of all items with a certain \(x\) value.
R Code: \(predict(fit, newdata, interval = "confidence")\)

Prediction Interval (Individual Response)

This is the interval for a single future observation. Individual outcomes are much harder to pin down than averages, so the Prediction Interval is always wider than the Confidence Interval.
R Code: \(predict(fit, newdata, interval = "prediction")\)

Analogy: It is much easier to predict the average height of 1,000 men (Confidence Interval) than it is to predict the height of the next man who walks through the door (Prediction Interval).

6. Summary and Model Selection

When you have several potential explanatory variables, you need to select the "best" set. This process involves:

  • Checking the significance of each variable (p-values).
  • Looking for the highest Adjusted R-squared.
  • Removing variables that do not contribute significantly to the model (a process often called "stepwise" selection, though in the exam you might just be asked to compare two specific models).

Did you know? Actuaries often use these models to set "base rates." For example, a linear regression might tell them that for every extra year of a vehicle's age, the expected annual repair cost increases by \(\$50\). That \(\$50\) is the slope coefficient you see in your R output!

Quick Review Box

  • lm(y ~ x): Fits the model.
  • summary(): Shows coefficients, p-values, and \(R^2\).
  • p-value < 0.05: Variable is significant.
  • R-squared: How much variance is explained.
  • plot(): Checks if the model assumptions are valid via residuals.
  • Prediction Interval: Always wider than a Confidence Interval.