Least Squares Regression

Category: Statistics   Level: 301

ŷ = a + bx; b = Sxy / Sxx = 168/42 = 4

The least squares line passes through the center of the data and makes the total of the squared vertical misses, the residuals, as small as possible.

Step through it

Watch it

Animated walkthrough

Step by step

Step 1: Plot the data

Each dot is one student: hours studied across, test score up. The dots drift upward, so more study goes with higher scores, on average.

n = 8 students

Step 1: Plot the data

Step 2: Mark the center of the data

Average the x values and the y values. The best line always passes through this center point.

mean of x = 4.5; mean of y = 64

Step 2: Mark the center of the data

Step 3: Draw the least squares line

The slope compares how x and y move together with how x moves alone. Then the line goes through the center.

b = 168/42 = 4, a = 64 - 4 · 4.5 = 46

Step 3: Draw the least squares line

Step 4: Measure the residuals

A residual is the vertical miss: actual minus predicted. Dots above the line miss high (green), dots below miss low (red), and they add to zero.

residual = y - ŷ; ∑ residuals = 0

Step 4: Measure the residuals

Step 5: Square the residuals and add

Draw each residual as a square. Least squares makes the total area as small as it can be. Tilt the line steeper through the center and the total jumps from 84 to 126.

SSE = ∑ (y - ŷ)2 = 84

Step 5: Square the residuals and add

Step 6: Predict with the line

For a student who studies 9 hours, read the line at x = 9. Predictions past the data are guesses, so trust them less the farther you go.

ŷ = 46 + 4 × 9 = 82

Step 6: Predict with the line

All steps at once

Storyboard

Generate it yourself

cd svg-generator
python3 -m svgmath render linear-regression --out /tmp/linear-regression

Source: svgmath/diagrams/statistics.py