The line that fits best · Statistics & Data Science… | StemPy Lab
Course 5.2·Lesson 1 of 3
The line that fits best
A straight line through a scatter plot lets us predict. But which line? This lesson measures how far each point misses a line, its residual, and explains why the best line makes the sum of the squared residuals as small as possible.
about 25 min1 simulation1 practice question1 Python program6 questions to check yourself
This lesson is free to read. Enroll free to keep your place and take the tests.
Lesson 1 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
find the residual of a point from a line, with its sign;
explain why we square the residuals before adding them;
find the sum of squared residuals of a line, and know that the least-squares line makes it smallest.
Drinks on a hot day
A made-up school canteen notes the noon temperature and the cold drinks it sells on six days.
Noon temperature (∘C)
Drinks sold
18
34
21
44
24
47
26
56
29
59
32
70
The scatter plot is a strong, positive, straight-line pattern, with r=0.987. The cook would like a rule: "on a day of x degrees, order about y^ drinks". A straight line gives such a rule. We write it as y^=a+bx. The hat on y^ says that it is a predicted value, not an observed one.
You could draw a line by eye. Two people would draw two different lines. We want one line that everybody agrees on.
The miss of each point
For each day, compare what happened with what the line predicts. The residual is
residual=y−y^=observed−predicted.
Take the line y^=2x, a first guess. On the day of 24∘C it predicts 48 drinks, and the canteen sold 47. The residual is 47−48=−1 drink: the point is 1 drink below the line. A point above the line has a positive residual.
A scatter plot of a made-up canteen. Noon temperature in degrees Celsius runs across from 16 to 34, and cold drinks sold run up from 25 to 80. Six blue dots rise to the right. A grey least-squares line passes among them, and a short red segment joins each dot to the line.
Why square them?
The natural idea is to add the residuals and make the sum small. It does not work. Positive and negative residuals cancel. In fact every line through the point of the means (xˉ,yˉ) has residuals that add up to exactly 0, however badly it fits.
So we square each residual first. A square is never negative, so misses cannot cancel. Squares also punish a big miss more than two small ones. The sum of squared residuals (SSE, for sum of squared errors) is the score of a line: the smaller, the better.
The least-squares line is the one line with the smallest possible SSE. For the canteen it is y^=−9.13+2.432x, with SSE =20.72. No other straight line does better. The first guess y^=2x has SSE =62.
One more point: the least-squares line measures the misses up and down, along the y direction, because y is the quantity we want to predict.
Your turn: new numbers
Try it in Python
The program scores two lines drawn by eye, and then the least-squares line that the statistics module finds.
The usual mistakes
Getting the sign the wrong way round. The residual is observed minus predicted. A point above the line has a positive residual.
Adding the residuals without squaring. They cancel, and every line through the means gives 0.
Measuring the misses across, or at right angles to the line. Least squares measures them up and down.
Thinking the best line passes through the most points. It may pass through none of them.
Remember these
The residual is y−y^, observed minus predicted.
SSE is the sum of the squared residuals, the score of a line.
The least-squares line has the smallest SSE of all straight lines.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
A line predicts 43.5 drinks on a day when the canteen sold 40. What is the residual?
A friend says the best line is the one whose residuals add up to zero. Why is that not enough?
The line y^=1+2x is drawn through the points (1,4), (2,4) and (3,8). Find the residuals and the SSE.
When is the SSE of a line equal to 0?
Why are the misses measured up and down, and not along the x direction?
References
OpenStax, Introductory Statistics, Chapter 12 (Linear Regression and Correlation), the section on the regression equation.
Enroll free in The Least-Squares Line
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.