Vertex Learn

Fitting a linear model

WorksheetShare to Classroom

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.
Worked example

A small data set

  • Five students from a made-up class.
Hours studied, x12345
Test mark, y5255616468
Worked example

Finding the line

  • Calculators do this, but the working shows where it comes from.
  1. mean x = 3, mean y = 60
  2. sum of (x − 3)(y − 60) = 41
  3. sum of (x − 3)² = 10
  4. b = 41 ÷ 10 = 4.1
  5. a = 60 − 4.1 × 3 = 47.7
  6. y = 47.7 + 4.1x
Worked example

Reading the slope

  • Write it in context.
  1. Each extra hour is linked to about 4.1 more marks
Key idea

The line through the means

  • The least squares line always passes through the point (mean of x, mean of y).
Five points with the fitted line y equals 47.7 plus 4.1x passing through the point 3, 60.
Key idea

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.
Key idea

Statistics explorer

Worked example

Reading the intercept

  • In y = 47.7 + 4.1x, the 47.7 is the model's mark for 0 hours of study.
  1. x = 0 is outside the data, which runs 1 to 5
  2. 47.7 is a feature of the line, not a fact about students
Key idea

Try it: the fitted line

Worked example

Finding a from the means

  • The mean of x is 6, the mean of y is 40 and b = 3.
  1. 40 = a + 3 × 6
  2. a = 22
  3. 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

1

For y = 20 + 3x, what is the slope?

2

For y = 20 + 3x, predict y when x = 5.

3

For y = 2 + 3x, the mean of x is 4. Find the mean of y.

4

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

Your turn

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
  1. In y = a + bx, b is the slope and a is the intercept. Here b = 1.45 and a = 6.3.
  2. 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.
  3. 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".
  4. The intercept is the y value when x = 0: 6.3 marks for no revising.
  5. 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.
  6. Estimate for 5 minutes: substitute x = 5. y = 6.3 + 1.45 × 5 = 6.3 + 7.25 = 13.55, about 13.6 marks.
  7. 5 is inside 2 to 10, so this is interpolation, which is reasonable.
  8. Check: the actual scores at 4 and 6 minutes are 12 and 16, and 13.6 sits between them.
Full working
  1. b = 1.45slope: about 1.45 marks per extra minute, on average
  2. a = 6.3 at x = 0outside the data 2 to 10, unreliable
  3. y = 6.3 + 1.45 × 5 = 13.55x = 5 is inside the data
  4. About 1.45 marks per minute; intercept unreliable; about 13.6 marks for 5 minutes
Your turn

Sample questions: Merit

1

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
  1. Means: x̄ = (2 + 4 + 6 + 8 + 10) ÷ 5 = 6 and ȳ = (9 + 12 + 16 + 17 + 21) ÷ 5 = 75 ÷ 5 = 15.
  2. x deviations, x − 6: −4, −2, 0, 2, 4.
  3. y deviations, y − 15: −6, −3, 1, 2, 6.
  4. Multiply in pairs: 24, 6, 0, 4, 24. Their sum is Σ(x − x̄)(y − ȳ) = 58.
  5. Square the x deviations: 16, 4, 0, 4, 16. Their sum is Σ(x − x̄)² = 40.
  6. Slope: b = 58 ÷ 40 = 1.45.
  7. Intercept: a = ȳ − b x̄ = 15 − 1.45 × 6 = 15 − 8.7 = 6.3.
  8. Classic mistake: dividing by Σ(y − ȳ)², which is 86. The bottom uses the x deviations only.
  9. So the line is y = 6.3 + 1.45x.
  10. 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
  1. x̄ = 6, ȳ = 15
  2. Σ(x − x̄)(y − ȳ) = 24 + 6 + 0 + 4 + 24 = 58
  3. Σ(x − x̄)² = 16 + 4 + 0 + 4 + 16 = 40
  4. b = 58 ÷ 40 = 1.45
  5. a = 15 − 1.45 × 6 = 6.3a = ȳ − b x̄
  6. y = 6.3 + 1.45x
2

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
  1. Key fact: the least squares line always goes through the point of means, (x̄, ȳ).
  2. That is because a = ȳ − b x̄ is part of how the line is built. Rearranged, it says ȳ = a + b x̄.
  3. So substitute x = 12.5 and y = 41 into y = a + 2.4x: 41 = a + 2.4 × 12.5.
  4. 2.4 × 12.5 = 30, so 41 = a + 30 and a = 11.
  5. The line is y = 11 + 2.4x.
  6. Classic mistake: substituting a data point instead of the means. Data points are usually not on the line; the point of means always is.
  7. Prediction at x = 15: y = 11 + 2.4 × 15 = 11 + 36 = 47.
  8. Check: at x = 12.5 the line gives 11 + 30 = 41 = ȳ. Correct.
  9. One caution: the prediction is only sensible if 15 is inside the range of the x data.
Full working
  1. line passes through (x̄, ȳ) = (12.5, 41)
  2. 41 = a + 2.4 × 12.5 = a + 30
  3. a = 11, so y = 11 + 2.4x
  4. y = 11 + 2.4 × 15 = 47x = 15
  5. a = 11; y = 47 when x = 15
Your turn

Sample questions: Excellence

1

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
  1. Each residual is actual − predicted: y − (a + bx).
  2. 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.
  3. Use the means: Σy = nȳ and Σx = n x̄. So the sum is nȳ − na − b n x̄.
  4. Now replace a with ȳ − b x̄: nȳ − n(ȳ − b x̄) − b n x̄.
  5. Expand: nȳ − nȳ + b n x̄ − b n x̄ = 0. So the residuals always add to zero.
  6. Classic mistake: thinking this proves the line fits well. Any least squares line has residuals that add to zero, even a poor fit.
  7. Check with the data. Predicted values: 6.3 + 1.45 × 2 = 9.2, then 12.1, 15, 17.9 and 20.8.
  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.
  9. Sum: −0.2 − 0.1 + 1 − 0.9 + 0.2 = 0. It matches the proof.
  10. In words: the points above the line balance the points below it exactly, because the line goes through the point of means.
Full working
  1. Σ(y − a − bx) = Σy − na − bΣx
  2. = nȳ − na − b n x̄Σy = nȳ, Σx = n x̄
  3. = nȳ − n(ȳ − b x̄) − b n x̄ = 0a = ȳ − b x̄
  4. predicted: 9.2, 12.1, 15, 17.9, 20.8
  5. residuals: −0.2, −0.1, 1, −0.9, 0.2sum 0
  6. The residuals always add to zero; here −0.2 − 0.1 + 1 − 0.9 + 0.2 = 0
2

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
  1. The y on x line makes the vertical gaps (the y residuals) as small as possible. Its slope is 58 ÷ 40 = 1.45.
  2. With x and y swapped, the line makes the horizontal gaps as small as possible. Its slope is d = 58 ÷ 86 ≈ 0.674.
  3. Both lines pass through the point of means (6, 15). So hers is x = 6 + 0.674(y − 15).
  4. Rearranging for y: y − 15 = (x − 6) ÷ 0.674, so the slope against x is 1 ÷ 0.674 ≈ 1.483.
  5. 1.483 is not 1.45, so the two lines are different, though both go through (6, 15).
  6. Classic mistake: thinking a line is a line, so it does not matter which variable is which. Least squares treats x and y differently.
  7. Link to r: 1.45 × 0.674 ≈ 0.978, and r² = 58² ÷ (40 × 86) ≈ 0.978. The two slopes multiply to r².
  8. So the lines only agree when r² = 1, a perfect straight line. The weaker the relationship, the further apart they are.
  9. 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.
  10. Her line would be the right one only for predicting minutes from a score.
Full working
  1. b(y on x) = 58 ÷ 40 = 1.45
  2. d(x on y) = 58 ÷ 86 ≈ 0.674
  3. x = 6 + 0.674(y − 15), so y slope = 1 ÷ 0.674 ≈ 1.483rearranged
  4. 1.45 × 0.674 ≈ 0.978 = r²slopes multiply to r²
  5. lines agree only if r = ±1
  6. No: her line has slope 1.483; use y = 6.3 + 1.45x to predict scores
Summary

What to take away

  • Slope in context, with units.
  • The line passes through the means.
Science

Where science uses this

Practice

Practise this

Worksheet with answersWithout answersDownload PDF
Report a mistake

More in AS 91581

Is your child on track?

Take the free placement test