- Linear regression — least squares line · coming soon
- Using regression for prediction — and its limits · coming soon
Two careful lines, two different answers
Go back to the twelve students from the last three lectures, with their hours of revision and their exam scores. Lena and Kwame each draw a line of best fit, exactly as the lecture on lines of best fit showed. They put a ruler through the mean point and turn it until the points are mixed above and below along the whole line.
Lena's line has a gradient of 3.4. Kwame's line has a gradient of 3.1. Both lines pass through the mean point, and both look mixed. Now a student plans to revise for 14 hours. Lena's line predicts about 79.1%. Kwame's line predicts about 77.3%.
Which prediction is right? Neither of them can prove the other wrong. The method had one exact rule, the mean point, and one judgement, the gradient. Two careful eyes made two different judgements.
The test from that lecture cannot settle it. The gaps above a line balance the gaps below it for every line through the mean point: Lena's, Kwame's, and even a flat line. Balance chose the point. It cannot choose the gradient.
The last lecture ended with a promise: the same gaps from the mean point would calculate the line exactly. Here it is. It is called the least squares regression line, and it removes the judgement completely.
The gaps get their proper name
Take any straight line drawn through a scatter graph. For each point, the line gives a predicted value of . Write it , and read it "y-hat". The hat means "predicted by the line".
The vertical gap between the point and the line now gets its proper name. It is the residual.
A point above the line has a positive residual. The line predicted too little. A point below the line has a negative residual. The line predicted too much.
Why measure the gap vertically? Because the line predicts a score from a number of hours. The error in that prediction is an error in the score, and scores are measured up the side.
Why the residuals are squared
We want one total that is small for a good line and large for a bad one. Adding up the residuals fails: for every line through the mean point, they add up to exactly 0. That was the balance from the lecture on lines of best fit.
The signs must go. There are two ways to remove them: use the size of each residual, or square each residual. Least squares squares them, for three reasons.
- Squares are never negative. A residual of −5 and a residual of +5 both give 25. Nothing cancels.
- Large misses count far more than small ones. A residual of 10 adds 100 to the total. A residual of 2 adds only 4. So one point missed by 10 counts as more than five points each missed by 2. A line that badly misses one student is punished hard.
- Squares give one clear lowest point. The total changes smoothly as the line turns, with exactly one lowest value, and algebra can find it. With sizes instead of squares, several lines can tie.
A residual is , the actual value minus the value predicted by the line.
Of all the straight lines you could draw, the least squares regression line is the one that makes the total of the squared residuals, , as small as possible.
Notice what the definition does not say. The line need not pass through any point, and it is not the closest line to every point. It is the best line by one exact measure, and everyone who uses that measure gets the same line.
Turn the line and watch the total
Every line through the mean point balances, so turn one about the mean point and watch what happens to the squares. Each gradient you try adds one point to the second graph, so you build the graph of the total yourself.
Change the gradient with the slider or the − / + buttons. The line turns about the mean point, and a square is drawn on every residual. The score axis starts at 30%. Each gradient you try adds a point to the second graph. Try at least six gradients, then hunt for the lowest total.
Presets:
Gradient: 1
The graph shows the total of squared residuals for every line through the mean point of the revision data, and the table gives the total at seven gradients.
| Gradient | Total of squared residuals |
|---|---|
| 0 | 2326.7 |
| 1 | 1164.7 |
| 2 | 426.7 |
| 3.1 (Kwame) | 104.6 |
| 3.24 | 100.4 (lowest) |
| 3.4 (Lena) | 105.8 |
| 5 | 756.7 |
You should have seen a U. A flat line gives a total of about 2326.7. As the line turns towards the points, the squares shrink. The total is lowest at a gradient of about 3.24, where it is about 100.4. Turn further, and the squares grow again.
Lena's line gives about 105.8 and Kwame's about 104.6. Both eyes were good, but neither was best.
Why the best line passes through the mean point
The graph builder only turned lines about the mean point. Could a line somewhere else do better? No.
Take a line through the mean point. Its residuals add up to 0. Now slide the line up by an amount , without turning it. Every predicted value rises by , so every residual becomes . Square it and add up for all twelve students:
The middle term vanished, because . So any slide, up or down, adds to the total. Sliding the best line up by 2 adds : the total rises from about 100.4 to about 148.4. The same argument works for any gradient. So the best line of every gradient passes through the mean point, and so does the best line of all. The eye method's one exact rule now has a second reason.
Lena checks her line. Its residuals add up to 0. She says: "So my line is the least squares line." Is she right?
Show the answer
No. Residuals that add up to 0 only show that the line passes through the mean point. Every line through the mean point has that property, whatever its gradient. The least squares line also needs the one gradient that makes the total of the squared residuals smallest. Lena's gradient, 3.4, gives a total of about 105.8, not the lowest, 100.4.
Finding the gradient exactly
The graph of the total is a U, like a quadratic graph. That is not a coincidence: the total is a quadratic in the gradient, and a quadratic's lowest point can be found exactly. This is the hard part of the lecture, so spend time here.
Use the gaps from the last lecture. For each student, the -gap is and the -gap is . Step through the argument below. Each line comes with the reason it is allowed.
Press Next to add one step. Read the reason before you go on.
Each step gives one line and the reason it is allowed.
Step 1. Every line through the mean point: ŷ − ȳ = b(x − x̄). Start at the mean point. Go x − x̄ across, and the line rises b times as much.
Step 2. Residual = (y − ȳ) − b(x − x̄). The actual value minus the predicted value, both measured from ȳ.
Step 3. Call the x-gap p and the y-gap q. Then residual = q − bp. Short names make the next step easier to read.
Step 4. Residual² = q² − 2bpq + b²p². Square the bracket (q − bp).
Step 5. Total S = Σq² − 2bΣpq + b²Σp². Add up for every point. The three totals are numbers from the data.
Step 6. Revision data: S = 2326.7 − 1374b + 212b². Here Σq² ≈ 2326.7, Σpq = 687 and Σp² = 212.
Step 7. S is a quadratic in b, with a positive b² term. So its graph is a U with one lowest point.
Step 8. The lowest point of Ab² + Bb + C is at b = −B ÷ (2A). This comes from completing the square.
Step 9. b = 2Σpq ÷ 2Σp² = Σpq ÷ Σp² = 687 ÷ 212 ≈ 3.24. Here A = Σp² and B = −2Σpq, and the 2s cancel.
Result. The least squares gradient is b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², the total of the products divided by the total of the squared x-gaps.
Look at what the gradient is built from. The top, , is the total of the products from the last lecture: the same number that sits on top of . The bottom is the total of the squared -gaps, which also appeared in . For the revision data the last lecture already found both: 687 and 212.
, where
and .
Find first. The formula for makes the line pass through the mean point.
For the revision data:
So the least squares regression line is , between Lena's line and Kwame's, and nobody had to judge it.
Regression books put the intercept first, . It is the same kind of line as : is the gradient and the intercept. Many exam questions also name the two totals: for the total of the products and for the total of the squared -gaps, so . When a question gives only totals such as and , use
These are the same totals, rearranged: multiply out the brackets and use .
What the gradient and the intercept say
The gradient, , says: for each extra hour of revision, the predicted score rises by about 3.24 percentage points. Say "predicted", or "tends to". A pattern is not proof of a cause, and a calculated line does not change that.
The intercept, , is the predicted score for 0 hours of revision. The data run from 2 to 16 hours, so this is an extrapolation, and extrapolations are unreliable. The same rule holds for every prediction. Inside the range of the data (interpolation), it is reasonable when the correlation is strong. Outside it, the line is a guess about students nobody measured.
Predicting y from x, not x from y
The line was built from vertical residuals: errors in . So it is designed to predict when is known. Many books call the explanatory variable and the response variable. The line of on predicts the response from the explanatory variable.
Suppose instead you know a student's score and want to estimate the hours. Now the error that matters is an error in hours, measured across. Making the squares of those horizontal gaps as small as possible gives a different line, the regression line of on . Its formula swaps the roles: , where is the total of the products divided by the total of the squared -gaps.
For the revision data, . Drawn on the same axes, this line has a gradient of , not 3.24.
Both lines pass through the mean point. They are close here because , and they swing further apart as the correlation weakens. They become one line only when every point lies exactly on a straight line. So use the line that matches the question. Most exam questions say which variable is which.
Worked examples
A plant nursery measured eight young trees, aged from 4 to 12 months (invented data). For age in months and height in cm: , , , , . (a) Find the regression line of on . (b) Interpret and . (c) Predict the height of a tree aged 10 months, and say how far you trust it.
(a) First the means: and . Then the two totals:
So . Keep all those digits. Then . The line is .
(b) The gradient: for each extra month of age, the predicted height rises by about 4.83 cm. The intercept: the line predicts a height of about 21.4 cm at age 0. The trees were 4 to 12 months old, so age 0 is outside the data. The intercept is an extrapolation. It need not be the real height of a seedling.
(c) cm. Age 10 months is inside the range 4 to 12, so this is an interpolation. It is reasonable, as long as the scatter graph shows a straight-line pattern.
Check: the line must pass through the mean point. At , ✓.
The five music learners from the last lecture practised for hours and then played bars without a mistake: (1, 3), (2, 6), (3, 5), (4, 8), (5, 8). Find the least squares line, its residuals and the total of squared residuals.
The last lecture found , , a total of products of 12, and a total of squared -gaps of 10. So and . The line is .
| x | y | Predicted | Residual | Residual² |
|---|---|---|---|---|
| 1 | 3 | 3.6 | −0.6 | 0.36 |
| 2 | 6 | 4.8 | 1.2 | 1.44 |
| 3 | 5 | 6.0 | −1.0 | 1.00 |
| 4 | 8 | 7.2 | 0.8 | 0.64 |
| 5 | 8 | 8.4 | −0.4 | 0.16 |
| Total | 0 | 3.6 |
The residuals add up to 0, as they must for a line through the mean point. The total of squared residuals is 3.6. No other straight line gives a smaller total for these five points.
Check: the predicted values add to 30, the same as the bars ✓.
A student from the same class scored 75%, but forgot how many hours she revised. Estimate her hours of revision from the revision data.
Now the score is known and the hours are wanted. So use the line of on : . With : hours.
Solving instead gives about 13.0 hours, from a line built to make errors in scores small, not errors in hours.
Answer: about 12.8 hours. The two answers differ by only 0.2 hours here, because the correlation is very strong.
Check: 75% is inside the range of scores (38% to 84%), so this is an interpolation, and 12.8 hours lies inside the range of hours.
In Worked Example 1, rounding to 4.83 first gives , not 21.38. The intercept is multiplied by , so a small rounding in grows. Keep unrounded, or in your calculator's memory, until is found. Round both only at the end.
The line of on predicts from . To estimate from a known , use the line of on . The two lines are the same only when every point lies on a straight line.
An exact line is not an exact truth. The calculation says nothing about values far outside the data, and the intercept is often one of them. State the range of the data, and give a warning with any extrapolation.
Work each one on paper first. Open a hint only when you are stuck.
-
A shop records, for 10 weeks, the amount spent on advertising, (in hundreds of dollars), and the number of items sold, (in hundreds). , , , . Find the regression line of on , and interpret its gradient.
Hint 1
Find and first. Then find and .
Hint 2
and .
Full answer
and . and . So and . The line is . Gradient: for each extra $100 spent on advertising, predicted sales rise by about 275 items. This is an association; it does not prove that the advertising caused the sales. Check: at , , the mean ✓.
-
Five points: (1, 2), (2, 3), (3, 5), (4, 4), (5, 6). (a) Find the least squares line. (b) Find its five residuals and the total of their squares. (c) The line passes exactly through three of the five points. Find its total of squared residuals. Which line is better by the least squares measure?
Hint 1
and . Make a table of -gaps, -gaps, products and squared -gaps.
Hint 2
The products add to 9 and the squared -gaps add to 10.
Full answer
(a) and , so . (b) Predicted values 2.2, 3.1, 4.0, 4.9, 5.8. Residuals −0.2, −0.1, 1.0, −0.9, 0.2, which add to 0. Squares 0.04, 0.01, 1.00, 0.81, 0.04, total 1.9. (c) For , the residuals are 0, 0, 1, −1, 0, with squares totalling 2. So the least squares line is better, 1.9 against 2, even though it passes through none of the points. Passing through many points is not the measure. Check: also passes through the mean point (3, 4), so its residuals add to 0 too; only the gradient differs.
-
For 12 phones, the regression line of battery level (in %) on minutes of video watched is . The phones watched between 10 and 90 minutes. (a) Interpret the gradient and the intercept. (b) Predict the battery level after 50 minutes and after 150 minutes, and comment on each. (c) Explain why this line should not be used to find how many minutes it takes to reach 40%.
Hint 1
A negative gradient means the predicted value falls as rises. Is inside the data?
Hint 2
For (c), ask which variable the line was built to predict.
Full answer
(a) For each extra minute of video, the predicted battery level falls by about 0.8 percentage points. The intercept, 96%, is the predicted level after 0 minutes. That is outside the data (10 to 90 minutes), so it is an extrapolation, though a believable one. (b) After 50 minutes: , an interpolation, reasonable. After 150 minutes: , which is impossible. It is an extrapolation far outside the data, and the battery cannot fall below 0%. (c) The line predicts the battery level from the minutes. To estimate minutes from a battery level, the line of on should be used. The two lines agree only if every point lies on one straight line. Check: ✓.
-
For the five music learners in Worked Example 2, the squared -gaps add up to 18. (a) Find the regression line of on , and write it in the form . (b) Show that it crosses the line of on , , at the mean point. (c) Multiply the gradient of the line of on by the number in the line of on . Compare the answer with , using from the last lecture.
Hint 1
is the total of the products divided by the total of the squared -gaps: .
Hint 2
. Rearrange it to make the subject.
Full answer
(a) , so . Rearranging: , so . Its gradient on the graph is 1.5, steeper than 1.2. (b) Set . Then , so and : the mean point. (c) , and . The answers agree. In fact always, because both are built from the same three totals: . So the two lines are the same only when , when every point is on a straight line. Check: for the revision data, , and ✓.
On 1 January 1801, the astronomer Giuseppe Piazzi, in Palermo, saw a faint point of light moving among the stars: the dwarf planet now called Ceres. He followed it for about 41 days before it was lost in the glare of the Sun. Astronomers had a couple of dozen imperfect measurements and one urgent question: where would it reappear?
The 24-year-old Carl Friedrich Gauss calculated an orbit from those few measurements. On 31 December 1801, the astronomer Franz Xaver von Zach found Ceres within about half a degree of where Gauss had said. Gauss later wrote that he had been using least squares since 1795. Historians still argue about how much it shaped his 1801 calculation. The first person to publish the method was Adrien-Marie Legendre, in 1805, in an appendix to a book on the orbits of comets. He also gave it its name. Gauss published his own account in 1809, and a famous argument about who came first followed.
The word "regression" came from Francis Galton in 1886. Children of very tall parents, he found, tended to be tall but closer to average. He called this "regression towards mediocrity", and the name stayed with the line.
The idea in this lecture has travelled far. Choose a model. Measure its errors. Square them, add them, and make the total as small as possible. Many of the systems that learn from data today are trained in exactly that way. You have just done the simplest version by hand.
- A residual is the actual value minus the value predicted by the line, , measured vertically because the line predicts .
- The least squares regression line is the straight line that makes the total of the squared residuals as small as possible.
- The best line passes through the mean point, because sliding any line away from it adds to the total.
- The total of squared residuals is a quadratic in the gradient, and its lowest point gives , with .
- The gradient is read in context as "per unit of "; the intercept and any value outside the data are extrapolations.
- The line of on predicts , and estimating needs the line of on .