Find a course

Module 2: Simple Linear RegressionLesson 3 of 1214 min

The Least-Squares Regression Line

Understand residuals and the least-squares criterion, compute the slope and intercept from summary statistics, and fit a line by hand to real numbers.

Step 1 of 12Learn

Ten people, ten different lines

Give ten people a scatterplot and a ruler and ask them to draw the line that best fits the points. You'll get ten different lines. Some will chase the outliers, some will run through the middle of the densest cluster, some will be steeper than others. If two analysts can't even agree on the line, they can't agree on predictions made from it.

Linear regression replaces eyeballing with a precise rule. It finds the single line that fits the data best according to a clear criterion, so everyone who runs the same data gets the same answer.

The line has the form

ŷ = b₀ + b₁x

where ŷ ("y-hat") is the predicted value of the response, b₀ is the intercept (the predicted y when x = 0) and b₁ is the slope (the change in predicted y for each one-unit increase in x).

We'll use a small example throughout this module. Five students practiced for 1, 2, 3, 4 and 5 hours before a 10-point quiz and scored 3, 5, 4, 7 and 8. The scatterplot rises from left to right with a correlation of about 0.92. The question is: which line describes that rise best, and how do we find it?

Step 2 of 12Learn

Residuals and the least-squares criterion

For each data point, the residual is the vertical distance between the actual value and the line's prediction:

residual = e = y − ŷ

A positive residual means the point sits above the line (the model underpredicted); a negative one means it sits below (the model overpredicted).

A good line should have small residuals overall. But we can't just add them up, because positive and negative residuals cancel: many terrible lines have residuals that sum to zero. Instead, regression squares each residual and adds them up. The least-squares regression line is the line that makes the sum of squared errors (SSE) as small as possible:

SSE = Σ(y − ŷ)²

Why squares? They make all distances positive, they penalize big misses much more than small ones (a residual of 4 counts 16 times as much as a residual of 1), and they lead to simple formulas for the best slope and intercept. The trade-off is that squaring makes the line sensitive to outliers: one point far from the rest pulls the line toward itself strongly.

In the explorer below, try to find the line with the smallest SSE for the five students yourself. Then you'll see the formulas that find it exactly.

Step 3 of 12Practice · Chart

Residuals from ŷ = 1.8 + 1.2x

Tap the student the line overpredicts by the most.

Tap the bar that answers the question, then press Check.

Residuals from ŷ = 1.8 + 1.2xBar chart with 5 categories: 1 h: 0; 2 h: 0.8; 3 h: -1.4; 4 h: 0.4; 5 h: 0.2.-1.5-1-0.500.51Residual1 h2 h3 h4 h5 hPractice hours00.8-1.40.40.2
The five-student data from the explorer below: scores 3, 5, 4, 7, 8. Squared residuals add up to SSE = 2.8.
Residuals from ŷ = 1.8 + 1.2x
Practice hoursResidual (y − ŷ)
1 h0
2 h0.8
3 h-1.4
4 h0.4
5 h0.2

Step 4 of 12Explore

Find the least-squares line

The data: practice hours 1, 2, 3, 4, 5 and quiz scores 3, 5, 4, 7, 8. Adjust the intercept and slope to make the sum of squared errors as small as you can. The smallest possible SSE is 2.8.

02.557.510 0246
Sum of squared errors
11
Prediction at 3 hours
6

Step 5 of 12Learn

Formulas for the slope and intercept

Calculus gives the line that minimizes SSE exactly. The slope is

b₁ = Sxy / Sxx = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)²

or, equivalently, in terms of the correlation and the standard deviations:

b₁ = r × (s_y / s_x)

and the intercept is

b₀ = ȳ − b₁x̄

For the five students: x̄ = 3, ȳ = 5.4, Sxy = 12 and Sxx = 10. So b₁ = 12 / 10 = 1.2 points per hour of practice, and b₀ = 5.4 − 1.2 × 3 = 1.8 points. The fitted line is

ŷ = 1.8 + 1.2x

The second formula for the slope shows how correlation and regression connect. The slope is the correlation, rescaled from standard-deviation units into the units of the data. If r = 0, the slope is 0 and the line is flat at ȳ. If r is positive, the line rises. The steepness depends on how spread out y is compared with x.

Notice that the intercept formula forces the line through the point (x̄, ȳ). Every least-squares line passes through the point of averages, which is a handy check on any calculation.

Step 6 of 12Practice · Diagram

The formulas on the five-student data

Which step guarantees the line passes through (x̄, ȳ)?

Tap the shape that answers the question, then press Check.

The formulas on the five-student dataProcess diagram: x̄ = 3, ȳ = 5.4 → Sxx = 10, Sxy = 12 → b₁ = 12 / 10 = 1.2 → b₀ = 5.4 − 1.2 × 3 = 1.8.x̄ = 3, ȳ = 5.4Sxx = 10, Sxy= 12b₁ = 12 / 10 =1.2b₀ = 5.4 − 1.2× 3 = 1.8
x̄ = 3, ȳ = 5.4
Sxx = 10, Sxy = 12

Sxx = Σ(x − x̄)2; Sxy = Σ(x − x̄)(y − ȳ).

b₁ = 12 / 10 = 1.2
b₀ = 5.4 − 1.2 × 3 = 1.8

Forces the line through (x̄, ȳ).

Step 7 of 12Practice · Work it out

In a dataset, the correlation between x and y is 0.8, the standard deviation of y is 10 and the standard deviation of x is 2. What is the slope of the least-squares line for predicting y from x?

Step 8 of 12Practice · Work it out

Continuing: the mean of x is 5 and the mean of y is 70, and the slope is 4. What is the intercept b₀?

Step 9 of 12Note

Step 10 of 12Learn

Using the line and reading residuals

With ŷ = 1.8 + 1.2x, a student who practices 3 hours has a predicted score of 1.8 + 3.6 = 5.4. The actual student who practiced 3 hours scored 4, so the residual is 4 − 5.4 = −1.4: that student did 1.4 points worse than the line predicts. The full set of residuals for the five students is 0, 0.8, −1.4, 0.4 and 0.2. They sum to zero, as they must, and their squares add up to SSE = 2.8, the minimum you hunted for in the explorer.

Residuals are more than leftovers. They tell you which observations the model explains poorly, and patterns in them reveal problems with the model, as you'll see later in this module.

One subtle point: the regression of y on x is not the same line as the regression of x on y. Predicting quiz score from practice minimizes vertical distances; predicting practice from score would minimize horizontal ones. Unless r = ±1, the two lines differ. Always regress the variable you want to predict on the variable you will know.

The name "regression" itself comes from the 1880s, when Francis Galton found that tall parents tend to have children who are tall but closer to average height. He called it regression toward mediocrity, the same regression to the mean you may have met elsewhere.

Step 11 of 12Practice · Work it out

Using the line ŷ = 1.8 + 1.2x, a sixth student practiced 6 hours and scored 8.5. What is this student's residual?

Step 12 of 12Practice · Choose one

What exactly does the least-squares method minimize?