Math glossary

Least Squares Method: Fit a Line and Check Residuals

Fit a regression line by minimising squared residuals. Calculate slope and intercept, compare fits, and check the effect of new data.

TL;DR

The least squares method chooses the model with the smallest sum of squared residuals. For a fitted line, each residual is the observed response minus the line's prediction at the same input.

Key Points

  • Square each residual before adding so positive and negative errors do not cancel.
  • With an intercept, the fitted line passes through (x¯,y¯)(\bar x,\bar y).
  • A straight-line slope requires at least two distinct input values.
  • A fit forced through the origin uses a different slope formula.
  • A small residual sum alone does not justify extrapolation or a causal claim.

What is the least squares method?

A common task is to fit a line to data pairs (xi,yi)(x_i,y_i). The model y^i=b0+b1xi\hat y_i=b_0+b_1x_i gives a prediction at each observed input. The residual ei=yi−y^ie_i=y_i-\hat y_i is the vertical difference between the observation and the line. Ordinary least squares chooses the slope and intercept that minimise

SSE=∑i=1n(yi−b0−b1xi)2.\mathrm{SSE}=\sum_{i=1}^n(y_i-b_0-b_1x_i)^2.

SSE means sum of squared errors or residuals. A positive residual means the model predicted too little. A negative residual means it predicted too much. Squaring prevents them from cancelling and gives a large residual greater influence. NIST distinguishes this choice of objective from assumptions needed for statistical inference.

Four observed points lie above and below a fitted line, with vertical segments showing their residuals
Least squares chooses a line using vertical differences at the observed input values.

How do you calculate the best-fit slope and intercept?

Compute the input mean x¯\bar x and response mean y¯\bar y. Use centred differences to calculate the slope, then place the intercept so the line passes through the pair of means.

b1=∑i(xi−x¯)(yi−y¯)∑i(xi−x¯)2,b0=y¯−b1x¯.b_1=\frac{\sum_i(x_i-\bar x)(y_i-\bar y)}{\sum_i(x_i-\bar x)^2}, \qquad b_0=\bar y-b_1\bar x.

The numerator tracks joint changes in xx and yy. The covariance matrix guide gives a related interpretation. The denominator measures variation in the inputs. If it is zero, the data do not identify a distinct slope.

To see why the equations minimise the objective, hold the slope fixed. The best intercept centres the residuals at zero, yielding b0=y¯−b1x¯b_0=\bar y-b_1\bar x. The remaining objective is a quadratic in b1b_1. Its squared coefficient is ∑i(xi−x¯)2>0\sum_i(x_i-\bar x)^2>0 when the inputs vary. Setting its derivative to zero gives the slope above, the unique minimiser in that case. The University of Illinois text also presents the general least-squares equations in matrix form.

How do you check whether the line answers the question?

Inspect a plot and residuals as well as the SSE. If residuals show a curve, a straight line may miss an important pattern. A point far from most inputs can have considerable influence on the slope. Verify an unusual value and consider the context before excluding it.

A line can predict between the measured inputs. Predictions far beyond them depend heavily on whether the pattern continues. A fitted association also does not establish causation. If your objective is vertical prediction of yy from xx, fit in that direction. Regressing xx on yy minimises horizontal errors in the original view and can give a different line.

How do you draw and check a line of best fit on a scatter plot?

Plot the paired observations first. For a visual estimate, sketch a line through the middle of the point cloud. Look for points above and below across the horizontal range. Choose two clearly readable points on the sketched line to calculate its slope. They need not be observations in the dataset.

For example, suppose the observed points are (1,4)(1,4), (2,4)(2,4), (3,8)(3,8), and (4,8)(4,8). A sketch through (1,4)(1,4) and (4,8)(4,8) gives a rise of 4 over a run of 3, hence slope 4/34/3 and intercept 8/38/3. Direct least squares gives slope 8/58/5 and intercept 2. The sketched line has squared residuals that sum to 32/932/9. The fitted line reduces that sum to 16/516/5. A reasonable visual estimate can differ from the least-squares answer. OpenStax, Fitting Linear Models to Data

The fitted line predicts 7.67.6 at x=3.5x=3.5, inside the observed input range. This is interpolation. Predicting at x=12x=12 is extrapolation. A line computed from observations at 1 through 4 gives little direct evidence about what happens at 12. Check the question and measure the prediction against new data when available. The correlation guide explains why a single strength measure cannot reveal curvature or determine whether extrapolation is reliable. OpenStax, Scatter Plots, Correlation, and Regression Lines

Worked Least Squares Examples

Example 1

Fit a line with intercept to (1,2)(1,2), (2,3)(2,3), (3,5)(3,5), and (4,6)(4,6).

  1. The means are x¯=2.5\bar x=2.5 and y¯=4\bar y=4.
  2. The sum of centred cross-products is 7, and the input sum of squares is 5. The slope is 7/5=1.47/5=1.4.
  3. The intercept is 4−1.4(2.5)=0.54-1.4(2.5)=0.5. Thus y^=0.5+1.4x\hat y=0.5+1.4x.

Example 2

Compare the fitted line with the candidate y^=1+x\hat y=1+x on the same observations.

  1. The fitted predictions are 1.9, 3.3, 4.7 and 6.1. The residuals are 0.1,−0.3,0.3,−0.10.1,-0.3,0.3,-0.1.
  2. The squared residuals sum to 0.01+0.09+0.09+0.01=0.200.01+0.09+0.09+0.01=0.20.
  3. The alternative has residuals 0, 0, 1 and 1, giving SSE 2. The least-squares line has the lower SSE.

Example 3

Use the fitted line to predict at x=3.5x=3.5.

  1. The input lies between 1 and 4, the observed extremes.
  2. Substitute to get 0.5+1.4(3.5)=5.40.5+1.4(3.5)=5.4.
  3. This is an interpolation. The line can be evaluated outside that range, but those predictions would be extrapolations.

Example 4

Fit those four observations with a line constrained to pass through the origin.

  1. The model is y^=bx\hat y=bx, so the intercept is fixed at zero.
  2. Minimising ∑i(yi−bxi)2\sum_i(y_i-bx_i)^2 gives b=∑ixiyi/∑ixi2=47/30b=\sum_i x_i y_i/\sum_i x_i^2=47/30.
  3. That slope is about 1.567. The SSE is 11/30≈0.36711/30\approx0.367, higher than the unconstrained fit's 0.20.

Example 5

Find what the data (2,1)(2,1), (2,4)(2,4), and (2,7)(2,7) can tell you about a line.

  1. Every input is 2, so the centred input sum of squares is zero.
  2. At x=2x=2, the value minimising squared residuals is the mean response 4.
  3. Any line passing through (2,4)(2,4) gives the same fitted responses at the observed inputs. The slope is not identified.

Example 6

What happens if you add observation (10,0)(10,0) to the first dataset?

  1. Recompute the mean pair, now (4,3.2)(4,3.2).
  2. The centred cross-product sum is −17-17 and the input sum of squares is 50. The slope is −17/50=−0.34-17/50=-0.34.
  3. The intercept is 3.2−(−0.34)(4)=4.563.2-(-0.34)(4)=4.56. One point far from the other inputs changed the sign of the fitted slope.

Mistakes That Change a Least Squares Fit

Mistake 1: Minimising the sum of signed residuals

A positive and negative residual can cancel even when both are large. The least-squares objective adds squared residuals.

Mistake 2: Using the formula for a fitted intercept when the intercept is fixed

Decide whether the model includes a free intercept. A line forced through zero fits a different set of candidate lines.

Mistake 3: Making causal claims from a line fit

A slope summarises an association in the model. An experiment or other supporting design is needed to justify a causal interpretation.

Try It Yourself

  1. A line predicts 7 and the observation is 9. Find the residual and its square.
  2. What line fits the points (1,3)(1,3) and (2,5)(2,5) with SSE zero?
  3. Can a line with an intercept have lower SSE than a line through the origin fitted to the same data?

Answers and Reasoning

  1. The residual is 9−7=29-7=2 and its square is 4.
  2. The slope is 2 and the intercept is 1. The line y^=1+2x\hat y=1+2x passes through both points.
  3. Yes. Allowing an intercept includes all through-origin lines plus other candidates. The best possible SSE can decrease or stay the same.

Questions About Least Squares

Is the method limited to a line?

No. The objective can combine squared residuals for polynomial and other models. A model is linear in its parameters when its unknown coefficients enter as linear combinations, even if the curves are nonlinear in the input.

Is SSE enough to compare datasets of different sizes?

SSE is a total and depends on the units and number of observations. To compare prediction accuracy under the same response units, consider a per-observation error measure and use comparable evaluation data.

Conclusion

  • Write the prediction model and define residuals.
  • Calculate the line that minimises their squared sum.
  • Check the data pattern, domain, and purpose before interpreting a fit.

Read More

Research Behind This Article

  1. NIST/SEMATECH, e-Handbook of Statistical Methods — Least Squares. NIST
  2. NIST/SEMATECH, Linear Least Squares Regression. NIST
  3. University of Illinois Urbana-Champaign, CS 357 — Least Squares Fitting. Illinois
  4. Dartmouth College, Least Squares (Linear Regression). Dartmouth
  5. OpenStax, Introductory Statistics 2e — The Regression Equation. OpenStax