The least-squares line
Learn to fit the least-squares line for Year 12 General Mathematics in Queensland (QCAA). When a scatterplot shows a linear trend, such as rent falling as you move further from the Brisbane CBD, one straight line \(y = mx + c\) can summarise it. The least-squares line is the fairest choice, because it keeps the squared vertical gaps to the points as small as possible.
You will practise finding the slope and \(y\)-intercept two ways: with the regression mode of a scientific calculator, and from summary statistics using \(m = r\,s_y/s_x\) and \(c = \bar{y} - m\bar{x}\). You will also check that the line passes through the mean point. Next, residual plots test whether a straight line really suits the data.
Theory
The least-squares line \(y = mx + c\) is the straight line that best fits a scatterplot of numerical data. This guide for Year 12 General Mathematics in Queensland shows what the line minimises, how to find its slope and \(y\)-intercept with a calculator or from summary statistics, and how to use the mean point as a check.
A residual is the vertical gap between a data point and a fitted line: the actual \(y\)-value minus the value the line predicts. The least-squares line is the one straight line that makes the sum of the squared residuals as small as possible. Squaring stops the positive and negative gaps from cancelling, so every point counts.
The line is written \(y = mx + c\), where \(m\) is the slope and \(c\) is the \(y\)-intercept. The explanatory variable always goes on the \(x\)-axis and the response variable on the \(y\)-axis. Swapping them gives a different line.
You can find the line in two ways: enter the data pairs into the two-variable statistics (linear regression) mode of a scientific calculator, or calculate it from summary statistics: the correlation coefficient \(r\), the means \(\bar{x}\) and \(\bar{y}\), and the standard deviations \(s_x\) and \(s_y\).
The least-squares line, with slope \(m\) and \(y\)-intercept \(c\):
From summary statistics, find the slope first and then the intercept:
What the calculator does with the raw data (the same slope, written with sums of deviations):
Finding the least-squares line
- Choose \(x\) and \(y\). The explanatory variable (the one used to predict) is \(x\); the response variable is \(y\).
- From raw data, use technology. Enter the pairs in the calculator's two-variable statistics (linear regression) mode and read off the slope and intercept. Check which letter your calculator uses for each.
- From summary statistics, use the formulas. Calculate \(m = r\,s_y/s_x\) first, then \(c = \bar{y} - m\bar{x}\) using the unrounded \(m\).
- Write the line. State \(y = mx + c\), rounding \(m\) and \(c\) only at the end (usually to 2 decimal places), and say what \(x\) and \(y\) stand for.
Find the slope from \(r\) and the standard deviations, then the intercept from the means.
| \(m\) | \(=\) | \(r\,\dfrac{s_y}{s_x}\) |
| \(m\) | \(=\) | \(0.64 \times \dfrac{2.5}{0.8}\) |
| \(m\) | \(=\) | \(2\) |
| \(c\) | \(=\) | \(\bar{y} - m\bar{x}\) |
| \(c\) | \(=\) | \(7 - 2 \times 3\) |
| \(c\) | \(=\) | \(1\) |
The least-squares line is \(y = 2x + 1\), where \(x\) is the cruise length (hours) and \(y\) is the number of sightings.
| Staff on shift, \(x\) | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| Customers per hour, \(y\) | 38 | 45 | 59 | 66 | 77 |
Staff numbers explain the service rate, so staff is \(x\). The regression mode calculates these values:
| \(S_{xx}\) | \(=\) | \(10\) |
| \(S_{xy}\) | \(=\) | \(99\) |
| \(m\) | \(=\) | \(\dfrac{S_{xy}}{S_{xx}}\) |
| \(m\) | \(=\) | \(\dfrac{99}{10}\) |
| \(m\) | \(=\) | \(9.9\) |
| \(\bar{x}\) | \(=\) | \(4\) |
| \(\bar{y}\) | \(=\) | \(57\) |
| \(c\) | \(=\) | \(\bar{y} - m\bar{x}\) |
| \(c\) | \(=\) | \(57 - 9.9 \times 4\) |
| \(c\) | \(=\) | \(17.4\) |
The least-squares line is \(y = 9.9x + 17.4\).
| Distance from CBD, \(x\) (km) | 4 | 7 | 10 | 14 | 18 | 25 |
|---|---|---|---|---|---|---|
| Weekly rent, \(y\) (\(\$\)) | 612 | 575 | 560 | 498 | 470 | 405 |
Enter distance as \(x\) and rent as \(y\). Keep the unrounded slope when finding \(c\):
| \(S_{xx}\) | \(=\) | \(296\) |
| \(S_{xy}\) | \(=\) | \(-2930\) |
| \(m\) | \(=\) | \(\dfrac{-2930}{296}\) |
| \(m\) | \(\approx\) | \(-9.898649\) |
| \(c\) | \(=\) | \(\bar{y} - m\bar{x}\) |
| \(c\) | \(\approx\) | \(520 - (-9.898649)(13)\) |
| \(c\) | \(\approx\) | \(520 + 128.6824\) |
| \(c\) | \(\approx\) | \(648.68\) |
The least-squares line is \(y = -9.90x + 648.68\). The slope is negative, matching the falling pattern of the points.
The mean point lies on the line, so substitute \(\bar{x}\). Then rearrange \(m = r\,s_y/s_x\) to make \(r\) the subject.
| \(\bar{y}\) | \(=\) | \(m\bar{x} + c\) |
| \(\bar{y}\) | \(=\) | \(0.05 \times 120 + 24.5\) |
| \(\bar{y}\) | \(=\) | \(6 + 24.5\) |
| \(\bar{y}\) | \(=\) | \(30.5\) |
| \(r\) | \(=\) | \(m\,\dfrac{s_x}{s_y}\) |
| \(r\) | \(=\) | \(0.05 \times \dfrac{32}{2}\) |
| \(r\) | \(=\) | \(0.8\) |
The mean speed is \(30.5\) km/h and \(r = 0.8\), a strong positive association.
Common pitfalls
Frequently asked questions
What is the least-squares line?
It is the straight line through a scatterplot that makes the sum of the squared residuals (the squared vertical gaps between the points and the line) as small as possible. It is written y = mx + c, with slope m and y-intercept c.
How do you find the least-squares line on a calculator?
Put the calculator in two-variable statistics or linear regression mode, enter the explanatory values as x and the response values as y, then read off the slope and the intercept. Check which letter your calculator uses for the number multiplying x.
How do you find the least-squares line from r and the standard deviations?
Use m = r times s_y divided by s_x for the slope. Then use c = y-bar minus m times x-bar for the intercept, keeping the unrounded slope. Finally write the line as y = mx + c.
Does the least-squares line always pass through the mean point?
Yes. Because c = y-bar minus m times x-bar, substituting x = x-bar into y = mx + c gives y = y-bar, so the point (x-bar, y-bar) is always on the line.
Why are the residuals squared?
Residuals above the line are positive and those below are negative, so their plain total is always zero for this line. Squaring makes every gap count, and the line with the smallest total of squares is the best fit.
Does it matter which variable is x?
Yes. The explanatory variable must be x and the response variable must be y. Swapping them gives a different least-squares line, because the method minimises vertical gaps only.