The least squares line
Learn to fit the least squares line in Year 12 VCE General Mathematics. When a scatterplot shows a linear trend, such as the air getting colder as you climb Mt Buller, one straight line \(y = a + bx\) can sum it up. The least squares line is the fairest choice, because it keeps the squared vertical gaps between the points and the line as small as possible.
You will find the intercept \(a\) and slope \(b\) two ways: with the regression mode of your calculator, and from summary statistics using \(b = r\,s_y/s_x\) and \(a = \bar{y} - b\bar{x}\). You will write the line in context, check it passes through the point of means, and then move on to interpreting what the slope and intercept tell you.
Theory
The least squares line \(y = a + bx\) is the straight line that best fits a scatterplot of numerical data. This Year 12 General Mathematics guide for students in Victoria shows what the line minimises, how to find its intercept \(a\) and slope \(b\) with technology or from summary statistics, and how to use the point of means as a check.
A residual is the vertical gap between a data point and a fitted line: the actual value of \(y\) 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 = a + bx\), where \(a\) is the intercept and \(b\) is the slope. The explanatory variable goes on the horizontal axis as \(x\) and the response variable on the vertical axis as \(y\), so in context the line reads \(\text{response} = a + b \times \text{explanatory}\). Swapping the variables 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 CAS or 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 intercept \(a\) and slope \(b\):
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 (the one being predicted) is \(y\).
- From raw data, use technology. Enter the pairs in the calculator's two-variable statistics (linear regression) mode and read off \(a\) and \(b\). Check which letter your calculator uses for the slope: it is the number multiplying \(x\).
- From summary statistics, use the formulas. Calculate \(b = r\,s_y/s_x\) first, then \(a = \bar{y} - b\bar{x}\) using the unrounded \(b\).
- Write the line in context. State \(y = a + bx\) with the variable names, for example \(\text{price} = 32.3 - 1.8 \times \text{age}\), rounding \(a\) and \(b\) only at the end.
Find the slope from \(r\) and the standard deviations, then the intercept from the means.
| \(b\) | \(=\) | \(r \times \dfrac{s_y}{s_x}\) |
| \(b\) | \(=\) | \(0.8 \times \dfrac{45}{1.2}\) |
| \(b\) | \(=\) | \(0.8 \times 37.5\) |
| \(b\) | \(=\) | \(30\) |
| \(a\) | \(=\) | \(\bar{y} - b\bar{x}\) |
| \(a\) | \(=\) | \(210 - 30 \times 3\) |
| \(a\) | \(=\) | \(210 - 90\) |
| \(a\) | \(=\) | \(120\) |
The least squares line is \(\text{grocery bill} = 120 + 30 \times \text{people}\), with the bill in dollars.
| Hours of sunshine, \(x\) | 2 | 4 | 6 | 8 | 10 |
|---|---|---|---|---|---|
| Ice creams sold, \(y\) | 30 | 41 | 49 | 62 | 68 |
Sunshine explains the sales, so sunshine is \(x\). The regression mode calculates these values:
| \(S_{xx}\) | \(=\) | \(40\) |
| \(S_{xy}\) | \(=\) | \(194\) |
| \(b\) | \(=\) | \(\dfrac{S_{xy}}{S_{xx}}\) |
| \(b\) | \(=\) | \(\dfrac{194}{40}\) |
| \(b\) | \(=\) | \(4.85\) |
| \(\bar{x}\) | \(=\) | \(6\) |
| \(\bar{y}\) | \(=\) | \(50\) |
| \(a\) | \(=\) | \(\bar{y} - b\bar{x}\) |
| \(a\) | \(=\) | \(50 - 4.85 \times 6\) |
| \(a\) | \(=\) | \(50 - 29.1\) |
| \(a\) | \(=\) | \(20.9\) |
The least squares line is \(\text{ice creams} = 20.9 + 4.85 \times \text{sunshine}\).
| Altitude, \(x\) (hundreds of m) | 4 | 6 | 8 | 10 | 12 | 14 |
|---|---|---|---|---|---|---|
| Temperature, \(y\) (°C) | 15.2 | 12.9 | 10.1 | 8.4 | 5.3 | 3.6 |
Enter altitude as \(x\) and temperature as \(y\). Keep the unrounded slope when finding \(a\):
| \(S_{xx}\) | \(=\) | \(70\) |
| \(S_{xy}\) | \(=\) | \(-82.5\) |
| \(b\) | \(=\) | \(\dfrac{S_{xy}}{S_{xx}}\) |
| \(b\) | \(=\) | \(\dfrac{-82.5}{70}\) |
| \(b\) | \(\approx\) | \(-1.178571\) |
| \(\bar{x}\) | \(=\) | \(9\) |
| \(\bar{y}\) | \(=\) | \(9.25\) |
| \(a\) | \(=\) | \(\bar{y} - b\bar{x}\) |
| \(a\) | \(\approx\) | \(9.25 - (-1.178571)(9)\) |
| \(a\) | \(\approx\) | \(9.25 + 10.6071\) |
| \(a\) | \(\approx\) | \(19.86\) |
The least squares line is \(\text{temperature} = 19.86 - 1.18 \times \text{altitude}\). The slope is negative, matching the falling pattern of the points (and the negative \(r\)).
The point of means lies on the line, so substitute \(\bar{x}\). Then rearrange \(b = r\,s_y/s_x\) to make \(r\) the subject.
| \(\bar{y}\) | \(=\) | \(a + b\bar{x}\) |
| \(\bar{y}\) | \(=\) | \(24.5 + 0.05 \times 120\) |
| \(\bar{y}\) | \(=\) | \(24.5 + 6\) |
| \(\bar{y}\) | \(=\) | \(30.5\) |
| \(r\) | \(=\) | \(\dfrac{b \times s_x}{s_y}\) |
| \(r\) | \(=\) | \(\dfrac{0.05 \times 32}{2}\) |
| \(r\) | \(=\) | \(\dfrac{1.6}{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 = a + bx, with intercept a and slope b.
How do you find the least squares line on a calculator?
Use the two-variable statistics or linear regression mode. Enter the explanatory values as x and the response values as y, then read off the intercept and the slope. The slope is the number multiplying x, whatever letter your calculator gives it.
How do you find a and b from r and the standard deviations?
Use b = r times s_y divided by s_x for the slope. Then use a = y-bar minus b times x-bar for the intercept, keeping the unrounded slope. Finally write the line as y = a + bx.
Does the least squares line always pass through the point of means?
Yes. Because a = y-bar minus b times x-bar, substituting x = x-bar into y = a + bx 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 least squares line.
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 only makes the vertical gaps small.