← Course home
MODULE 9 · FIRST-YEAR LEVEL

Linear regression and prediction

How does a line summarise an association in data?

70–90 minutesConcepts first, R optionalSynthetic data

Learning outcomes

Conceptual lesson

Simple linear regression uses Y = β0 + β1 X + error to model a conditional mean outcome. The slope β1 describes the expected modelled change in Y per one-unit increase in X; in observational data it is not automatically a causal effect. The intercept β0 is the predicted value at X=0 and may lack a useful interpretation when zero is outside the observed range.

Residuals are observed minus fitted values. Examine linearity, independence, approximately constant error variance and any distributional assumptions needed for inference. R² summarises the fraction of sample outcome variability explained by the fitted model; it does not validate causality or performance on unseen data.

Predictions far beyond the observed X range are extrapolations. Low training error or high R² alone cannot guarantee dependable real-world decisions. Distinguish an interval for the mean response from an interval for a new individual outcome.

Worked research example

Fit `lm(score_post ~ study_hours)` on the fictional data. Limit your slope claim to “under this model in these data”, and avoid unsupported extrapolation to 20 study hours.

Common reasoning trap

A high R² does not show that the model is true, a cause is discovered, or future individual scores can be predicted with certainty.

Independent practice

  1. Interpret the regression slope with units of score points per hour.
  2. Inspect the residual-versus-fitted plot for curvature or changing spread.
  3. Predict a score for an hour value within the observed range; acknowledge uncertainty.

Explain your reasoning, not only the number.

Optional R lab

Save the synthetic CSV in the working folder before running the script. Base R only.

CSV · Download R script

# Statistics Lab / ferdo — synthetic data (no real student records)
# Save stats_synthetic.csv in your working directory.
d <- read.csv("stats_synthetic.csv", stringsAsFactors = TRUE)
stopifnot(nrow(d) == 36, all(c("score_pre","score_post","group") %in% names(d)))
# Groups are observational labels: differences cannot establish a treatment effect.

m <- lm(score_post ~ study_hours, data=d)
summary(m)
plot(m, which=1)
predict(m, newdata=data.frame(study_hours=4),
        interval="prediction")
range(d$study_hours)

Stretch: mathematical track

Explain why controlling for confounding in multiple regression requires more than adding variables mechanically.

Exit ticket

Write one defensible claim and one limitation from this week's example.

Student workbook for this module

Four written problems, marking guide and a formative four-question quiz.

Open week 9 workbook →

Optional mathematical derivations

Extend this topic with worked mathematics, written exercises and base R checks.

Math M8 · Full math pathway