Skip to content

Linear regression

The least-squares line makes the total area of the residual squares smallest; its slope is the correlation times a ratio of spreads.

Helps to know firstCovariance and correlation

Helps to know first
This topic

First, the picture

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.

intercept
4.00
slope
0.00
total area
6.00
0.00 / 10.60 s
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, y=2.2+0.6xy=2.2+0.6x, where the total area is 2.40. After the clip two sliders set the line’s height at x=1x=1 and at x=5x=5, 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 yy. 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 (3,4)(3,4) and tilted to slope bb, the total area is 6−12b+10b26-12b+10b^2: 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 bb, lowest at b=0.6b=0.6.

Watch out

The gaps are vertical, not the shortest distance to the line. Regression predicts yy from xx, so it measures its misses in yy. Swapping the roles of xx and yy gives a different line.

The best line

Write the line y=a+bxy=a+bx, with intercept aa and slope bb. As in Covariance and correlation, let

Sxx=∑k(xk−xˉ)2,Sxy=∑k(xk−xˉ)(yk−yˉ),Syy=∑k(yk−yˉ)2.\begin{aligned} S_{xx}&=\sum_k(x_k-\bar x)^2,\\ S_{xy}&=\sum_k(x_k-\bar x)(y_k-\bar y),\\ S_{yy}&=\sum_k(y_k-\bar y)^2. \end{aligned}

Setting the slopes of the sum of squares to zero, in aa and in bb, gives the best line:

b^=SxySxx,a^=yˉ−b^ xˉ.\hat b=\frac{S_{xy}}{S_{xx}},\qquad \hat a=\bar y-\hat b\,\bar x.

The second equation says the best line always passes through the centre of the cloud, (xˉ,yˉ)(\bar x,\bar y). That is why the clip could tilt the line about (3,4)(3,4).

The slope can be written with the sample correlation rr and the two sample standard deviations sxs_x and sys_y:

b^=r sysx.\hat b=r\,\frac{s_y}{s_x}.

The fitted values are y^k=a^+b^xk\hat y_k=\hat a+\hat bx_k and the residuals ε^k=yk−y^k\hat\varepsilon_k=y_k-\hat y_k; I use ε^\hat\varepsilon, not ee, which is Euler’s number here. The residuals of the best line always add to 0.

How much is explained: R2R^2

The spread of yy about its mean is SyyS_{yy}. What the line leaves over is the residual sum of squares, SSres=∑kε^k2\mathrm{SS}_\text{res}=\sum_k\hat\varepsilon_k^2. Their ratio says how much of the spread the line failed to explain, so

R2=1−SSresSyyR^2=1-\frac{\mathrm{SS}_\text{res}}{S_{yy}}

is the share it did explain. For a straight line it equals the squared correlation, r2r^2, 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 yky_k is a true line plus an independent bell-shaped error εk\varepsilon_k of a fixed spread.

Estimate that spread from the residuals, dividing by n−2n-2 because two numbers, a^\hat a and b^\hat b, were fitted:

s2=SSresn−2.s^2=\frac{\mathrm{SS}_\text{res}}{n-2}.

Here ss is the residual standard deviation. The slope’s standard error is s/Sxxs/\sqrt{S_{xx}}, and a 95 % interval for the true slope is

b^±t0.975, n−2 sSxx,\hat b\pm t_{0.975,\,n-2}\,\frac{s}{\sqrt{S_{xx}}},

with t0.975, n−2t_{0.975,\,n-2} from t-tests. The line’s height at a point x0x_0 has its own interval, y^0±t0.975, n−2 s1n+(x0−xˉ)2/Sxx\hat y_0\pm t_{0.975,\,n-2}\,s\sqrt{\tfrac1n+(x_0-\bar x)^2/S_{xx}}. It is narrowest at xˉ\bar x 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 R2R^2 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.

best line, all four
y = 3.00 + 0.500x
correlation
0.816
R²
0.67
0.00 / 7.20 s
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, y=3.00+0.500xy=3.00+0.500x, through each. Readouts give that line, the correlation 0.816 and R2R^2 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 xx 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 R2R^2, four different stories. One number cannot replace the picture: always plot the data and the residuals.

Worked example

1. The five points. xˉ=3\bar x=3 and yˉ=4\bar y=4. The deviations of xx are −2,−1,0,1,2-2,-1,0,1,2 and of yy are −2,0,1,0,1-2,0,1,0,1; their products are 4,0,0,0,24,0,0,0,2. So Sxx=10S_{xx}=10, Sxy=6S_{xy}=6, Syy=6S_{yy}=6, and

b^=610=0.6,a^=4−0.6×3=2.2.\hat b=\frac{6}{10}=0.6,\qquad \hat a=4-0.6\times3=2.2.

The correlation is r=6/60=0.7746r=6/\sqrt{60}=0.7746, and r sy/sx=0.7746×1.2247/1.5811=0.6r\,s_y/s_x=0.7746\times1.2247/1.5811=0.6, the same slope.

The fitted values are 2.8, 3.4, 4.0, 4.6, 5.22.8,\ 3.4,\ 4.0,\ 4.6,\ 5.2, and the residuals −0.8, 0.6, 1.0, −0.6, −0.2-0.8,\ 0.6,\ 1.0,\ -0.6,\ -0.2, which add to 0. Their squares add to 0.64+0.36+1+0.36+0.04=2.40.64+0.36+1+0.36+0.04=2.4, so R2=1−2.4/6=0.6R^2=1-2.4/6=0.6.

2. The slope’s uncertainty. s2=2.4/3=0.8s^2=2.4/3=0.8 and s=0.8944s=0.8944. The slope’s standard error is 0.8944/10=0.28280.8944/\sqrt{10}=0.2828. With t0.975, 3=3.1824t_{0.975,\,3}=3.1824, the 95 % interval for the slope is 0.6±0.900.6\pm0.90, from −0.30-0.30 to 1.501.50. It includes 0: five points do not pin the slope down. The matching test of slope 0 gives tobs=0.6/0.2828=2.121t_\text{obs}=0.6/0.2828=2.121 with 3 degrees of freedom and a two-sided pvalp_\text{val} of 0.1240.124.

3. The band. At x0=3x_0=3 the line’s height is 4.0±1.274.0\pm1.27. At x0=6x_0=6, outside the data, it is 5.8±2.995.8\pm2.99: 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:

[5151555][ab]=[2066],\begin{bmatrix}5&15\\15&55\end{bmatrix}\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}20\\66\end{bmatrix},

with the sums ∑1=5\sum1=5, ∑xk=15\sum x_k=15, ∑xk2=55\sum x_k^2=55, ∑yk=20\sum y_k=20 and ∑xkyk=66\sum x_ky_k=66. Their solution is (2.2, 0.6)(2.2,\ 0.6).

Try it yourself. With r=0.5r=0.5, sy=4s_y=4 and sx=2s_x=2, the slope is 0.5×4/2=10.5\times4/2=1. The best line predicts 2.2+0.6×6=5.82.2+0.6\times6=5.8 at x=6x=6. An R2R^2 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 yy.

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 xx values, and the residual vector is perpendicular to both. Projections and Least squares build that picture; software solves it with QR factorisation.

Reference card

QuantityFormulaNotes
Slopeb^=Sxy/Sxx=r sy/sx\hat b=S_{xy}/S_{xx}=r\,s_y/s_x
Intercepta^=yˉ−b^ xˉ\hat a=\bar y-\hat b\,\bar xthe line passes through (xˉ,yˉ)(\bar x,\bar y)
Residualsε^k=yk−a^−b^xk\hat\varepsilon_k=y_k-\hat a-\hat bx_kvertical gaps; they add to 0
Explained shareR2=1−SSres/Syy=r2R^2=1-\mathrm{SS}_\text{res}/S_{yy}=r^2not a test of the model
Residual spreads2=SSres/(n−2)s^2=\mathrm{SS}_\text{res}/(n-2)two numbers fitted
Slope intervalb^±t0.975, n−2 s/Sxx\hat b\pm t_{0.975,\,n-2}\,s/\sqrt{S_{xx}}
Band at x0x_0y^0±t0.975, n−2 s1/n+(x0−xˉ)2/Sxx\hat y_0\pm t_{0.975,\,n-2}\,s\sqrt{1/n+(x_0-\bar x)^2/S_{xx}}widens away from xˉ\bar x
Alwaysplot the datafour clouds, one line

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look