Lesson 1 of 7 · 16 min
The least-squares line
Understand what makes the least-squares line the line of best fit, and use technology to fit it to raw data and write it as with the variable names.
What you will be able to do
- Explain what the least-squares line minimises.
- Use a calculator's statistics mode or a spreadsheet to find the slope and intercept of the least-squares line.
- Write the equation of the line in terms of the explanatory and response variables.
A café in Fortitude Valley records the maximum temperature and the number of iced coffees it sells on ten summer days. In Bivariate data analysis 1 you would describe this as a strong positive linear association, with .
The owner wants more than a description. On a 31 °C day, how many iced coffees should she prepare? To answer that you need a line through the data — a linear model.
The vertical segments are the residuals: how far each point is above or below the line. Drag a point and watch the line move to keep the squared residuals as small as possible.
You could draw a line by eye, but two people would draw two different lines. Statisticians agree on one line by measuring how badly a line fits.
For each point, the vertical distance from the point to the line is its residual. Some residuals are positive (points above the line) and some negative (points below). Square them so they are all positive, then add them up. The line that makes this total as small as possible is the least-squares line.
Definition
The least-squares line
The least-squares line is the straight line that minimises the sum of the squared residuals. Here
- is the explanatory variable and is the response variable,
- is the slope (gradient) and is the -intercept.
It always passes through the point .
What does the least-squares line make as small as possible?
You never find the least-squares line by trial and error. Your calculator does it for you from the raw data. For the café, technology gives and , so to two decimal places
On a 31 °C day the model predicts , or about 78 iced coffees.
Fitting the least-squares line with technology
Decide which variable is explanatory
It goes in the list. The response variable goes in the list. Swapping them gives a different line.
Enter the data in paired-variable statistics mode
On most scientific calculators: Statistics (or MODE → STAT) → y = a + bx (or A + BX), then type each with its .
Read the slope and intercept
Find them in the regression menu. Careful: many calculators write the line as , so is the slope and is the intercept.
Round and write the equation in context
Round as the question asks, then replace and with the variable names.
Scientific calculator
Paired-variable statistics mode, . It gives (intercept), (slope) and . Clear old data first, and check the number of data pairs matches the question.
Spreadsheet
Put in column A and in column B. Then =SLOPE(B2:B11, A2:A11) gives and =INTERCEPT(B2:B11, A2:A11) gives . Note the range comes first. =RSQ(B2:B11, A2:A11) gives .
Common mistake
a and b are not m and c
On a calculator showing with and , the line is , not . The number multiplying is always the slope, whatever letter the calculator uses.
A calculator fits to some data and shows and . Write the equation of the least-squares line in the form .
Worked example
Fit a least-squares line to used-car prices
A Brisbane car dealer lists the age and price of ten used Toyota Corollas.
| Age (years) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Price ($'000) | 31.5 | 27.8 | 28.4 | 23.0 | 24.1 | 19.6 | 18.9 | 17.5 | 13.2 | 14.6 |
Use technology to determine the equation of the least-squares line that can be used to predict price from age. Give the slope and intercept correct to two decimal places.
Step 1: Identify the variables
We predict price from age, so age is the explanatory variable () and price is the response variable ().
The variable you predict from always goes in the list; swapping the lists gives a different line.
Step 2: Enter the data and run the linear regression
In paired-variable statistics mode (), enter the ages in the list and the prices in the list. Check that .
Step 3: Read off the slope and intercept
The calculator gives (the slope) and (the intercept). It also gives .
Step 4: Write the equation in context
with price in thousands of dollars and age in years.
Step 5: Check with the mean point
The mean age is and the mean price is . Using the unrounded values, . ✓
The least-squares line always passes through , so this catches a swapped slope and intercept.
Try it yourself first, then check the working one step at a time.
Answer
, with price in thousands of dollars and age in years.
Method
Decide which variable is explanatory; it goes in the list.
Run the regression: is the slope, the intercept.
Round as asked and write the equation with the variable names and units.
Watch out for
Writing by mixing up the calculator's and .
Leaving the answer as without saying what and are.
Use technology to fit a least-squares line to this data, with as the explanatory variable. Give the slope and the intercept .
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 3 | 5 | 4 | 7 | 8 |
Exam tip
Write it in context
QCAA marking guides give a mark for the equation with the variable names. is incomplete unless you have said what and are. Use the variable names, or define and in words.
Why square the residuals?
Adding raw residuals doesn't work: positive and negative residuals cancel, and every line through has residuals that sum to exactly 0, however badly it fits.
Squaring makes every residual count, and it punishes big misses more than small ones. It also gives a line with tidy formulas: and , which you'll use in the next lesson.
Summary
The least-squares line minimises the sum of the squared residuals. is explanatory, is response.
Fit it with technology from the raw data: a calculator's gives the slope as and the intercept as .
In a spreadsheet use
=SLOPE(y, x)and=INTERCEPT(y, x), range first.Write the final equation with the variable names and round as asked.
The line always passes through — a quick check on your equation.
Finished the lesson?
Try the 3 quick checks above and the lesson completes itself, or mark it complete now.