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 y=mx+cy = mx + c 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 mm and intercept cc 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 r=0.946r = 0.946.

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.

Interactive

Fit a line to the café data

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 y=mx+cy = mx + c that minimises the sum of the squared residuals. Here

  • xx is the explanatory variable and yy is the response variable,
  • mm is the slope (gradient) and cc is the yy-intercept.

It always passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}).

What does the least-squares line make as small as possible?

Choose one answer

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 m=3.2345…m = 3.2345\ldots and c=−21.8598…c = -21.8598\ldots, so to two decimal places

iced coffees sold=3.23×temperature−21.86\text{iced coffees sold} = 3.23 \times \text{temperature} - 21.86

On a 31 °C day the model predicts 3.23×31−21.86=78.273.23 \times 31 - 21.86 = 78.27, or about 78 iced coffees.

Fitting the least-squares line with technology

  1. Decide which variable is explanatory

    It goes in the xx list. The response variable goes in the yy list. Swapping them gives a different line.

  2. 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 xx with its yy.

  3. Read the slope and intercept

    Find them in the regression menu. Careful: many calculators write the line as y=a+bxy = a + bx, so bb is the slope and aa is the intercept.

  4. Round and write the equation in context

    Round as the question asks, then replace xx and yy with the variable names.

Scientific calculator

Paired-variable statistics mode, y=a+bxy = a + bx. It gives aa (intercept), bb (slope) and rr. Clear old data first, and check the number of data pairs nn matches the question.

Spreadsheet

Put xx in column A and yy in column B. Then =SLOPE(B2:B11, A2:A11) gives mm and =INTERCEPT(B2:B11, A2:A11) gives cc. Note the yy range comes first. =RSQ(B2:B11, A2:A11) gives R2R^2.

Common mistake

a and b are not m and c

On a calculator showing y=a+bxy = a + bx with a=−21.86a = -21.86 and b=3.23b = 3.23, the line is y=3.23x−21.86y = 3.23x - 21.86, not y=−21.86x+3.23y = -21.86x + 3.23. The number multiplying xx is always the slope, whatever letter the calculator uses.

A calculator fits y=a+bxy = a + bx to some data and shows a=12.4a = 12.4 and b=0.85b = 0.85. Write the equation of the least-squares line in the form y=mx+cy = mx + c.

Reads asYour answer shows here as maths.

Worked example

Fit a least-squares line to used-car prices

  • Simple familiar
  • Technology active

A Brisbane car dealer lists the age and price of ten used Toyota Corollas.

Age (years)12345678910
Price ($'000)31.527.828.423.024.119.618.917.513.214.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.

5 steps

Try it yourself first, then check the working one step at a time.

Method

  1. Decide which variable is explanatory; it goes in the xx list.

  2. Run the y=a+bxy = a + bx regression: bb is the slope, aa the intercept.

  3. Round as asked and write the equation with the variable names and units.

Watch out for

  • Writing y=32.71x−1.97y = 32.71x - 1.97 by mixing up the calculator's aa and bb.

  • Leaving the answer as y=−1.97x+32.71y = -1.97x + 32.71 without saying what xx and yy are.

Use technology to fit a least-squares line to this data, with xx as the explanatory variable. Give the slope mm and the intercept cc.

xx12345
yy35478

Exam tip

Write it in context

QCAA marking guides give a mark for the equation with the variable names. y=3.23x−21.86y = 3.23x - 21.86 is incomplete unless you have said what xx and yy are. Use the variable names, or define xx and yy in words.

Why square the residuals?

Adding raw residuals doesn't work: positive and negative residuals cancel, and every line through (xˉ,yˉ)(\bar{x}, \bar{y}) 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: m=rsysxm = r\frac{s_y}{s_x} and c=yˉ−mxˉc = \bar{y} - m\bar{x}, which you'll use in the next lesson.

Summary

  • The least-squares line y=mx+cy = mx + c minimises the sum of the squared residuals. xx is explanatory, yy is response.

  • Fit it with technology from the raw data: a calculator's y=a+bxy = a + bx gives the slope as bb and the intercept as aa.

  • In a spreadsheet use =SLOPE(y, x) and =INTERCEPT(y, x), yy range first.

  • Write the final equation with the variable names and round as asked.

  • The line always passes through (xˉ,yˉ)(\bar{x}, \bar{y}) — 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.

Lesson 1 of 70% read

Save your progress with a free account