Book contents

REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 4

Regression Diagnostics and Assumption Checking

Once a linear regression model is fitted, it is crucial to assess whether the underlying assumptions hold. Diagnostic plots and statistical tests help identify violations and guide appropriate remedial actions. This chapter describes common diagnostics and shows how to implement them in R, Python and Julia.

Assessing Linearity and Model Fit

Scatterplots and residual plots.

A scatterplot of the outcome versus each predictor should suggest an approximately linear relationship. After fitting the model, plot the residuals (ε̂i=yi−ŷi\hat{\varepsilon}_i = y_i - \hat{y}_i) against the fitted values ŷi\hat{y}_i. The residual plot should display no systematic pattern; a non–linear trend indicates that transformations or nonlinear terms may be needed.

Partial residuals.

For multiple regression, partial residual plots (component–plus–residual plots) show the relationship between a single predictor and the outcome after adjusting for other predictors. These plots help detect nonlinearity or outliers attributable to individual predictors.

Homoscedasticity (Constant Variance)

Homoscedasticity means that the residual variance is constant across different levels of the predictors. In a residual vs fitted plot, the points should exhibit roughly constant vertical spread. A trumpet‐shaped pattern indicates heteroscedasticity. Remedies include transforming the outcome (e.g., log transformation) or using weighted least squares.

Normality of Residuals

Although the normality of residuals is not required for estimation, it affects the validity of confidence intervals and p–values. A normal Q–Q plot compares the quantiles of the residuals to the theoretical quantiles of a normal distribution. Points should fall approximately on a straight line. Deviations in the tails may indicate heavy tails or skewness.

Independence and Influential Observations

Independence.

Independence of observations is essential for valid inference. Time series or repeated measurements violate this assumption. Mixed effects models (Chapter [chap:mixed_effects_models]) can account for such dependencies.

Influential points.

Outliers or high‐leverage observations can unduly influence parameter estimates. Cook’s distance and leverage statistics quantify the influence of each observation. Investigate observations with large Cook’s distances and decide whether they represent data errors or genuine but extreme values.

Implementing Diagnostics

R
model <- lm(sbp ~ weight + age + sex, data = df)

# Residual vs fitted plot
plot(model$fitted.values, resid(model),
     xlab = "Fitted values", ylab = "Residuals")
abline(h = 0, col = "red")

# Normal Q-Q plot
qqnorm(resid(model)); qqline(resid(model), col = "blue")

# Cook's distance
cooksD <- cooks.distance(model)
plot(cooksD, type="h", ylab="Cook's Distance")
Python
import matplotlib.pyplot as plt
import statsmodels.api as sm

model = sm.OLS(y, X).fit()

# Residuals vs fitted
plt.scatter(model.fittedvalues, model.resid)
plt.axhline(0, color='red')
plt.xlabel('Fitted values'); plt.ylabel('Residuals');

# Q-Q plot
sm.qqplot(model.resid, line='45')

# Influence measures
influence = model.get_influence()
cooks = influence.cooks_distance[0]
plt.stem(cooks, use_line_collection=True)
plt.ylabel("Cook's distance")
Julia
using StatsPlots
using GLM

model = lm(@formula(sbp ~ weight + age + sex), df)

# Residual vs fitted
scatter(fitted(model), residuals(model), xlabel = "Fitted", ylabel = "Residuals")
hline!([0])

# Q-Q plot
qqplot(residuals(model))

# Cook's distance requires additional packages such as MixedModels or custom
# computation; users can compute influence measures manually if needed.

Interpretation

Diagnostics help determine whether the assumptions of linear regression are reasonable. When residual plots reveal nonlinearity or heteroscedasticity, consider transforming variables or adding polynomial terms. If residuals are non‐normal, robust standard errors or bootstrapping may provide more reliable inference. Dependence among observations often necessitates mixed effects models. Influential observations should be examined carefully but not automatically discarded unless there is a clear data quality issue.

Exercises

Exercises

  1. For the multiple regression model fitted in Chapter [chap:03_multiple_linear_regression], create residual vs fitted and Q–Q plots. Do the plots suggest any violations of assumptions? Try applying a log transformation to the outcome and refit the model. How do the diagnostics change?

  2. Simulate a dataset with heteroscedastic errors (e.g., error variance increases with xx). Fit a linear regression and examine the residual plot. Fit a weighted least squares model and compare the results.

  3. For a given dataset of your choice, compute Cook’s distance for each observation. Identify any influential points and discuss whether they should be excluded or whether the model should be modified.

Code examples use illustrative datasets. Supply data with the named columns to run them.