The least-squares line has a simple recipe: the slope is r times the ratio of the standard deviations, and the line passes through the point of the means. This lesson builds the line, reads its slope and intercept in words, and shows why a prediction far outside the data can be nonsense.
about 30 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 2 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
find the slope b=rsxsy and the intercept a=yˉ−bxˉ;
say what the slope and the intercept mean, with their units;
predict inside the data, and explain the danger of extrapolation.
A running club
A made-up running club asks seven members how far they train each week and times them over 5 km.
Training (km a week)
Time (minutes)
10
31.5
15
30.2
20
28.9
25
28.4
30
26.8
40
25.1
50
23.6
The pattern falls: members who train more run faster. r=−0.9942.
The recipe
The least-squares line y^=a+bx has
b=rsxsy,a=yˉ−bxˉ.
Here sx and sy are the standard deviations. We use the sample ones, with n−1, but the ratio sxsy is the same with n. The second formula says that the line always passes through the point of the means (xˉ,yˉ).
Why r times sxsy? The ratio turns a step in x into the matching step in y. The factor r shrinks it when the pattern is loose. If r=0, the slope is 0 and the best prediction is simply yˉ.
For the club, xˉ=27.14 km, yˉ=27.79 minutes, sx=14.10 km and sy=2.797 minutes. So
b=−0.9942×14.102.797=−0.1972 minutes per km;
a=27.79−(−0.1972)×27.14=33.14 minutes.
The line is y^=33.14−0.1972x.
A scatter plot of a made-up running club. Training in km a week runs across from 0 to 160, and 5 km race time in minutes runs up from 0 to 40. Seven blue dots fall gently between 10 and 50 km. A grey line continues far to the right, solid inside the data and dashed beyond it.
Reading the slope and the intercept
The slope has the unit of y per unit of x. Here: each extra kilometre of training a week goes with a race time about 0.197 minutes (about 12 seconds) shorter, on average. Ten more kilometres go with about 2 minutes less.
The intercept is the predicted y when x=0: a member who does not train at all is predicted to take 33.14 minutes. Is that sensible? The nearest data is at 10 km, so it is a guess just outside the data. Often the intercept has no real meaning. For the canteen, y^=−9.13+2.432x predicts −9.13 drinks at 0∘C, which is impossible. It only fixes the height of the line.
Predicting, and its limits
To predict, put a value of x into the line. For 35 km a week, y^=33.14−0.1972×35=26.24 minutes. That is interpolation: 35 km lies inside the measured range, 10 to 50 km.
For 150 km a week, the line predicts 3.55 minutes for 5 km, faster than any human has ever run. That is extrapolation, a prediction outside the range of the data. The line was fitted only between 10 and 50 km. Beyond that, nothing says that the pattern stays straight. Here it clearly does not: race times level off, they cannot fall forever.
For the canteen, 27∘C gives y^=−9.13+2.432×27=56.5 drinks. That is inside the data, and fair to use.
A last warning. The line predicts y from x. To predict the training distance from a race time, you need another line, with the roles swapped. The Maths course on Correlation and Regression works with both lines.
Your turn: new numbers
Try it in Python
The program builds the line of the running club from r, the two standard deviations and the means, and then predicts twice.
The usual mistakes
Turning the ratio upside down. The slope is r times sy over sx.
Forgetting the units of the slope. Say "minutes per km", not just −0.197.
Trusting every intercept. If x=0 is far from the data, the intercept is only the height of the line.
Extrapolating. A line fitted between 10 and 50 km says nothing reliable about 150 km.
Remember these
b=rsxsy and a=yˉ−bxˉ.
The line passes through (xˉ,yˉ).
The slope is the change in y^ for each extra unit of x.
Predict inside the data (interpolation). Be very careful outside it (extrapolation).
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
For some data, r=0.9, sx=3∘C and sy=6 drinks. Find the slope of the least-squares line, with its unit.
A least-squares line has slope 2, and the means are xˉ=10 and yˉ=30. Find the intercept.
What does the slope −0.1972 minutes per km of the running club mean in words?
The canteen line is y^=−9.13+2.432x, from days of 18 to 32∘C. Would you use it for a day of 45∘C?
What is the least-squares line when r=0?
References
OpenStax, Introductory Statistics, Chapter 12 (Linear Regression and Correlation), the sections on the regression equation and on prediction.
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.