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
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?
Show answer
1. 3 2. 35 3. 14 4. y falls by about 2 for each extra unit of x
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.
Where science uses this
Practise this
Report a mistake
More in AS 91581
Is your child on track?
Take the free placement test


