The least-squares line of best fit
Fitting the least-squares regression line with a calculator or from summary statistics, interpreting its gradient and intercept in context, and understanding residuals.
Once you know there's a strong linear association, the next step is to draw a line through the middle of the points and use it to make predictions. Everyone who draws a line by eye draws a slightly different one. The least-squares line is the one line that everyone (and every calculator) agrees on.
Residuals and "least squares"
For each point, the residual is the vertical gap between the actual $y$-value and the value the line predicts:
A point above the line has a positive residual and a point below has a negative one. The least-squares line is the line that makes the sum of the squared residuals as small as possible. Squaring stops the positive and negative gaps from cancelling, and it punishes big misses more than small ones.
Finding the line with a calculator
The line is written $y = mx + c$ in the Standard course. Your calculator may use $y = a + bx$, where $b$ is the gradient and $a$ is the intercept. Watch out for the swap.
Eight students recorded hours of study per week ($x$) and their exam mark out of $100$ ($y$).
| Hours ($x$) | 2 | 4 | 5 | 7 | 8 | 10 | 12 | 13 |
|---|---|---|---|---|---|---|---|---|
| Mark ($y$) | 48 | 55 | 52 | 63 | 66 | 70 | 79 | 81 |
Find the equation of the least-squares line, with values to two decimal places.
Interpreting the gradient and intercept
The numbers in the equation have meanings, and HSC questions ask for them in words.
- The gradient $m$ is the predicted change in $y$ for each increase of $1$ in $x$. Here: each extra hour of study per week is linked to about $3.09$ more marks.
- The intercept $c$ is the predicted $y$ when $x = 0$. Here: a student who does no study is predicted to score about $40.72$. That only makes sense if $x = 0$ is close to the data. Here the smallest $x$ is $2$, so it's a small stretch, which is acceptable.
The line from summary statistics
Sometimes you are not given the raw data, only the means, standard deviations and $r$. Then use:
The gradient formula makes sense: if $y$ is much more spread out than $x$, the line has to be steeper. And $r$ scales it down when the association is weak. The intercept formula comes straight from the line passing through $(\bar{x}, \bar{y})$.
For a group of adults, height $x$ (cm) and mass $y$ (kg) have $\bar{x} = 168.4$, $s_x = 7.2$, $\bar{y} = 64.1$, $s_y = 8.5$ and $r = 0.82$. Find the least-squares line and predict the mass of someone $175$ cm tall.
The negative intercept means a person of height $0$ cm would weigh $-98.92$ kg. That's nonsense, and it is fine. $x = 0$ is far outside the data, so the intercept has no real meaning here.
Drag any point and watch $r$ and the least-squares line update. Turn on Show residuals to see the gaps the line is minimising.
Drag one point far above the others. How much does the line tilt? How much does $r$ drop? Then try to arrange the points so that $r$ is close to $0$.
Your turn
For a set of data, $\bar{x} = 20$, $s_x = 4$, $\bar{y} = 50$, $s_y = 10$ and $r = -0.6$. Find the equation of the least-squares line.
Show the answer
$m = -0.6 \times \dfrac{10}{4} = -1.5$. Then $c = 50 - (-1.5)(20) = 50 + 30 = 80$. The line is $y = -1.5x + 80$.
The least-squares line is the line that:
"Least squares" means the sum of the squared vertical gaps (residuals) is the least possible.
A point has actual $y = 70$, and the line predicts $67$. What is the residual?
Residual $=$ actual $-$ predicted $= 70 - 67 = 3$.
A calculator in $y = a + bx$ mode gives $a = 12.5$ and $b = -0.8$. In $y = mx + c$ form the line is:
$b$ is the gradient, so $m = -0.8$. $a$ is the intercept, so $c = 12.5$. The line is $y = -0.8x + 12.5$.
The line $\text{cost} = 45 \times \text{hours} + 80$ models a plumber's charges in dollars. The gradient means:
The gradient is the change in cost for each extra hour: $\$45$ per hour. The $\$80$ is the intercept, a fixed call-out fee.
Find $m$ given $r = -0.75$, $s_x = 5$ and $s_y = 10$.
$m = r\dfrac{s_y}{s_x} = -0.75 \times \dfrac{10}{5} = -1.5$.
The least-squares line has gradient $m = 2$, and $\bar{x} = 15$, $\bar{y} = 40$. Find $c$.
$c = \bar{y} - m\bar{x} = 40 - 2 \times 15 = 40 - 30 = 10$.
For a line of age of a car ($x$, years) against value ($y$), the intercept is $c = 31.2$ (thousands of dollars). The best interpretation is:
The intercept is the predicted $y$ when $x = 0$. Age $0$ means new, so the predicted value of a new car is $31.2$ thousand, or $\$31{,}200$.
Which point must lie on the least-squares line?
The least-squares line always passes through the means, $(\bar{x}, \bar{y})$. That's why $c = \bar{y} - m\bar{x}$ works.
Exam style: Using $\text{mark} = 3.09 \times \text{hours} + 40.72$, predict the mark of a student who studies $10$ hours per week. Give your answer to one decimal place.
$3.09 \times 10 + 40.72 = 30.9 + 40.72 = 71.62 \approx 71.6$ marks.
Exam style: Another student, not in the original data, studied $10$ hours per week and scored $75$. Using the prediction from the previous question ($71.6$), find the residual for this student.
Residual $=$ actual $-$ predicted $= 75 - 71.6 = 3.4$. It is positive, so the student scored above what the line predicted.
Coaching Centre