Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 General (Unit 3 & 4) Bivariate data analysis 2

The least-squares line

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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\).

Key idea. The least-squares line is the line with the smallest possible total of squared residuals. Its slope is \(m = r\dfrac{s_y}{s_x}\), and it always passes through the mean point \((\bar{x}, \bar{y})\).
The least-squares line minimises the squared residualsSix points with the least-squares line y = 2x + 1. Dashed vertical segments join each point to the line and shaded squares show each squared residual. x y 1 2 3 4 5 6 4 8 12 16
Each dashed segment is a residual, and each shaded square is that residual squared. The least-squares line \(y = 2x + 1\) makes the total area of the squares as small as possible.
The least-squares line passes through the mean pointFive points with the least-squares line y = -1.25x + 17.5, which passes through the mean point (6, 10), marked with a gold ring. x y 2 4 6 8 10 5 10 15 20 mean point (6, 10)
The least-squares line \(y = -1.25x + 17.5\) passes through the mean point \((\bar{x}, \bar{y}) = (6, 10)\). A negative slope means \(y\) falls as \(x\) rises.

The least-squares line, with slope \(m\) and \(y\)-intercept \(c\):

\[y = mx + c\]
y=mx+c

From summary statistics, find the slope first and then the intercept:

\[m = r\,\dfrac{s_y}{s_x}\]
m=r⁢sysx
\[c = \bar{y} - m\bar{x}\]
c=y¯-mx¯

What the calculator does with the raw data (the same slope, written with sums of deviations):

\[m = \dfrac{S_{xy}}{S_{xx}}, \quad S_{xx} = \sum (x - \bar{x})^2, \quad S_{xy} = \sum (x - \bar{x})(y - \bar{y})\]
m=SxySxx
Sign check. \(s_x\) and \(s_y\) are always positive, so the slope \(m\) always has the same sign as \(r\). Rearranging \(c = \bar{y} - m\bar{x}\) gives \(\bar{y} = m\bar{x} + c\): the mean point is always on the line.

Finding the least-squares line

  1. Choose \(x\) and \(y\). The explanatory variable (the one used to predict) is \(x\); the response variable is \(y\).
  2. 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.
  3. 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\).
  4. 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.
Example 1 — From summary statistics
For whale-watching cruises from Hervey Bay, the cruise length, \(x\) (hours), and the number of whale sightings, \(y\), have \(\bar{x} = 3\), \(s_x = 0.8\), \(\bar{y} = 7\), \(s_y = 2.5\) and \(r = 0.64\). Find the least-squares line.
Solution

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\)
y=2x+1

The least-squares line is \(y = 2x + 1\), where \(x\) is the cruise length (hours) and \(y\) is the number of sightings.

Example 2 — Using technology
A Cairns café recorded the number of staff on shift and the number of customers served per hour on five mornings. Use technology to find the least-squares line.
Staff on shift, \(x\)23456
Customers per hour, \(y\)3845596677
Solution

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\)
y=9.9x+17.4
Example 2 scatterplotCustomers served per hour against staff on shift, with the least-squares line y = 9.9x + 17.4. staff customers 1 2 3 4 5 6 20 40 60 80

The least-squares line is \(y = 9.9x + 17.4\).

Example 3 — A negative slope, rounded
The table shows the distance from the Brisbane CBD and the weekly rent for six similar two-bedroom units. Find the least-squares line, with values to 2 decimal places.
Distance from CBD, \(x\) (km)4710141825
Weekly rent, \(y\) (\(\$\))612575560498470405
Solution

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\)
y=-9.90x+648.68
Example 3 scatterplotWeekly rent against distance from the CBD, with the least-squares line y = -9.90x + 648.68. km rent 5 10 15 20 25 400 500 600 700

The least-squares line is \(y = -9.90x + 648.68\). The slope is negative, matching the falling pattern of the points.

Example 4 — Working backwards
For members of a Brisbane cycling club, the least-squares line relating weekly training distance, \(x\) (km), to average speed, \(y\) (km/h), is \(y = 0.05x + 24.5\). The training distances have \(\bar{x} = 120\) and \(s_x = 32\), and the speeds have \(s_y = 2\). Find \(\bar{y}\) and the correlation coefficient \(r\).
Solution

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\)
r=0.8

The mean speed is \(30.5\) km/h and \(r = 0.8\), a strong positive association.

Common pitfalls

Watch the calculator's letters. Some calculators show the line as \(y = ax + b\) and others as \(y = a + bx\). Match each value to its role: the number multiplying \(x\) is the slope \(m\), and the constant is the intercept \(c\).
Put the explanatory variable on \(x\). The line for predicting \(y\) from \(x\) is not the same as the line for predicting \(x\) from \(y\). Decide which variable explains the other before you enter the data.
Use the unrounded slope for \(c\). Rounding \(m\) first and then calculating \(c = \bar{y} - m\bar{x}\) can change the second decimal place of \(c\). Round both values only at the end.

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.