[Home](https://learningfolio.de5.net/) / [Math](https://learningfolio.de5.net/math/) / [Math glossary](https://learningfolio.de5.net/math/glossary/) / Least Squares Method: Fit a Line and Check Residuals

[Math glossary](https://learningfolio.de5.net/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.

Written by **Learningfolio Team**

We’re subject-matter experts in math, science, parent support, and education, with 100 years of combined experience. We believe in learning, experimenting, and growing, and we review one another’s work.

## On this page

* [TL;DR](https://learningfolio.de5.net/math/least-square-method/#tldr)
* [Key Points](https://learningfolio.de5.net/math/least-square-method/#key-points)
* [What is the least squares method?](https://learningfolio.de5.net/math/least-square-method/#what-is-the-least-squares-method)
* [How do you calculate the best-fit slope and intercept?](https://learningfolio.de5.net/math/least-square-method/#how-do-you-calculate-the-best-fit-slope-and-intercept)
* [How do you check whether the line answers the question?](https://learningfolio.de5.net/math/least-square-method/#how-do-you-check-whether-the-line-answers-the-question)
* [How do you draw and check a line of best fit on a scatter plot?](https://learningfolio.de5.net/math/least-square-method/#how-do-you-draw-and-check-a-line-of-best-fit-on-a-scatter-plot)
* [Worked Least Squares Examples](https://learningfolio.de5.net/math/least-square-method/#worked-least-squares-examples)
* [Mistakes That Change a Least Squares Fit](https://learningfolio.de5.net/math/least-square-method/#mistakes-that-change-a-least-squares-fit)
* [Try It Yourself](https://learningfolio.de5.net/math/least-square-method/#try-it-yourself)
* [Questions About Least Squares](https://learningfolio.de5.net/math/least-square-method/#questions-about-least-squares)
* [Conclusion](https://learningfolio.de5.net/math/least-square-method/#conclusion)
* [Read More](https://learningfolio.de5.net/math/least-square-method/#read-more)
* [Research Behind This Article](https://learningfolio.de5.net/math/least-square-method/#research-behind-this-article)

## 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 $(\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 $(x_i,y_i)$. The model $\hat y_i=b_0+b_1x_i$ gives a prediction at each observed input. The residual $e_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

$$
\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](https://learningfolio.de5.net/assets/versioned/least-squares-regression-residuals.847412088a08.webp)


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 $\bar x$ and response mean $\bar y$. Use centred differences to calculate the slope, then place the intercept so the line passes through the pair of means.

$$
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 $x$ and $y$. The [covariance matrix guide](https://learningfolio.de5.net/math/covariance-matrix/) 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 $b_0=\bar y-b_1\bar x$. The remaining objective is a quadratic in $b_1$. Its squared coefficient is $\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 $y$ from $x$, fit in that direction. Regressing $x$ on $y$ 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)$, $(2,4)$, $(3,8)$, and $(4,8)$. A sketch through $(1,4)$ and $(4,8)$ gives a rise of 4 over a run of 3, hence slope $4/3$ and intercept $8/3$. Direct least squares gives slope $8/5$ and intercept 2. The sketched line has squared residuals that sum to $32/9$. The fitted line reduces that sum to $16/5$. A reasonable visual estimate can differ from the least-squares answer. [OpenStax, Fitting Linear Models to Data](https://openstax.org/books/algebra-and-trigonometry-2e/pages/4-3-fitting-linear-models-to-data)

The fitted line predicts $7.6$ at $x=3.5$, inside the observed input range. This is **interpolation**. Predicting at $x=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](https://learningfolio.de5.net/math/correlation-coefficient-formula/) explains why a single strength measure cannot reveal curvature or determine whether extrapolation is reliable. [OpenStax, Scatter Plots, Correlation, and Regression Lines](https://openstax.org/books/contemporary-mathematics/pages/8-8-scatter-plots-correlation-and-regression-lines)

## Worked Least Squares Examples

### Example 1

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

1. The means are $\bar x=2.5$ and $\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.4$.
3. The intercept is $4-1.4(2.5)=0.5$. Thus $\hat y=0.5+1.4x$.

### Example 2

Compare the fitted line with the candidate $\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.1$.
2. The squared residuals sum to $0.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.5$.

1. The input lies between 1 and 4, the observed extremes.
2. Substitute to get $0.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 $\hat y=bx$, so the intercept is fixed at zero.
2. Minimising $\sum_i(y_i-bx_i)^2$ gives $b=\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\approx0.367$, higher than the unconstrained fit's 0.20.

### Example 5

Find what the data $(2,1)$, $(2,4)$, and $(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=2$, the value minimising squared residuals is the mean response 4.
3. Any line passing through $(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)$ to the first dataset?

1. Recompute the mean pair, now $(4,3.2)$.
2. The centred cross-product sum is $-17$ and the input sum of squares is 50. The slope is $-17/50=-0.34$.
3. The intercept is $3.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)$ and $(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=2$ and its square is 4.
2. The slope is 2 and the intercept is 1. The line $\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

* [Calculate slope and average rate of change](https://learningfolio.de5.net/math/rate-of-change-formula/)
* [Understand why deviations are squared in variance](https://learningfolio.de5.net/math/variance/)
* [Use a dataset to practise interpreting a statistical claim](https://learningfolio.de5.net/math/statistics-questions/)

## Research Behind This Article

1. NIST/SEMATECH, *e-Handbook of Statistical Methods — Least Squares*. [NIST](https://www.itl.nist.gov/div898/handbook/pmd/section4/pmd431.htm)
2. NIST/SEMATECH, *Linear Least Squares Regression*. [NIST](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm)
3. University of Illinois Urbana-Champaign, *CS 357 — Least Squares Fitting*. [Illinois](https://cs357.cs.illinois.edu/textbook/notes/linear-least-squares.html)
4. Dartmouth College, *Least Squares (Linear Regression)*. [Dartmouth](https://engs20book.thayer.dartmouth.edu/plotting-leastsquares)
5. OpenStax, *Introductory Statistics 2e — The Regression Equation*. [OpenStax](https://openstax.org/books/introductory-statistics-2e/pages/12-3-the-regression-equation)
