Least Squares Regression Calculator
The line of best fit through a set of points — slope, intercept, R² and the residual spread, with the full working.
Least squares chooses the line that minimises the total squared vertical distance from the points.
How the least squares regression calculator works
Least squares chooses the line that minimises the total squared vertical distance from the points. The slope is Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)², and the intercept follows from the fact that the line always passes through (x̄, ȳ).
Squared distances are what make the solution unique and computable in closed form — and also what makes a single outlier able to swing the whole line.
Formula: b = Σ(x−x̄)(y−ȳ) / Σ(x−x̄)²; a = ȳ − b x̄
Worked examples
| Inputs | Slope | Note |
|---|---|---|
| Eight points | 0.904762 | a clear upward trend |
| A perfect line | 2 | slope 2, intercept 1, R² = 100% |
| No relationship | 0 | slope 0 |
FAQFrequently asked questions
What does least squares minimise?
The sum of squared vertical distances from the points to the line. Not perpendicular distances — that is a different method.
Why squares rather than absolute distances?
Because it has a closed-form solution and clean statistical properties. Minimising absolute distances is more robust but needs iteration.
What does R² tell me?
The share of variance in Y that the line accounts for. R² = 0.8 means 80% explained and 20% left in the residuals.
Is a high R² a good model?
Not necessarily. Anscombe's quartet is four datasets with identical regressions and completely different shapes — always plot the residuals.
How much does one outlier matter?
Enormously. A single distant point can dominate the fit, because its squared residual dwarfs everything else.
Where these figures come from
- NIST Digital Library of Mathematical Functions — reference definitions for elementary and special functions
- Wolfram MathWorld — definitions and formulas for every topic on this page
- NIST/SEMATECH e-Handbook of Statistical Methods — the statistical formulas (mean, variance, z, confidence intervals, sample size)
- National curriculum in England — Mathematics — the terms and methods taught in UK schools
Last checked: September 2026. Formulas are fixed by mathematics and do not change with tax years or regulations.