← back to statistics

Simple Linear Regression

OpenIntro Statistics · CC BY-SA 3.0 · Chapter 8

Regression fits a line y = b0 + b1*x to data. The least squares method minimizes the sum of squared residuals. R-squared measures the fraction of variance explained. Inference for the slope measures uncertainty about the linear association under the model’s assumptions; it does not establish causation.

x y fitted line residuals

How close can your line get?

Each dot is an invented observation. Move the line to make the vertical gaps small. Those gaps are residuals: observed value minus predicted value.

Predict y from x · dashed vertical lines are residuals
Observations, fitted line, and residuals; exact values are in the table.00551010xy

Sum of squared residuals: 23.00. Best possible for these points: 4.73.

Adding a point keeps your current line. Predict how the best line will change, then fit again. Axes stay fixed; parts of a trial line outside the plot are clipped.

Read predictions and residuals
xObserved yPredicted yResidual
122.50-0.50
243.001.00
333.50-0.50
454.001.00
554.500.50
685.003.00
775.501.50
896.003.00
The line you have built

ŷ = b₀ + b₁x. The hat on y marks a prediction. b₀ is the intercept; b₁ is the slope. Your current line is ŷ = 2.00 + (0.50)x. Least squares chooses the coefficients that minimize the sum of squared residuals, ∑(y − ŷ)².

A good fit does not establish causation. Extrapolating beyond observed x values needs additional justification.

A moment to think

One prediction is 3 units too high and another is 3 too low. Is the fit perfect because the errors cancel?

Least squares line

The slope b1 = r * (sy / sx) and the intercept b0 = y-bar - b1 * x-bar. These minimize the sum of squared residuals. The correlation r measures linear association; the regression line passes through (x-bar, y-bar).

Scheme

Residuals

A residual is the observed value minus the predicted value: e = y - y-hat. Residuals should show no pattern when plotted against x or y-hat. Patterns indicate the linear model is missing something: curvature, heteroscedasticity, or outliers.

Scheme

R-squared

R-squared = 1 - SSE/SST, where SST is the total sum of squares. It measures the proportion of variance in y explained by x. An R-squared of 0.95 means the regression captures 95% of the variability. It equals the square of the correlation coefficient r.

Scheme

Inference for the slope

To test H0: beta1 = 0 (no linear relationship), compute t = b1 / SE(b1), where SE(b1) = sqrt(MSE / SS_xx). This follows a t-distribution with n - 2 degrees of freedom. The confidence interval is b1 +/- t* * SE(b1).

Scheme

Prediction intervals

A confidence interval targets the mean response at x. A prediction interval targets a single new observation. The prediction interval is wider because it adds individual-level noise on top of estimation uncertainty: SE_pred = sqrt(MSE * (1 + 1/n + (x - x-bar)^2 / SS_xx)).

Scheme

Notation reference

Symbol Formula Meaning
b1r * sy/sxSlope of regression line
b0ȳ - b1*x̄Intercept
ey - ŷResidual
R²1 - SSE/SSTCoefficient of determination
SE(b1)√(MSE/SS_xx)Standard error of slope
Neighbors

Cross-references

  • Linear Algebra Ch.1 — solving systems of equations, the algebraic backbone of least squares
  • 🤖 ML Ch.2 — linear regression as a machine learning problem: gradient descent vs. the normal equation
  • ∫ Calculus Ch.12 — least-squares minimization uses the same optimization techniques

Foundations (Wikipedia)