Statistics — Inferential Methods

Linear Regression
Fitting a Line Through Data

Given a scatter of data points, find the single straight line that best captures their relationship. From the least squares principle to R², residuals, significance tests, and confident prediction — regression is the workhorse of applied statistics.

2Parameters to Estimate
10Worked Examples
Goodness of Fit
10Quiz Questions
Share this page

§ 01What Is Linear Regression?

Linear regression models the relationship between a response variable y and one or more predictor variables x by fitting a straight line (or hyperplane) through the data. The goal is both to understand the relationship and to make predictions.

Consider predicting a student’s exam score from the number of hours studied, or a house’s sale price from its floor area. In each case, there is a quantitative response y and a quantitative predictor x, and we want to describe how y tends to change as x changes. Linear regression provides a precise, testable, and predictive answer.

This page covers simple linear regression — one predictor, one response. The ideas extend naturally to multiple regression (several predictors), but simple regression contains every key concept and is the essential foundation.

The Simple Linear Regression Model
y = β0 + β1x + ε
β0 = intercept  ·  β1 = slope  ·  ε = random error term  ·  ε ∼ N(0, σ²)

The parameters β0 and β1 are unknown population values. We estimate them from data using sample estimates b0 and b1, giving the fitted line ŷ = b0 + b1x (read “y-hat”).

§ 02The Least Squares Criterion

With n data points (x1, y1), …, (xn, yn), infinitely many lines could be drawn through or near the scatter. We need a unique, optimal choice. The least squares method selects the line that minimises the sum of squared residuals (SSE):

Least Squares Objective
SSE = ∑i=1n (yi − ŷi)2 = ∑i=1n (yi − b0 − b1xi)2
Minimise over b0 and b1. Each residual eᵢ = yᵢ − ŷᵢ is the vertical distance from a point to the line.

Deriving the Normal Equations

Minimise SSE by taking partial derivatives with respect to b0 and b1 and setting both to zero. This yields the normal equations:

Normal Equations
∑yi = nb0 + b1∑xi
∑xiyi = b0∑xi + b1∑xi2

Solving these two simultaneous equations gives the unique least squares estimates.

The Closed-Form Solution

Solving the normal equations algebraically produces compact formulas for the slope and intercept that can be computed directly from the data.

Least Squares Estimates
b1 = Sxy / Sxx = ∑(xi − x̄)(yi − ȳ) / ∑(xi − x̄)2

b0 = ȳ − b1
x̄ = sample mean of x  ·  ȳ = sample mean of y  ·  The fitted line always passes through (x̄, ȳ).

A computationally convenient equivalent for Sxy and Sxx avoids computing deviations explicitly:

Computational Shortcut
Sxx = ∑xi2 − n x̄2
Sxy = ∑xiyi − n x̄ȳ

These are faster to compute in a summary table and give identical results.

Example 01 Computing b₁ and b₀ from a small dataset

Five students studied x hours and scored y marks. Find the least squares regression line.

ixᵢ (hours)yᵢ (marks)xᵢ²xᵢyᵢ
1140140
22504100
33559165
446516260
557025350
Σ1528055915
Means
x̄ = 15/5 = 3,   ȳ = 280/5 = 56
Sxx, Sxy
Sxx = 55 − 5×3² = 55 − 45 = 10 Sxy = 915 − 5×3×56 = 915 − 840 = 75
Slope, Intercept
b1 = 75/10 = 7.5    b0 = 56 − 7.5×3 = 56 − 22.5 = 33.5
Fitted Line
ŷ = 33.5 + 7.5x

§ 03Interpreting the Coefficients

Getting numbers for b0 and b1 is straightforward. Understanding what they mean in context is equally important.

Slope b1
Δŷ per unit Δx For every one-unit increase in x, the predicted y changes by b1 units. In Example 01, each extra hour of study predicts 7.5 more marks. Always state the units.
Intercept b0
Predicted y when x = 0 b0 is the y-value where the line crosses the y-axis (x = 0). Often uninterpretable in context (e.g., a student studying 0 hours scoring 33.5 is plausible here, but negative intercepts can be nonsensical).
Extrapolation Warning
Only predict within [xmin, xmax] The fitted line is only trustworthy within the observed range of x. Predicting far outside this range (extrapolation) can produce wildly inaccurate results if the linear trend doesn’t continue.
Example 02 Interpreting slope and intercept — house prices

A regression of house price y (in £1000s) on floor area x (m²) gives ŷ = 120 + 2.4x.

Slope

For each additional m² of floor area, the predicted price increases by £2,400 (b1 = 2.4 in units of £1000/m²).

Intercept

A house with 0 m² would be predicted to cost £120,000 — a physically meaningless extrapolation (no house has zero area). The intercept is a mathematical anchor, not an interpretable quantity here.

Predict
For x = 80 m²: ŷ = 120 + 2.4×80 = 120 + 192 = £312,000
Prediction
An 80 m² house is predicted to sell for approximately £312,000.

§ 04Residuals — Measuring What the Model Misses

The residual for observation i is the difference between the actual and fitted value: ei = yi − ŷi. Residuals carry all the information the model did not capture. Analysing them reveals whether the model’s assumptions hold.

Key Properties of Residuals
∑ ei = 0    (residuals sum to zero)
∑ xiei = 0    (residuals are uncorrelated with x)
The fitted line passes through (x̄, ȳ)

These three properties hold for every least squares regression, by construction.

The Assumptions to Check

What We Assume

  • Linearity: the true relationship is linear
  • Independence: observations are independent
  • Normality: errors εi ∼ N(0, σ²)
  • Homoscedasticity: constant variance σ²

How to Check

  • Plot residuals vs fitted values (look for curves or fans)
  • Normal Q-Q plot of residuals (check for straight line)
  • Scale-location plot (check variance is constant)
  • Residuals vs order plot (check independence over time)
Example 03 Computing and checking residuals

Using the fitted line ŷ = 33.5 + 7.5x from Example 01, compute the residuals.

xᵢyᵢŷᵢ = 33.5 + 7.5xᵢeᵢ = yᵢ − ŷᵢeᵢ²
14041.0−1.01.00
25048.5+1.52.25
35556.0−1.01.00
46563.5+1.52.25
57071.0−1.01.00
Σ0.0SSE = 7.50
Verification
∑ eᵢ = −1 + 1.5 − 1 + 1.5 − 1 = 0 ✓    SSE = 7.50

§ 05R² — The Coefficient of Determination

R² measures what fraction of the total variation in y is explained by the regression on x. It is the single most widely used summary of regression fit.

Partitioning Total Variation
SST = SSR + SSE

R² = SSR / SST = 1 − SSE / SST
SST = ∑(yᵢ − ȳ)² (Total)  ·  SSR = ∑(ŷᵢ − ȳ)² (Regression)  ·  SSE = ∑(yᵢ − ŷᵢ)² (Error)

R² always lies between 0 and 1. An R² of 0.85 means 85% of the variability in y is explained by the linear relationship with x. The remaining 15% is unexplained scatter around the line.

R² = 0
No linear relationship The regression line explains none of the variation. The fitted line is horizontal (b1 = 0). Predicting ȳ is as good as the regression.
R² = 1
Perfect linear fit All points lie exactly on the line. Every SSE = 0. This almost never happens in real data and may indicate a tautological relationship or data entry error.
R vs R²
R = sign(b1) × √R² R is the sample correlation coefficient between x and y. R² is its square. For simple linear regression only, R = |r| and they carry the same information.
Example 04 Computing R² for the hours-vs-marks data
SST
Syy = ∑yi² − nȳ² ∑yi² = 40²+50²+55²+65²+70² = 1600+2500+3025+4225+4900 = 16250 SST = 16250 − 5×56² = 16250 − 15680 = 570
SSE
SSE = 7.50  (from Example 03)
R² = 1 − 7.50/570 = 1 − 0.01316 ≈ 0.987
Interpretation
R² ≈ 0.987: 98.7% of the variation in marks is explained by hours studied. Excellent fit.

§ 06Standard Error and Testing the Slope

Even with a strong-looking fit, we must ask: could the apparent slope have arisen by chance from a population where β1 = 0? The significance test for the slope answers this directly.

Estimating σ²

The population error variance σ² is estimated by the mean squared error (MSE), using n − 2 degrees of freedom because two parameters (β0, β1) were estimated from the data.

Mean Squared Error and Standard Error of Regression
s² = MSE = SSE / (n−2)     s = √(SSE/(n−2))

s is called the residual standard error — it measures the typical size of a residual.

Standard Error of b1 and the t-Test

t-Test for H₀: β₁ = 0
SE(b1) = s / √Sxx

t = b1 / SE(b1)  ∼  tn−2
If H₀: β₁ = 0 is rejected, the linear predictor is statistically significant.
Example 05 Testing significance of the slope

Using the hours-vs-marks example: SSE = 7.50, n = 5, Sxx = 10, b1 = 7.5. Test H0: β1 = 0 at α = 0.05.

MSE
s² = 7.50 / (5−2) = 7.50/3 = 2.50  →  s = √2.50 ≈ 1.581
SE(b1)
SE(b1) = 1.581 / √10 = 1.581 / 3.162 ≈ 0.500
t-statistic
t = 7.5 / 0.500 = 15.00  (df = 3)
Critical Value
t0.025, 3 = 3.182  (two-tailed, 3 df)
Decision
t = 15.00 ≫ 3.182 → Reject H0. The slope is highly significant; hours studied is a significant predictor of marks.
Example 06 Confidence interval for the slope β₁

Construct a 95% confidence interval for β1 using the values from Example 05.

Formula
b1 ± tα/2, n−2 × SE(b1)
Values
7.5 ± 3.182 × 0.500 = 7.5 ± 1.591
95% CI for β₁
(5.909, 9.091) — we are 95% confident the true slope lies in this interval.

§ 07Prediction — Point Estimates and Intervals

A regression line can answer two types of prediction questions, which require different intervals even though both start from the same point estimate ŷ* = b0 + b1x*.

Confidence Interval for the Mean

Estimates the mean response at x = x*. Narrower because it targets a population average.

ŷ* ± tα/2 × s√[1/n + (x*−x̄)²/Sxx]

Prediction Interval for a Single Observation

Estimates where a new individual observation at x = x* will fall. Wider because it includes individual variability.

ŷ* ± tα/2 × s√[1 + 1/n + (x*−x̄)²/Sxx]

Key Distinction Both intervals are widest at the extremes of x and narrowest at x = x̄. The prediction interval is always wider than the confidence interval, since individual observations scatter around the mean response. Confusing the two is one of the most common errors in applied regression.
Example 07 Point prediction and 95% prediction interval

For x* = 3.5 hours, using ŷ = 33.5 + 7.5x, s = 1.581, n = 5, x̄ = 3, Sxx = 10. Compute the 95% prediction interval.

Point Estimate
ŷ* = 33.5 + 7.5×3.5 = 33.5 + 26.25 = 59.75 marks
SE Factor
s√[1 + 1/5 + (3.5−3)²/10] = 1.581×√[1+0.2+0.025] = 1.581×√1.225 = 1.581×1.107 ≈ 1.750
Interval
59.75 ± 3.182 × 1.750 = 59.75 ± 5.57
95% Prediction Interval
(54.18, 65.32) — a new student studying 3.5 hours is 95% likely to score in this range.

§ 08The ANOVA Table for Regression

Regression results are commonly presented in an Analysis of Variance (ANOVA) table, which partitions SST into SSR and SSE, computes F-statistics, and gives a significance test for the whole model. For simple linear regression, the F-test is equivalent to the t-test on b1, but the ANOVA framework generalises to multiple regression.

SourceSSdfMSF
RegressionSSR1SSR/1MSR/MSE
Error (Residual)SSEn−2SSE/(n−2)
TotalSSTn−1

Under H0: β1 = 0, the F-statistic follows an F-distribution with 1 and n−2 degrees of freedom. Reject H0 if F > Fcrit.

Example 08 Complete ANOVA table for the hours-vs-marks data
Known
SST = 570,   SSE = 7.50,   n = 5
SSR
SSR = SST − SSE = 570 − 7.50 = 562.50
MS & F
MSR = 562.50,   MSE = 7.50/3 = 2.50,   F = 562.50/2.50 = 225.0
F-test
F = 225.0 ≫ F0.05, 1, 3 = 10.13 → Reject H0. The model is highly significant (p < 0.001).

§ 09Correlation vs Regression — and Common Mistakes

Correlation and regression both measure linear association, but they answer different questions and should not be confused.

Pearson Correlation r

  • Symmetric: r(x,y) = r(y,x)
  • Dimensionless, always −1 ≤ r ≤ 1
  • Measures strength and direction only
  • No distinction between x and y
  • Does not allow prediction

Regression

  • Asymmetric: y on x ≠ x on y
  • Slope has units of y per x
  • Measures how much y changes per unit x
  • x is the predictor, y is the response
  • Enables prediction and causal modelling
Correlation ≠ Causation
High r does not mean x causes y Ice cream sales and drowning rates are correlated (both increase in summer), but ice cream doesn’t cause drowning. A third variable (hot weather) drives both.
Influential Points
High leverage + outlier = problem A single outlier with a large x-value (high leverage) can drag b1 drastically. Always plot your data and check Cook’s distance for influential observations.
R² Isn’t Everything
Anscombe’s Quartet Four datasets with identical R², b0, and b1 can look completely different. Always plot the data; residual analysis often reveals more than R².
Omitted Variable Bias
Missing x → biased b1 If an important predictor is left out of the model and is correlated with x, the slope b1 absorbs its effect and becomes biased. This motivates multiple regression.
Example 09 Computing the correlation coefficient r from regression quantities

Using Sxy = 75, Sxx = 10, Syy = SST = 570 from the hours-vs-marks data.

Formula
r = Sxy / √(Sxx × Syy)
Compute
r = 75 / √(10 × 570) = 75 / √5700 = 75 / 75.50 ≈ 0.9934
Result
r ≈ 0.993  →  R² = r² ≈ 0.987 ✓   Strong positive linear relationship.
Example 10 Regression from scratch — temperature vs ice cream sales

Daily temperature x (°C) and ice cream sales y (units): (20, 80), (22, 90), (25, 110), (28, 130), (30, 140). Find ŷ = b0 + b1x, R², and predict sales at 27°C.

Summaries
n=5,  ∑x=125,  ∑y=550,  ∑x²=3173,  ∑xy=13980 x̄=25,  ȳ=110
Sxx, Sxy
Sxx = 3173 − 5×625 = 3173 − 3125 = 48 Sxy = 13980 − 5×25×110 = 13980 − 13750 = 230
Coefficients
b1 = 230/48 ≈ 4.792    b0 = 110 − 4.792×25 = 110 − 119.79 = −9.79
Syy: ∑y² = 6400+8100+12100+16900+19600 = 63100,  Syy=63100−5×12100=2100 SSE = Syy − b1Sxy = 2100 − 4.792×230 ≈ 2100 − 1102.2 = 997.8 R² = 1 − 997.8/2100 ≈ 0.525
Prediction
x* = 27:  ŷ = −9.79 + 4.792×27 = −9.79 + 129.38 ≈ 119.6 units
Summary
ŷ = −9.79 + 4.79x,  R² = 0.525,  Predicted sales at 27°C: ~120 units

§ 10Summary and Decision Guide

Linear regression is a powerful, interpretable method for modelling the relationship between two quantitative variables. The entire procedure from data collection to inference rests on a small set of key quantities: Sxx, Sxy, Syy, SSE, and MSE.

The Complete Regression Workflow
1. Plot y vs x (always look first)
2. Compute x̄, ȳ, Sxx, Sxy, Syy
3. Estimate b1 = Sxy/Sxx,  b0 = ȳ − b1
4. Compute SSE, MSE, s
5. Test H0: β1 = 0 (t-test or F-test)
6. Compute R² = 1 − SSE/SST
7. Check residual plots for assumption violations
8. Predict (within x-range only) with appropriate interval
Always Plot First No statistical summary can replace a scatter plot. Before fitting any regression, visualise x against y. A non-linear pattern, a single influential outlier, or obvious heteroscedasticity are all immediately visible in a plot but completely hidden in summary statistics alone.

Continue the Statistics Series

§ 11Quiz — Test Your Understanding

Ten questions covering the full regression workflow. Select the best answer for each.

Linear Regression Quiz

Question 1 of 10
The least squares method finds b₀ and b₁ by minimising…
Question 2 of 10
Given Sxy = 120 and Sxx = 40, what is b₁?
Question 3 of 10
The fitted line ŷ = b₀ + b₁x always passes through…
Question 4 of 10
If SST = 500 and SSE = 75, what is R²?
Question 5 of 10
The MSE (mean squared error) in regression uses denominator…
Question 6 of 10
A regression of salary y on years of experience x gives b₁ = 2.5 (£1000/year). The correct interpretation is…
Question 7 of 10
Which interval is always wider: the 95% confidence interval for the mean response, or the 95% prediction interval for a new observation?
Question 8 of 10
It is always true that ∑ eᵢ = …
Question 9 of 10
A residual plot of eᵢ vs ŷᵢ shows a clear fan shape (variance increasing with fitted values). This violates which regression assumption?
Question 10 of 10
Two variables have a Pearson correlation of r = 0.9. Which statement is correct?
0 out of 10

Cookie Settings