Probability & Statistics · Linear regression

Linear regression

Simple linear regression fits the straight line ŷ = a + bx that makes the sum of the squared vertical residuals as small as possible. Here you can shift and turn a line of your own to find that minimum, and compare the fitted line with the cloud it summarizes.

Linear regression in this visualization

Simple linear regression predicts yy from xx with the least-squares line y^=a+bx\hat y = a + bx: the straight line with the smallest sum of squared residuals, where a point's residual e=y−y^e = y - \hat y is how far it lies above the line (negative below). With a few points each residual carries a square, so the sum is an area you can see. You can shift and turn a line of your own to look for the least, and compare the fitted line with a simulated cloud of 1000 points.

Why does the line pass through the center (xˉ,yˉ)(\bar x, \bar y)? Write any line as y=h+b(x−xˉ)y = h + b(x - \bar x), with hh its height above xˉ\bar x. Its residuals add up to n(yˉ−h)n(\bar y - h), and its sum of squares splits in two:

Any line's sum of squares: a height part and a slope part
SSE=n(yˉ−h)2+P(b),P(b)=Syy−2b Sxy+b2Sxx\mathrm{SSE} = n(\bar y - h)^2 + P(b), \qquad P(b) = S_{yy} - 2b\,S_{xy} + b^2 S_{xx}

Here SxxS_{xx}, SyyS_{yy} and SxyS_{xy} are the sums of squared and multiplied deviations from the means. The first part depends only on the height and the second only on the slope, so at any slope the best height is yˉ\bar y: the best line passes through the center, and its residuals add up to 0. Turned about the center, the sum follows the parabola P(b)P(b). When the xx values are not all equal, its single lowest point is at b=Sxy/Sxxb = S_{xy}/S_{xx}.

What does R2R^2 measure? The flat line y=yˉy = \bar y is the parabola at slope 0, and its sum of squares is the total variation SST=Syy\mathrm{SST} = S_{yy}. The fitted line leaves SSE\mathrm{SSE}; when the yy values vary, R2=1−SSE/SSTR^2 = 1 - \mathrm{SSE}/\mathrm{SST} is the share of the variation in yy that it accounts for. When both coordinates vary, R2=r2R^2 = r^2, so a correlation of 0.5 accounts for only a quarter. Accounting for is fitting, not causing.

Why is the line flatter than the cloud? In standard units its predictions are z^y=r zx\hat z_y = r\,z_x, so when ∣r∣<1|r| < 1 a point away from xˉ\bar x gets a prediction fewer standard deviations from yˉ\bar y than it lies from xˉ\bar x. In an oval cloud such as the simulated one, the SD line, with slope ±sy/sx\pm s_y/s_x, runs along the cloud as the eye sees it; the regression line is flatter. Nothing pulls the data back: the effect holds for any such data, and predicting xx from yy shows it the other way, with a second line through the center that is steeper on the plot. In such a cloud, averages of yy in narrow strips of xx show the effect, close to the regression line but not exactly on it. The name comes from Francis Galton's 1886 study of heights: the children of very tall or very short parents were on average less extreme.

When can the line mislead? Raising one point by 1 raises its own fitted value by its leverage h=1/n+(x−xˉ)2/Sxxh = 1/n + (x - \bar x)^2/S_{xx}, which grows with the distance from xˉ\bar x. A point alone far out in xx can have leverage near 1: the line follows it, so even when it disagrees with every other point its own residual can look small, and even in a large cloud such a point can turn the line. And rr can be close to 1 while the residuals run positive at both ends and negative in the middle, a curve that no straight line follows.

Things to notice

  • The squares are drawn square on the screen whatever the scales of the axes, so their areas compare like e2e^2.

Controls

  • On the cloud, pressing one of its points shows its residual and leverage; pressing empty space adds a point of your own.
  • With the keyboard, your line's pivot moves with ↑ ↓ and its knob turns 1° a step; a step across the center or a level line stops there, and so does one across the least-squares slope while answers are shown.

Related