Grade by Grade
D. Statistical Analysis · Two-Variable Data Analysis and Validity

The Line of Best Fit by Least Squares

D2.4About 75 minutes
Learning goals
  • I can explain what "least squares" means and why the squares of the residuals are used.
  • I can calculate the equation of the least-squares line y = ax + b by hand from a table of sums.
  • I can find the line from r, the means and the standard deviations.
  • I can interpret the slope and y-intercept in context and use the line to predict.

Words to know

line of best fit
A straight line that models the trend in a scatter plot.
residual
Actual y minus predicted y: e = y − ŷ. Positive for points above the line, negative below.
least-squares (regression) line
The one line that makes the sum of the squared residuals, Σ(y − ŷ)², as small as possible.
ŷ ("y-hat")
The value of y predicted by the model for a given x.
slope (a)
The predicted change in y for each increase of 1 unit in x.
y-intercept (b)
The predicted value of y when x = 0. It has a sensible meaning only if x = 0 makes sense in context.

Everyone's line is different

Give five people the rink data and a ruler, and you will get five slightly different lines of best fit. For predictions that everyone can check, we need one line chosen by a clear rule. The rule used in almost every field (economics, biology, sports analytics, machine learning) is least squares.

Remember from Grade 9: you drew lines of best fit by eye, found their equations, used technology to get the regression line, and met residuals: residual = actual y − predicted y. This lesson shows the formulas behind the technology.

Why squares?

For any line, each point has a residual e = y − ŷ, the vertical gap between the point and the line. We want the residuals small overall. But simply adding them does not work: positive and negative residuals cancel. In fact every line through the centre point (x̄, ȳ) has residuals that add to 0, whatever its slope!

So we square each residual (making them all positive, and punishing big misses much more than small ones) and choose the line that makes

Σ(y − ŷ)², the sum of squared residuals,

as small as possible. Picture each residual as the side of a real square: the least-squares line is the one with the smallest total area of squares.

01234567891055606570758085Hours of study (x)Test mark, % (y)Least squares: make the total area of the squares as small as possibleŷ = 3.6x + 50.53
Each yellow square has a side equal to one residual. Tilting or shifting the line would make some squares smaller but others bigger; the least-squares line gives the smallest possible total area.

The formulas

Using calculus (which you will meet in university, and in the Grade 12 course Calculus and Vectors), one can show that the least-squares line ŷ = ax + b has

a = [nΣxy − (Σx)(Σy)] ÷ [nΣx² − (Σx)²]

b = ȳ − a·x̄

Notice that the numerator of a is the same as the numerator of r, and the denominator is the first bracket from r. So if you have already calculated r, you have most of the work done.

The formula for b says the line always passes through the centre of the data, (x̄, ȳ).

There is also a neat link to r: a = r · (s_y ÷ s_x). The slope has the same sign as r, and a stronger correlation (bigger |r|) means a steeper line for the same spreads.

Worked example

Example 1: Study time

Find the least-squares line for the study-time data (n = 6, Σx = 33, Σy = 422, Σxy = 2456, Σx² = 219). Predict the mark for 7 hours of study.

  1. a = [6(2456) − 33(422)] ÷ [6(219) − 33²] = 810 ÷ 225 = 3.6.
  2. x̄ = 33 ÷ 6 = 5.5 and ȳ = 422 ÷ 6 ≈ 70.333.
  3. b = 70.333 − 3.6(5.5) = 70.333 − 19.8 ≈ 50.53.
  4. The line is ŷ = 3.6x + 50.53.
  5. For x = 7: ŷ = 3.6(7) + 50.53 = 25.2 + 50.53 ≈ 75.7.

ŷ = 3.6x + 50.53. Each extra hour of study is associated with about 3.6 more percentage points, on average. A student who studies 7 h is predicted to score about 76%. The intercept (50.5% for 0 hours) is outside the data (2 to 9 h), so treat it with caution.

Worked example

Example 2: Rink data

For the rink data, n = 12, Σx = −66, Σy = 840, Σxy = −5654, Σx² = 800. Find the least-squares line and interpret the slope and intercept.

  1. a = [12(−5654) − (−66)(840)] ÷ [12(800) − (−66)²] = −12 408 ÷ 5244 ≈ −2.366.
  2. x̄ = −66 ÷ 12 = −5.5 and ȳ = 840 ÷ 12 = 70.
  3. b = 70 − (−2.366)(−5.5) = 70 − 13.014 ≈ 56.99.
  4. ŷ = −2.37x + 56.99.

Slope: for each 1 °C rise in the daily high, the canteen sells about 2.4 fewer cups, on average. Intercept: on a day with a high of 0 °C, the model predicts about 57 cups. Here x = 0 is inside the data range (−16 °C to 4 °C), so the intercept has a real meaning.

−18−15−12−9−6−303630405060708090100Daily high temperature (°C)Cups of hot chocolate soldRink data with the least-squares linecentre (x̄, ȳ) = (−5.5, 70)run 3rise ≈ −7.1ŷ = −2.37x + 56.99
The least-squares line passes through the centre point (x̄, ȳ) = (−5.5, 70). The slope triangle shows a fall of about 7.1 cups for every 3 °C warmer.
Worked example

Example 3: From r and the summary statistics

For 50 adults, height x has x̄ = 170 cm and s_x = 8 cm; mass y has ȳ = 68 kg and s_y = 10 kg; r = 0.6. Find the least-squares line and predict the mass of a person 180 cm tall.

  1. a = r · s_y ÷ s_x = 0.6 × 10 ÷ 8 = 0.75 kg per cm.
  2. b = ȳ − a·x̄ = 68 − 0.75(170) = 68 − 127.5 = −59.5.
  3. ŷ = 0.75x − 59.5. For x = 180: ŷ = 135 − 59.5 = 75.5 kg.

ŷ = 0.75x − 59.5; predicted mass about 75.5 kg. The intercept (−59.5 kg at 0 cm) is meaningless: x = 0 is far outside the data. It only positions the line.

Technology and notation

  • Desmos: y_1 ~ a x_1 + b gives a, b and r (and r²).
  • Spreadsheet: =SLOPE(B2:B13, A2:A13) and =INTERCEPT(B2:B13, A2:A13). Note the y-values come first.
  • Some books write the line as y = mx + b or ŷ = b₀ + b₁x. The meaning is the same: slope and intercept.
  • Keep at least 3 or 4 significant digits in a and b; rounding the slope too early makes predictions drift.
Just for fun

The method of least squares was published by Adrien-Marie Legendre in 1805. Carl Friedrich Gauss later said he had been using it since 1795, which started one of the best-known "I did it first!" arguments in the history of mathematics.

Hands-on activity

Predict a height from an arm span

You need: your arm-span and height data from the first lesson (or the sample family: arm span 152, 160, 165, 171, 178, 183 cm; height 155, 158, 168, 170, 180, 181 cm), a calculator, and a spreadsheet or Desmos if you have one.

  1. Using arm span as x, find a and b by hand with the formulas.
  2. Write the equation and interpret the slope in a sentence.
  3. Measure the arm span of someone who was not in your data (a neighbour, a friend). Predict their height, then measure it. How close were you?
  4. Check your a and b with technology.

What to look for: for the sample family, ŷ ≈ 0.92x + 14.3 (a ≈ 0.918, b ≈ 14.3). A good prediction is usually within a few centimetres. The slope is close to 1 because height and arm span are close to equal for most adults.

Your turn

1

Find the least-squares line for (1, 4), (2, 6), (3, 7), (4, 10), (5, 11).

2

Use your line from the previous question to predict y when x = 3.5.

3

Find the least-squares line for (10, 9), (20, 7), (30, 8), (40, 4), (50, 2).

4

Show that the line in the previous question passes through (x̄, ȳ).

5

A model for a phone plan is ŷ = 0.05x + 25, where x is megabytes over the limit and ŷ is the monthly bill in dollars. Interpret the slope and intercept.

6

For a data set, r = −0.9, s_x = 2 and s_y = 5. Find the slope of the least-squares line.

7

Why can't we choose the line that makes the plain sum of residuals Σ(y − ŷ) as small as possible?

More practice: warm-up

1

A point is (4, 20) and the line predicts ŷ = 17. Find the residual.

2

Is a point below the line's residual positive or negative?

3

Through which point does every least-squares line pass?

4

If r is negative, what is the sign of the slope a?

5

ŷ = 2x + 3. Predict y for x = 10.

More practice: on your own

1

Find the least-squares line for (3, 20), (5, 18), (6, 15), (8, 14), (9, 10), (11, 9).

2

Find the least-squares line for (1, 2), (2, 3), (3, 5), (4, 6), (5, 9).

3

A car's value is modelled by ŷ = −2400x + 31 000, with x = age in years for cars aged 1 to 8 years. Interpret the slope. Should you use the model for a 15-year-old car?

4

For 40 students, x̄ = 72 (Grade 11 math mark), s_x = 9, ȳ = 70 (Grade 12 mark), s_y = 12, r = 0.75. Find the line and predict the Grade 12 mark of a student with 80 in Grade 11.

5

Explain why the slope of the least-squares line and r always have the same sign.

Challenge

1

For the study-time data, the line for predicting x from y is x̂ = 0.266y − 13.2 (approximately). Rearrange it to the form y = ... and compare with ŷ = 3.6x + 50.53. Are they the same line? Why not?

2

Show that for any data set, the sum of the residuals from the least-squares line is 0.

3

If every x-value is increased by 10 (for example, temperatures measured from a different starting point), what happens to a and b?

Check your understanding

Quiz

1.

The least-squares line minimizes:

2.

nΣxy − ΣxΣy = 300 and nΣx² − (Σx)² = 150. The slope is:

3.

x̄ = 8, ȳ = 30 and a = 2.5. What is b?

4.

ŷ = −1.8x + 95 models percent battery left (y) after x hours of video. What does −1.8 mean?

5.

r = 0.5, s_x = 4 and s_y = 12. The slope of the least-squares line is:

6.

A model has x = years of experience (from 2 to 20 in the data). When is the y-intercept meaningful?

7.

Which point always lies on the least-squares line?

For parents and teachers

Big idea (D2.4): the least-squares line is the one line that makes the total of the squared vertical gaps (residuals) as small as possible. Its slope is a = [nΣxy − ΣxΣy] ÷ [nΣx² − (Σx)²] and it always passes through (x̄, ȳ).

How to help: Ask your teen to read the slope of a model aloud as a sentence with units: "for each extra hour, the mark goes up about 3.6 points, on average." Ask whether the intercept makes sense in the situation.

Common difficulties:

  • Using r as the slope.
  • Rounding a too early, so b and predictions drift.
  • Forgetting to use ȳ − a·x̄ (some students write b = ȳ − x̄).
  • Interpreting an intercept that lies far outside the data.
  • In spreadsheets, putting the x-range first in SLOPE (it is SLOPE(y-range, x-range)).