REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 2
Simple Linear Regression
This chapter introduces simple linear regression, which
models a continuous outcome as a linear function of a single predictor.
Linear regression is the workhorse of quantitative medical research and
forms the foundation for more complex models. Here we derive the model,
discuss its assumptions and show how to fit it in R,
Python and Julia.
Model Formulation
Suppose we observe individuals, each with a predictor variable and a response . The simple linear regression model asserts that
where is the intercept, is the slope and are independent errors with mean zero and constant variance. The slope represents the expected change in for a one–unit increase in .
The parameters and are estimated by ordinary least squares (OLS), which chooses estimates and that minimise the sum of squared residuals . The resulting fitted line provides point predictions for new observations.
Assumptions and Diagnostics
For valid inference, several assumptions are required:
Linearity. The relationship between and is approximately linear. Scatterplots help assess this assumption.
Independence. Observations are independent. Repeated measurements on the same individual violate this assumption and call for mixed effects models.
Homoscedasticity. The error variance is constant across levels of ; residual plots should show no systematic patterns.
Normality of errors. The errors have an approximately normal distribution. This assumption primarily affects inference rather than estimation; QQ plots of residuals can help assess normality.
Violations of these assumptions can often be addressed by transforming the outcome or predictor, or by using more flexible models such as splines or generalised additive models (see Chapter [chap:smoothing_and_gam]).
Worked Example: Weight and Blood Pressure
Consider a hypothetical dataset of 10 patients with body weights () and systolic blood pressures ():
| Weight (kg) | 60 | 65 | 68 | 70 | 72 | 75 | 78 | 80 | 82 | 85 |
| Systolic BP (mmHg) | 120 | 122 | 124 | 125 | 127 | 130 | 132 | 135 | 137 | 140 |
We can fit a linear regression to model blood pressure as a function of weight.
R
# Create data vectors
weight <- c(60, 65, 68, 70, 72, 75, 78, 80, 82, 85)
sbp <- c(120, 122, 124, 125, 127, 130, 132, 135, 137, 140)
# Fit linear regression
model <- lm(sbp ~ weight)
summary(model)
# Obtain predictions for a 78‑kg person
predict(model, newdata = data.frame(weight = 78))
Python
import numpy as np
import pandas as pd
import statsmodels.api as sm
# Create DataFrame
df = pd.DataFrame({
'weight': [60,65,68,70,72,75,78,80,82,85],
'sbp': [120,122,124,125,127,130,132,135,137,140]
})
# Add intercept term
X = sm.add_constant(df['weight'])
y = df['sbp']
model = sm.OLS(y, X).fit()
print(model.summary())
# Prediction for 78‑kg person
pred = model.predict([1, 78])
Julia
using DataFrames, GLM
# Create DataFrame
df = DataFrame(weight = [60,65,68,70,72,75,78,80,82,85],
sbp = [120,122,124,125,127,130,132,135,137,140])
# Fit linear model
model = lm(@formula(sbp ~ weight), df)
print(coef(model))
# Prediction for 78‑kg person
predict(model, DataFrame(weight = [78]))
These commands yield estimates similar to and , indicating that blood pressure increases by about 0.8 mmHg for each kilogram of body weight. The predicted pressure for a 78‑kg individual is roughly 132 mmHg.
Interpretation
In linear regression, the slope quantifies the expected change in the outcome per unit change in the predictor. The intercept represents the expected outcome when the predictor is zero (which may or may not have a practical interpretation depending on the context). Confidence intervals for the slope and intercept convey the precision of these estimates; if the confidence interval for the slope excludes zero, there is evidence of an association between the predictor and the outcome. Always inspect residual plots to check assumptions and consider transformations or more flexible models if patterns remain.
Exercises
Exercises
Derive the least squares estimates and for the simple linear regression model. Show that they minimise the sum of squared residuals.
Using the provided weight–blood pressure data, calculate the slope and intercept manually (without using software). Compare your results with those from
RorPython.Generate a scatterplot of the data and overlay the fitted regression line. What does the plot suggest about the linearity assumption?
Simulate 100 samples of size from a model with true coefficients and and normally distributed errors with standard deviation 5. For each sample, estimate . Examine the sampling distribution of and calculate its mean and standard deviation.
Code examples use illustrative datasets. Supply data with the named columns to run them.