Fitting a linear model
What this lesson covers
- The line is y = a + bx.
- The slope gives the change in y per unit of x.
- The line goes through the mean of x and the mean of y.
A small data set
- Five students from a made-up class.
| Hours studied, x | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Test mark, y | 52 | 55 | 61 | 64 | 68 |
Finding the line
- Calculators do this, but the working shows where it comes from.
- mean x = 3, mean y = 60
- sum of (x − 3)(y − 60) = 41
- sum of (x − 3)² = 10
- b = 41 ÷ 10 = 4.1
- a = 60 − 4.1 × 3 = 47.7
- y = 47.7 + 4.1x
Reading the slope
- Write it in context.
- Each extra hour is linked to about 4.1 more marks
The line through the means
- The least squares line always passes through the point (mean of x, mean of y).
Try it: move the line
- Drag a point up or down. The line follows, and it always stays as close as it can to all the points.
Statistics explorer
Reading the intercept
- In y = 47.7 + 4.1x, the 47.7 is the model's mark for 0 hours of study.
- x = 0 is outside the data, which runs 1 to 5
- 47.7 is a feature of the line, not a fact about students
Try it: the fitted line
Finding a from the means
- The mean of x is 6, the mean of y is 40 and b = 3.
- 40 = a + 3 × 6
- a = 22
- y = 22 + 3x
Try it: build the line
Common mistakes
- Swapping x and y.x is the explanatory variable.
- Saying each hour causes 4.1 marks.Say it is linked to, on average.
Your turn
For y = 20 + 3x, what is the slope?
For y = 20 + 3x, predict y when x = 5.
For y = 2 + 3x, the mean of x is 4. Find the mean of y.
A slope of −2 means what?
The line y = a + 2.5x passes through (mean of x, mean of y) = (8, 31). Find a.
Marks y and hours x (data 1 to 5 hours) give y = 47.7 + 4.1x. What does 47.7 mean?
The sum of (x − mean x)(y − mean y) is 36 and the sum of (x − mean x)² is 15. Find the slope b.
Mean x = 4, mean y = 30, sum of (x − 4)(y − 30) = −24 and sum of (x − 4)² = 12. Find the least squares line.
Show answer
1. 3 2. 35 3. 14 4. y falls by about 2 for each extra unit of x 5. 11. The line goes through the means, so 31 = a + 2.5 × 8 = a + 20, giving a = 11. 6. The model's mark for 0 hours, outside the data. x = 0 is not in the 1 to 5 range, so it is a feature of the line, not a fact about students. 7. 2.4. b = 36 ÷ 15 = 2.4. 8. y = 38 − 2x. b = −24 ÷ 12 = −2, and 30 = a − 2 × 4 gives a = 38.
Sample question: Achieved
Five students record minutes spent revising (x) and quiz score out of 30 (y): (2, 9), (4, 12), (6, 16), (8, 17), (10, 21). The least squares line is y = 6.3 + 1.45x. Interpret the slope and the intercept in context, and estimate the score for 5 minutes of revising.
Hint
The slope is the change in y for one more unit of x. The intercept is the value of y when x = 0; check whether 0 is inside the data.
Answer
Slope: each extra minute of revising is linked to about 1.45 more marks, on average. Intercept: the model gives 6.3 marks for no revising, but 0 minutes is outside the data (2 to 10), so this is not reliable. For 5 minutes the estimate is 6.3 + 1.45 × 5 ≈ 13.6 marks.
Step by step
- In y = a + bx, b is the slope and a is the intercept. Here b = 1.45 and a = 6.3.
- The slope is the change in y for each extra 1 of x. So each extra minute of revising goes with about 1.45 more marks, on average.
- Classic mistake: saying each minute causes 1.45 marks. The data show a link, not a cause, so say "is linked to" and "on average".
- The intercept is the y value when x = 0: 6.3 marks for no revising.
- But the data run from 2 to 10 minutes. x = 0 is outside that, so 6.3 is a feature of the line, not a reliable fact about students.
- Estimate for 5 minutes: substitute x = 5. y = 6.3 + 1.45 × 5 = 6.3 + 7.25 = 13.55, about 13.6 marks.
- 5 is inside 2 to 10, so this is interpolation, which is reasonable.
- Check: the actual scores at 4 and 6 minutes are 12 and 16, and 13.6 sits between them.
Full working
- b = 1.45slope: about 1.45 marks per extra minute, on average
- a = 6.3 at x = 0outside the data 2 to 10, unreliable
- y = 6.3 + 1.45 × 5 = 13.55x = 5 is inside the data
- About 1.45 marks per minute; intercept unreliable; about 13.6 marks for 5 minutes
Sample questions: Merit
Five students record minutes spent revising (x) and quiz score out of 30 (y): (2, 9), (4, 12), (6, 16), (8, 17), (10, 21). Calculate the least squares line by hand, using b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² and a = ȳ − b x̄.
Hint
Find the two means first, then the deviations from them. Multiply the deviations in pairs for the top, and square the x deviations for the bottom.
Answer
y = 6.3 + 1.45x
Step by step
- Means: x̄ = (2 + 4 + 6 + 8 + 10) ÷ 5 = 6 and ȳ = (9 + 12 + 16 + 17 + 21) ÷ 5 = 75 ÷ 5 = 15.
- x deviations, x − 6: −4, −2, 0, 2, 4.
- y deviations, y − 15: −6, −3, 1, 2, 6.
- Multiply in pairs: 24, 6, 0, 4, 24. Their sum is Σ(x − x̄)(y − ȳ) = 58.
- Square the x deviations: 16, 4, 0, 4, 16. Their sum is Σ(x − x̄)² = 40.
- Slope: b = 58 ÷ 40 = 1.45.
- Intercept: a = ȳ − b x̄ = 15 − 1.45 × 6 = 15 − 8.7 = 6.3.
- Classic mistake: dividing by Σ(y − ȳ)², which is 86. The bottom uses the x deviations only.
- So the line is y = 6.3 + 1.45x.
- Check: the line passes through the means. At x = 6, y = 6.3 + 8.7 = 15 = ȳ. A calculator's regression gives the same line.
Full working
- x̄ = 6, ȳ = 15
- Σ(x − x̄)(y − ȳ) = 24 + 6 + 0 + 4 + 24 = 58
- Σ(x − x̄)² = 16 + 4 + 0 + 4 + 16 = 40
- b = 58 ÷ 40 = 1.45
- a = 15 − 1.45 × 6 = 6.3a = ȳ − b x̄
- y = 6.3 + 1.45x
For a set of data, the mean of x is 12.5 and the mean of y is 41. A least squares line y = a + 2.4x is fitted. Find a, explain why the method works, and use the line to predict y when x = 15.
Hint
The least squares line always passes through the point (x̄, ȳ). Substitute it to find a.
Answer
a = 11, so y = 11 + 2.4x, because the line passes through (12.5, 41). At x = 15, y = 47.
Step by step
- Key fact: the least squares line always goes through the point of means, (x̄, ȳ).
- That is because a = ȳ − b x̄ is part of how the line is built. Rearranged, it says ȳ = a + b x̄.
- So substitute x = 12.5 and y = 41 into y = a + 2.4x: 41 = a + 2.4 × 12.5.
- 2.4 × 12.5 = 30, so 41 = a + 30 and a = 11.
- The line is y = 11 + 2.4x.
- Classic mistake: substituting a data point instead of the means. Data points are usually not on the line; the point of means always is.
- Prediction at x = 15: y = 11 + 2.4 × 15 = 11 + 36 = 47.
- Check: at x = 12.5 the line gives 11 + 30 = 41 = ȳ. Correct.
- One caution: the prediction is only sensible if 15 is inside the range of the x data.
Full working
- line passes through (x̄, ȳ) = (12.5, 41)
- 41 = a + 2.4 × 12.5 = a + 30
- a = 11, so y = 11 + 2.4x
- y = 11 + 2.4 × 15 = 47x = 15
- a = 11; y = 47 when x = 15
Sample questions: Excellence
Using a = ȳ − b x̄, prove that the residuals from any least squares line add to zero. Check it with the revision data (x) and quiz score out of 30 (y): (2, 9), (4, 12), (6, 16), (8, 17), (10, 21). and the line y = 6.3 + 1.45x.
Hint
A residual is y − (a + bx). Add them for all n points and replace a with ȳ − b x̄. Remember Σy = nȳ and Σx = n x̄.
Answer
Σ residuals = Σy − na − bΣx = nȳ − n(ȳ − b x̄) − b n x̄ = 0. For the data the residuals are −0.2, −0.1, 1, −0.9 and 0.2, which add to 0.
Step by step
- Each residual is actual − predicted: y − (a + bx).
- Add them over all n points: Σ(y − a − bx) = Σy − na − bΣx. The a is the same for every point, so it adds up to na.
- Use the means: Σy = nȳ and Σx = n x̄. So the sum is nȳ − na − b n x̄.
- Now replace a with ȳ − b x̄: nȳ − n(ȳ − b x̄) − b n x̄.
- Expand: nȳ − nȳ + b n x̄ − b n x̄ = 0. So the residuals always add to zero.
- Classic mistake: thinking this proves the line fits well. Any least squares line has residuals that add to zero, even a poor fit.
- Check with the data. Predicted values: 6.3 + 1.45 × 2 = 9.2, then 12.1, 15, 17.9 and 20.8.
- Residuals: 9 − 9.2 = −0.2, 12 − 12.1 = −0.1, 16 − 15 = 1, 17 − 17.9 = −0.9, 21 − 20.8 = 0.2.
- Sum: −0.2 − 0.1 + 1 − 0.9 + 0.2 = 0. It matches the proof.
- In words: the points above the line balance the points below it exactly, because the line goes through the point of means.
Full working
- Σ(y − a − bx) = Σy − na − bΣx
- = nȳ − na − b n x̄Σy = nȳ, Σx = n x̄
- = nȳ − n(ȳ − b x̄) − b n x̄ = 0a = ȳ − b x̄
- predicted: 9.2, 12.1, 15, 17.9, 20.8
- residuals: −0.2, −0.1, 1, −0.9, 0.2sum 0
- The residuals always add to zero; here −0.2 − 0.1 + 1 − 0.9 + 0.2 = 0
Five students record minutes spent revising (x) and quiz score out of 30 (y): (2, 9), (4, 12), (6, 16), (8, 17), (10, 21). For this data Σ(x − x̄)(y − ȳ) = 58, Σ(x − x̄)² = 40 and Σ(y − ȳ)² = 86. A student fits the line with x and y swapped, x = c + dy, then rearranges it to make y the subject. Will she get y = 6.3 + 1.45x again? Show the working, explain the difference, and say which line to use to predict a score.
Hint
With the roles swapped, the slope is Σ(x − x̄)(y − ȳ) ÷ Σ(y − ȳ)². Rearranging turns a slope d into 1d for y against x.
Answer
No. Her slope is d = 58 ÷ 86 ≈ 0.674, which rearranges to a y slope of 1d ≈ 1.483, not 1.45. Least squares on y makes vertical gaps small; on x it makes horizontal gaps small, so the lines differ unless r = ±1 (here 1.45 × 0.674 = r² ≈ 0.978). To predict a score from minutes, use y = 6.3 + 1.45x.
Step by step
- The y on x line makes the vertical gaps (the y residuals) as small as possible. Its slope is 58 ÷ 40 = 1.45.
- With x and y swapped, the line makes the horizontal gaps as small as possible. Its slope is d = 58 ÷ 86 ≈ 0.674.
- Both lines pass through the point of means (6, 15). So hers is x = 6 + 0.674(y − 15).
- Rearranging for y: y − 15 = (x − 6) ÷ 0.674, so the slope against x is 1 ÷ 0.674 ≈ 1.483.
- 1.483 is not 1.45, so the two lines are different, though both go through (6, 15).
- Classic mistake: thinking a line is a line, so it does not matter which variable is which. Least squares treats x and y differently.
- Link to r: 1.45 × 0.674 ≈ 0.978, and r² = 58² ÷ (40 × 86) ≈ 0.978. The two slopes multiply to r².
- So the lines only agree when r² = 1, a perfect straight line. The weaker the relationship, the further apart they are.
- To predict a quiz score from minutes, the gaps that matter are in the scores, so use y on x: y = 6.3 + 1.45x.
- Her line would be the right one only for predicting minutes from a score.
Full working
- b(y on x) = 58 ÷ 40 = 1.45
- d(x on y) = 58 ÷ 86 ≈ 0.674
- x = 6 + 0.674(y − 15), so y slope = 1 ÷ 0.674 ≈ 1.483rearranged
- 1.45 × 0.674 ≈ 0.978 = r²slopes multiply to r²
- lines agree only if r = ±1
- No: her line has slope 1.483; use y = 6.3 + 1.45x to predict scores
What to take away
- Slope in context, with units.
- The line passes through the means.
Challenge
A Taupō café owner fits a least squares line to 30 winter and spring days: hot drinks sold, y cups, against the noon temperature, C °C. The temperatures run from 6 to 22 °C and the line is y = 260 − 9C. Her business partner, using the same days, records temperature in degrees Fahrenheit (F = 1.8C + 32) and sales in tens of cups, and fits her own least squares line. Without the data, find the partner's line, interpret its slope in context, and show that both lines give the same prediction for a 20 °C day. Is the partner's intercept useful?
Hint
Changing units only relabels the axes: every point and the owner's line move together. Write C in terms of F, substitute into the owner's line, then divide by 10 for tens of cups.
Answer
Sales (tens of cups) = 42 − 0.5F. Each extra degree Fahrenheit is linked to about half a ten (5 cups) fewer hot drinks, on average. At 20 °C (68 °F) both lines predict 80 cups. The intercept, 42 tens at 0 °F (about −17.8 °C), is far outside the data and is not useful.
Step by step
- Changing units stretches and shifts the axes in the same way for every point and for the line, so the line with the smallest squared vertical gaps is still the same line, just relabelled.
- From F = 1.8C + 32, C = (F − 32) ÷ 1.8, so substitute that into y = 260 − 9C.
- 9 ÷ 1.8 = 5, so y = 260 − 5(F − 32) = 420 − 5F cups, which is 42 − 0.5F in tens of cups.
- The slope −0.5 means half a ten, or 5 cups, fewer for each extra °F. This matches −9 cups per °C, since 1 °C is 1.8 °F and 1.8 × 5 = 9.
- At 20 °C the owner's line gives 260 − 180 = 80 cups. 20 °C is 68 °F, and the partner's line gives 42 − 34 = 8 tens, also 80 cups.
- 0 °F is about −17.8 °C, well below the data's 6 to 22 °C (42.8 to 71.6 °F), so the intercept is a feature of the line only.
Full working
- C = (F − 32) ÷ 1.8rearrange F = 1.8C + 32
- y = 260 − 9(F − 32) ÷ 1.8 = 260 − 5(F − 32)substitute
- y = 260 − 5F + 160 = 420 − 5F cupsexpand
- y ÷ 10 = 42 − 0.5Fsales in tens of cups
- C = 20: y = 260 − 9 × 20 = 80 cupsowner's line
- F = 1.8 × 20 + 32 = 68: 42 − 0.5 × 68 = 8 tens = 80 cupspartner's line
- F = 0 is C ≈ −17.8, outside 6 to 22 °Cintercept not useful
- Sales (tens of cups) = 42 − 0.5F; both predict 80 cups at 20 °C; intercept is extrapolation
A student measures the height y (cm) of a seedling at x weeks after planting: (1, 5), (2, 7), (4, 15), (5, 16) and (8, ?). The height at week 8 has been smudged, and so has the intercept of her least squares line, which now reads y = ? + 2.5x. Find the missing height and the full equation of the line.
Hint
The slope uses b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and x̄ is known. Because Σ(x − x̄) = 0, the top can be worked out as Σ(x − x̄)y, which needs no ȳ.
Answer
The week 8 height is 22 cm and the line is y = 3 + 2.5x.
Step by step
- All five x values are known, so x̄ = (1 + 2 + 4 + 5 + 8) ÷ 5 = 4.
- The x deviations are −3, −2, 0, 1 and 4, so Σ(x − x̄)² = 9 + 4 + 0 + 1 + 16 = 30.
- The slope is 2.5, so the top of the slope formula must be 2.5 × 30 = 75.
- Since the x deviations add to 0, the top equals Σ(x − x̄)y. The four known points give −15 − 14 + 0 + 16 = −13, so the week 8 point gives 4y = 75 + 13 = 88 and y = 22.
- Now ȳ = (5 + 7 + 15 + 16 + 22) ÷ 5 = 13, and the line goes through (4, 13), so a = 13 − 2.5 × 4 = 3.
Full working
- x̄ = 20 ÷ 5 = 4all x values known
- Σ(x − x̄)² = 9 + 4 + 0 + 1 + 16 = 30deviations −3, −2, 0, 1, 4
- Σ(x − x̄)(y − ȳ) = 2.5 × 30 = 75b = 2.5
- Σ(x − x̄)(y − ȳ) = Σ(x − x̄)ybecause Σ(x − x̄) = 0
- −3 × 5 − 2 × 7 + 0 × 15 + 1 × 16 + 4y = 75the four known points and the missing one
- −13 + 4y = 75, so y = 22missing height
- ȳ = (5 + 7 + 15 + 16 + 22) ÷ 5 = 13mean height
- a = 13 − 2.5 × 4 = 3a = ȳ − b x̄
- Missing height 22 cm; y = 3 + 2.5x
Use the lesson's five students: hours studied x = 1, 2, 3, 4, 5 and test marks y = 52, 55, 61, 64, 68, with least squares line y = 47.7 + 4.1x. (a) A sixth student studied 3 hours and scored 72. Show that the slope of the new least squares line is still 4.1, and find the new line. (b) Describe every point that could be added as a sixth student without changing the line y = 47.7 + 4.1x at all, and explain why.
Hint
(a) The new point's x equals x̄, so check what happens to x̄, to each (x − x̄) and to the two sums in the slope formula. (b) Least squares means the smallest possible sum of squared residuals. Ask what a new point adds to that sum for the old line and for any other line.
Answer
(a) Slope still 4.1; new line y = 49.7 + 4.1x. (b) Exactly the points on the line y = 47.7 + 4.1x, such as (3, 60) or (7, 76.4): such a point adds 0 to the old line's sum of squared residuals and at least 0 to every other line's, so the old line is still the smallest.
Step by step
- The new x is 3, which is x̄, so x̄ stays 3 and every x deviation is unchanged, with the new one equal to 0.
- Σ(x − x̄)² stays 10, and the new point adds 0 × (72 − ȳ) = 0 to the top. Because the x deviations add to 0, changing ȳ does not change the top either, so it stays 41 and b = 41 ÷ 10 = 4.1.
- The new mean mark is (300 + 72) ÷ 6 = 62, so a = 62 − 4.1 × 3 = 49.7. The whole line moves up 2 marks.
- For (b): the old line had the smallest sum of squared residuals for the five students. A new point on that line has residual 0, so the old line's total does not change.
- Any other line was already worse on the five students, and the new point adds a square, which is never negative, to its total. So the old line still wins and the line is unchanged.
- A point off the line would add a positive amount to the old line's total, and in general a different line would then do better, as in (a).
Full working
- x̄ = (1 + 2 + 3 + 4 + 5 + 3) ÷ 6 = 3unchanged
- Σ(x − x̄)² = 4 + 1 + 0 + 1 + 4 + 0 = 10new deviation is 0
- Σ(x − x̄)(y − ȳ) = Σ(x − x̄)y = 41 + 0 × 72 = 41top unchanged
- b = 41 ÷ 10 = 4.1slope unchanged
- ȳ = (300 + 72) ÷ 6 = 62new mean mark
- a = 62 − 4.1 × 3 = 49.7a = ȳ − b x̄
- new point on old line: residual 0, adds 0 to its sum of squares(b)
- any other line: adds a square ≥ 0, and was already largerold line still least
- (a) y = 49.7 + 4.1x; (b) any point on y = 47.7 + 4.1x



