Five points and a line. Each point’s gap to the line is drawn as a square. Watch the squares shrink as the line tilts.
Least squares, literally
Five points, a line, and a square on every gap between them.
A flat line at height 4, the mean of the five heights. Each gap to the line is the side of a square; the squares add to 6.00.
Describe this picture
One panel: five points at (1, 2), (2, 4), (3, 5), (4, 4) and (5, 5), and a straight line. From each point a vertical gap runs to the line, and a square is drawn with that gap as its side. Readouts give the line’s intercept and slope and the total area of the five squares. In the clip, over 8 seconds, the line starts flat at height 4, with total area 6.00; it tilts about the point (3, 4), which is ringed, and stops at the best line, , where the total area is 2.40. After the clip two sliders set the line’s height at and at , from 0 to 7 in steps of 0.1, so the line can be moved to any position; the squares and the total follow.
Residuals and their squares
The vertical gap from a point to the line is its residual: how far the line misses that point, measured in the units of . Points above the line have positive residuals, points below negative ones.
The least-squares line is the line that makes the total area of the squares, the sum of the squared residuals, as small as possible. Watch the readout as the line tilts: 6.0 when flat, falling to 2.4 at the best line. Move the line anywhere after the clip; no position does better than 2.4.
With the line held through the point and tilted to slope , the total area is : 6.0 at slope 0, 3.3 at 0.3, 2.4 at 0.6, 4.0 at 1 and 6.0 again at 1.2. It is a parabola in , lowest at .
Watch out
The gaps are vertical, not the shortest distance to the line. Regression predicts from , so it measures its misses in . Swapping the roles of and gives a different line.
The best line
Write the line , with intercept and slope . As in Covariance and correlation, let
Setting the slopes of the sum of squares to zero, in and in , gives the best line:
The second equation says the best line always passes through the centre of the cloud, . That is why the clip could tilt the line about .
The slope can be written with the sample correlation and the two sample standard deviations and :
The fitted values are and the residuals ; I use , not , which is Euler’s number here. The residuals of the best line always add to 0.
How much is explained:
The spread of about its mean is . What the line leaves over is the residual sum of squares, . Their ratio says how much of the spread the line failed to explain, so
is the share it did explain. For a straight line it equals the squared correlation, , which is where the name comes from.
How well the slope is known
Five points pin a line down only roughly: another five points from the same source would give another slope. To say how roughly, assume each is a true line plus an independent bell-shaped error of a fixed spread.
Estimate that spread from the residuals, dividing by because two numbers, and , were fitted:
Here is the residual standard deviation. The slope’s standard error is , and a 95 % interval for the true slope is
with from t-tests. The line’s height at a point has its own interval, . It is narrowest at and widens away from it: the further you predict from the data, the less you know.
Anscombe’s four data sets
A line and its say nothing about whether a line was the right model.
Four clouds, one line
Anscombe's four data sets: the same line, correlation and R², and four different pictures.
Four data sets of 11 points: a cloud, a curve, a line with one far point, and one point far to the right.
Describe this picture
Four small panels side by side (two by two on a phone), Anscombe’s four data sets of 11 points each, named “a cloud”, “a curve”, “one outlier” and “one lever”. A short clip shows them one after another, then draws the same fitted line, , through each. Readouts give that line, the correlation 0.816 and 0.67, the same for all four. The first is an ordinary cloud around the line; the second is a smooth curve; the third lies on a straight line except for one far point; the fourth has ten points at one and a single point far to the right that sets the slope alone. The panel has a play button and no other control.
Same line, same , four different stories. One number cannot replace the picture: always plot the data and the residuals.
Worked example
1. The five points. and . The deviations of are and of are ; their products are . So , , , and
The correlation is , and , the same slope.
The fitted values are , and the residuals , which add to 0. Their squares add to , so .
2. The slope’s uncertainty. and . The slope’s standard error is . With , the 95 % interval for the slope is , from to . It includes 0: five points do not pin the slope down. The matching test of slope 0 gives with 3 degrees of freedom and a two-sided of .
3. The band. At the line’s height is . At , outside the data, it is : more than twice as wide.
4. The same fit by matrices. The two conditions for the best line are the normal equations of Least squares:
with the sums , , , and . Their solution is .
Try it yourself. With , and , the slope is . The best line predicts at . An of 0.6 does not mean 60 % of the points lie on the line; it means the line accounts for 60 % of the spread of .
Where you’ll meet this
Calibration. A sensor is calibrated by fitting its readings against a reference: the slope is its gain and the intercept its offset.
Trends. A trend line through monthly sales, temperatures or response times is a least-squares fit, often with the band drawn to show how far it can be trusted.
Prediction. Load forecasts, house-price estimates and growth charts start from regression lines, and their bands widen exactly where predictions are wanted most: away from the data.
In signal processing · least squares in filter design
Designing a filter whose response is as close as possible, on average, to a target is a least-squares fit with the filter’s taps as the unknowns, as Optimal FIR design in the signal processing course shows.
Where this leads · a projection
The fitted values are the projection of the data vector onto the plane spanned by the all-ones vector and the vector of values, and the residual vector is perpendicular to both. Projections and Least squares build that picture; software solves it with QR factorisation.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Slope | ||
| Intercept | the line passes through | |
| Residuals | vertical gaps; they add to 0 | |
| Explained share | not a test of the model | |
| Residual spread | two numbers fitted | |
| Slope interval | ||
| Band at | widens away from | |
| Always | plot the data | four clouds, one line |