Euler Hall — free maths guides

Fit a line by least squares

State what least squares minimises and why the deviations are squared. The idea in plain words, a worked example, and a check with the answers shown — part of the free Statistics guides.

The line that leaves the smallest squared gaps

Every straight line through a scatter leaves a vertical gap at each point: the observation'syy minus the line's height there. Least squares chooses the line whose gaps, squared and added, are as small as possible:

min⁡a, b∑i=1n(yi−a−bxi)2.\min_{a,\,b} \sum_{i=1}^{n} (y_i - a - b x_i)^2.

Squared so that a gap above and a gap below cannot cancel, and so that a point twice as far away counts four times as much. Vertical because the model predictsyy fromxx.

Setting the two partial derivatives to zero gives the normal equations, and their solutions are

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

whereSxy=∑(xi−xˉ)(yi−yˉ)S_{xy} = \sum (x_i - \bar x)(y_i - \bar y) andSxx=∑(xi−xˉ)2S_{xx} = \sum (x_i - \bar x)^2.

Why the intercept formula looks the way it does

The intercept is not chosen independently of the slope. Oncebb is known, the line is rotated about the point of the means, anda=yˉ−bxˉa = \bar y - b\bar x is what keeps it passing through that point.

That has a practical consequence worth remembering: the intercept is only meaningful whenx=0x = 0 is inside, or near, the observed range. A fitted intercept of400400 from data whosexx runs from1010 to6060 is the line's height at axx nobody measured, and reporting it as "the value whenxx is zero" is an extrapolation wearing a parameter's name.

Worked example: five points, exact arithmetic

Data:x=−2,−1,0,1,2x = -2, -1, 0, 1, 2 andy=1,3,5,7,9y = 1, 3, 5, 7, 9.

The means.xˉ=0\bar x = 0 andyˉ=5\bar y = 5.

The slope.Sxy=∑xy=(−2)(1)+(−1)(3)+0+1(7)+2(9)=20S_{xy} = \sum xy = (-2)(1) + (-1)(3) + 0 + 1(7) + 2(9) = 20, andSxx=4+1+0+1+4=10S_{xx} = 4 + 1 + 0 + 1 + 4 = 10, sob=20/10=2b = 20/10 = 2.

The intercept.a=5−2(0)=5a = 5 - 2(0) = 5.

So the fitted line isy^=5+2x\hat y = 5 + 2x — and here it happens to pass through every point, which is what a perfect line looks like. Check: the residuals are all zero, and they sum to zero as they must.

By the end you can
  • State what least squares minimises and why the deviations are squared
  • Compute the slope and intercept from a data table
  • Explain why the line passes through the point of the means
  • Say when a fitted intercept is meaningful
Check yourself
  1. 1. Forx=−1,0,1x = -1, 0, 1 andy=1,2,4y = 1, 2, 4, computebb.
  2. 2. Computeaa and write the line.
  3. 3. What is the fitted value atx=2x = 2?
  4. 4. Do the residuals sum to zero?
Answers

1.xˉ=0\bar x = 0,yˉ=7/3\bar y = 7/3;Sxy=(−1)(−4/3)+0+1(5/3)=3S_{xy} = (-1)(-4/3) + 0 + 1(5/3) = 3,Sxx=2S_{xx} = 2, sob=3/2b = 3/2. 2.a=7/3−(3/2)(0)=7/3a = 7/3 - (3/2)(0) = 7/3, soy^=7/3+(3/2)x\hat y = 7/3 + (3/2)x. 3.7/3+3=16/37/3 + 3 = 16/3. 4. Yes — that is the first normal equation, and it holds for any least-squares fit with an intercept.

Work out where to start — free, no accountAnswer six real questions — free, no account