The line that minimises squared error, why tall parents have shorter children, and how a single point with leverage can swing the whole fit
Draw the best straight line through a cloud of points. It sounds like one task, but it hides three questions: what “best” means, why the best line is always flatter than the one your eye wants to draw, and how a single unusual point can drag the whole answer.
Drag the two handles to make the total area of the squares as small as you can, then press Snap to see how close you came. Switch what the line is asked to make small and the same points give a different best line.
Teal discs: the data. Drag one to move it, tap an empty spot to add a point, tap a point without dragging to remove it. Amber: your line, its two handles, each point’s miss and the square built on it; the filled amber square in the corner has the total area, drawn to the same scale. The white cross is the centre of the cloud, (x̄, ȳ). On the map, each position is one line and brighter means a smaller total; drag the dot there to steer by slope and intercept directly (with a mouse; on a touch screen, tap).
Adrien-Marie Legendre published the method of least squares in 1805: choose the line that makes the sum of the squared misses as small as possible. Carl Friedrich Gauss published it in 1809, said he had been using it since 1795, and gave it a reason, connecting it with the normal distribution of errors: if the misses are independent draws from a normal curve, the least-squares line is the one under which the observed data are most probable.
Squaring is what makes the problem easy. The total is a quadratic in the slope b and the intercept a, so its graph over all lines is a bowl with exactly one lowest point, which is the map in the demo. Setting the two derivatives to zero gives a pair of linear equations with a tidy solution: the slope is b = r · sy / sx, where r is the correlation and sx and sy are the standard deviations, and the line passes through the centre of the cloud, (x̄, ȳ). The same idea, seen as dropping a perpendicular onto a subspace, is how it extends to many predictors (see also the Linear Algebra and Machine Learning modules: regression as projection).
The squares around the flat line at ȳ add up to the total variation in y. The squares around the fitted line are what is left over. The fraction removed is exactly r², which is why r² is described as the share of the variance of y that the line accounts for. “Accounts for” is a statement about arithmetic, not about causes: a high r² says the points lie near a line, not why.
The other choices in the demo are equally legitimate answers to different questions. Measuring misses sideways gives the line for predicting x from y, which is steeper; the two lines agree only when r is ±1. Adding up plain distances instead of squares gives the least-absolute-deviations line, which has no formula and must be found by search, but is much less disturbed by a stray value of y. That is the price of squaring: a miss ten times as long counts a hundred times as much, so least squares will tilt the whole line to appease one distant point. The third demo is about that weakness.
Drag the band over the tallest parents and see where their children’s average falls, then choose a band of children and look back at their parents. On the second tab, give the worst scorers a treatment that does nothing and watch them improve.
Simulated heights, in the spirit of Galton’s 1886 table but not his data: 900 parent and child pairs from a bivariate normal with the same mean and SD in both generations. Teal: the pairs, brighter inside the band; drag on the chart to move it. Dashed white: the SD line, which most people sketch by eye. Amber: the average child of each parent. Violet: the average parent of each child. The dashed ring is where the band’s relatives would average if they were as unusual as the people chosen; the arrow shows where they do.
Francis Galton’s paper “Regression towards Mediocrity in Hereditary Stature” (1886) tabulated the heights of 928 adult children against those of their 205 pairs of parents. He found that the children of tall parents were tall, but less so: on average they deviated from the general average by about two-thirds as much as their parents did. The word regression, now attached to every fitted line, began as his name for that falling back. The cloud in the demo is simulated, not Galton’s table, so that you can change the correlation.
The arithmetic is short. Measure both heights in standard deviations from their means. If parent and child have correlation r, then among parents who are z standard deviations above average, the children average r · z above. Since r is less than 1 for any imperfect relationship, the prediction is always closer to the mean than the thing you predicted from. The line most people draw by eye, the SD line with slope sy / sx, would be right only if r were 1. The regression line has slope r times that. Run the argument from the children instead and the parents average r · z: there are two regression lines, one on each side of the SD line.
That symmetry is the proof that nothing causal is going on. Children do not pull their parents towards the average. Nor is the population shrinking towards its middle: the spread of each generation is the same, because people near the average have children scattered on both sides of it, which replaces the extremes that fell back. Regression to the mean is a property of any imperfect correlation, not of heredity. Whenever a group is picked for being extreme on one measurement, it will be less extreme, on average, on any other measurement that is imperfectly correlated with the first.
The regression fallacy is to explain the falling back by a cause. The “sophomore slump” of a brilliant first season is one example. Daniel Kahneman discussed another: a flight instructor who noticed that trainees praised for an excellent manoeuvre usually did worse next time, and those shouted at for a bad one usually did better, and who concluded that praise harms and punishment helps. Uncontrolled before-and-after studies are the most consequential case, because patients are often enrolled when they are at their worst, and would on average have improved with no treatment at all. The cure is a control group chosen in the same way and left untreated (see also: Designing Experiments).
Drag the rose point up and down in the middle of the cloud, then far out to one side, and compare how much the line moves. Try Anscombe’s four sets, which share one fitted line, and look at the residual plot under each.
Simulated data. Drag anywhere on the chart to move the rose point. Solid amber: the least-squares line through every point. Dashed teal: the line through the teal points alone. The halo round each point grows with its Cook’s distance. The darker band is the 95% confidence band for the height of the true line; the paler, dashed one is the 95% prediction band for a new observation. Both assume a straight-line model with evenly scattered errors, which the residual plot lets you check.
The leverage of point i is hi = 1/n + (xi − x̄)2 / Sxx, where Sxx is the sum of the squared deviations of the x values from their mean. It depends only on x, not on y: it measures how far out the point sits, and so how much a change in its y would move the fitted value there. Leverages lie between 1/n and 1 and add up to 2, the number of coefficients fitted. At leverage 1 the line is forced through the point, which is what happens in Anscombe’s fourth set.
Leverage is potential. Influence needs both leverage and a large residual: a point far out in x that agrees with the trend changes little, and a point far off the line near x̄ can only shift it slightly. R. D. Cook’s distance (1977) combines the two into one number, which equals the total squared change in all the fitted values when the point is deleted, on the scale of the noise. The studentised residual in the demo judges each point against a line fitted without it, because an influential point pulls the line towards itself and so hides its own residual.
The confidence band answers “where is the true line?” and the prediction band answers “where will a new observation fall?”. The second is wider because a new observation carries its own noise on top of the uncertainty about the line, and it does not shrink to nothing however much data you collect. Both flare away from x̄, because an error in the slope matters more the further you go, and beyond the data they also rest on the untested hope that the line stays straight. Extrapolation is where regression is least trustworthy.
The standard errors and both bands assume a linear relationship, independent errors, constant variance and, for small samples, roughly normal errors. The residual plot is the tool for checking them: it should look like a formless horizontal band. Curvature (Anscombe’s second set) means the straight line is the wrong model; a fan means the noise grows with the fitted value, so the bands are too narrow at one end and too wide at the other. The robust line in the demo is the Theil–Sen line, whose slope is the median of the slopes between every pair of points. An unusual point is not necessarily a wrong one, so a disagreement between the robust line and least squares is a reason to look at the data, not a licence to delete from it.
Spot a mistake or have an idea?