Book contents

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 nn individuals, each with a predictor variable xix_i and a response yiy_i. The simple linear regression model asserts that

yi=β0+β1xi+εi,i=1,…,n,y_i = \beta_0 + \beta_1 x_i + \varepsilon_i,\quad i=1,\dots,n,

where β0\beta_0 is the intercept, β1\beta_1 is the slope and εi\varepsilon_i are independent errors with mean zero and constant variance. The slope β1\beta_1 represents the expected change in yy for a one–unit increase in xx.

The parameters β0\beta_0 and β1\beta_1 are estimated by ordinary least squares (OLS), which chooses estimates β̂0\hat{\beta}_0 and β̂1\hat{\beta}_1 that minimise the sum of squared residuals ∑i=1n(yi−β̂0−β̂1xi)2\sum_{i=1}^n (y_i - \hat{\beta}_0 - \hat{\beta}_1 x_i)^2. The resulting fitted line ŷ=β̂0+β̂1x\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x provides point predictions for new observations.

Assumptions and Diagnostics

For valid inference, several assumptions are required:

  1. Linearity. The relationship between xx and yy is approximately linear. Scatterplots help assess this assumption.

  2. Independence. Observations are independent. Repeated measurements on the same individual violate this assumption and call for mixed effects models.

  3. Homoscedasticity. The error variance is constant across levels of xx; residual plots should show no systematic patterns.

  4. 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 (xx) and systolic blood pressures (yy):

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 β̂0≈72.1\hat{\beta}_0 \approx 72.1 and β̂1≈0.8\hat{\beta}_1 \approx 0.8, 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

  1. Derive the least squares estimates β̂0\hat{\beta}_0 and β̂1\hat{\beta}_1 for the simple linear regression model. Show that they minimise the sum of squared residuals.

  2. Using the provided weight–blood pressure data, calculate the slope and intercept manually (without using software). Compare your results with those from R or Python.

  3. Generate a scatterplot of the data and overlay the fitted regression line. What does the plot suggest about the linearity assumption?

  4. Simulate 100 samples of size n=10n=10 from a model with true coefficients β0=50\beta_0=50 and β1=1.5\beta_1=1.5 and normally distributed errors with standard deviation 5. For each sample, estimate β̂1\hat{\beta}_1. Examine the sampling distribution of β̂1\hat{\beta}_1 and calculate its mean and standard deviation.

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