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 .
- 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 . The model gives a prediction at each observed input. The residual is the vertical difference between the observation and the line. Ordinary least squares chooses the slope and intercept that minimise
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.
How do you calculate the best-fit slope and intercept?
Compute the input mean and response mean . Use centred differences to calculate the slope, then place the intercept so the line passes through the pair of means.
The numerator tracks joint changes in and . 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 . The remaining objective is a quadratic in . Its squared coefficient is 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 from , fit in that direction. Regressing on 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 , , , and . A sketch through and gives a rise of 4 over a run of 3, hence slope and intercept . Direct least squares gives slope and intercept 2. The sketched line has squared residuals that sum to . The fitted line reduces that sum to . A reasonable visual estimate can differ from the least-squares answer. OpenStax, Fitting Linear Models to Data
The fitted line predicts at , inside the observed input range. This is interpolation. Predicting at 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 , , , and .
- The means are and .
- The sum of centred cross-products is 7, and the input sum of squares is 5. The slope is .
- The intercept is . Thus .
Example 2
Compare the fitted line with the candidate on the same observations.
- The fitted predictions are 1.9, 3.3, 4.7 and 6.1. The residuals are .
- The squared residuals sum to .
- 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 .
- The input lies between 1 and 4, the observed extremes.
- Substitute to get .
- 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.
- The model is , so the intercept is fixed at zero.
- Minimising gives .
- That slope is about 1.567. The SSE is , higher than the unconstrained fit's 0.20.
Example 5
Find what the data , , and can tell you about a line.
- Every input is 2, so the centred input sum of squares is zero.
- At , the value minimising squared residuals is the mean response 4.
- Any line passing through gives the same fitted responses at the observed inputs. The slope is not identified.
Example 6
What happens if you add observation to the first dataset?
- Recompute the mean pair, now .
- The centred cross-product sum is and the input sum of squares is 50. The slope is .
- The intercept is . 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
- A line predicts 7 and the observation is 9. Find the residual and its square.
- What line fits the points and with SSE zero?
- Can a line with an intercept have lower SSE than a line through the origin fitted to the same data?
Answers and Reasoning
- The residual is and its square is 4.
- The slope is 2 and the intercept is 1. The line passes through both points.
- 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
- Calculate slope and average rate of change
- Understand why deviations are squared in variance
- Use a dataset to practise interpreting a statistical claim
Research Behind This Article
- NIST/SEMATECH, e-Handbook of Statistical Methods — Least Squares. NIST
- NIST/SEMATECH, Linear Least Squares Regression. NIST
- University of Illinois Urbana-Champaign, CS 357 — Least Squares Fitting. Illinois
- Dartmouth College, Least Squares (Linear Regression). Dartmouth
- OpenStax, Introductory Statistics 2e — The Regression Equation. OpenStax