What Is Least Squares?
The least squares method chooses the straight line that makes the sum of squared vertical errors as small as possible. Setting the derivatives of that sum to zero gives two normal equations whose solution is the slope and the intercept.
Enter your x and y values to get the slope, intercept, SSE, R² and root mean squared error. A working table lists x·y, x², each fitted value and each residual, so you can follow the arithmetic.
Add an x value to predict y, but stay within the range of your data.
How to Use the Least Squares Calculator
- Enter the x values.
- Enter the y values.
- Optionally enter an x to predict at.
- Read the line, SSE and the working table.
Least Squares Formula
Slope
Intercept
Sum of squared errors
Least Squares Variables Explained
| Symbol | Meaning | Notes |
|---|---|---|
| b₁ | Slope | |
| b₀ | Intercept | |
| SSE | Sum of squared errors | |
| ŷ | Fitted value |
Least Squares Worked Example
For x = 1 to 6 and y = 2.1, 3.9, 6.2, 7.8, 10.1, 12.2: n = 6, Σx = 21, Σy = 42.3, Σxy = 183.4 and Σx² = 91. Then b₁ = (6 × 183.4 − 21 × 42.3) ÷ (6 × 91 − 441) = 212.1 ÷ 105 = 2.02, and b₀ = (42.3 − 2.02 × 21) ÷ 6 = −0.02. The line is y = −0.02 + 2.02x.
With the example values loaded in the calculator above, the steps are:
- n = 6, Σx = 21, Σy = 42.3, Σxy = 183.4, Σx² = 91.
- Least squares chooses the line that makes Σ(y − ŷ)² as small as possible. Setting the derivatives to zero gives the normal equations: Σy = n·b₀ + b₁·Σx and Σxy = b₀·Σx + b₁·Σx².
- Slope b₁ = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) = (1100.4 − 888.3) ÷ (546 − 441) = 2.02.
- Intercept b₀ = (Σy − b₁Σx) ÷ n = -0.02.
- Error: SSE = Σ(y − ŷ)² = 0.128. No other straight line gives a smaller value.
Result: Slope b₁ = 2.02.
How to Read the Least-Squares Line
For x = 1 to 6 and y = 2.1, 3.9, 6.2, 7.8, 10.1, 12.2 the line is y = −0.02 + 2.02x. Each extra unit of x adds about 2.02 to y, and the sum of squared errors is 0.128, which gives an R² of 0.998.
The residuals sum to zero, and the residual chart shows how far each point sits from the line. A residual that stands out marks a point the line fits poorly. A pattern in the residuals, such as a curve, says a straight line is the wrong model.
Where Least Squares Is Used
Line of best fit
Fit a straight line to measured data.
Showing the working
See each sum, the normal equations and the residuals.
Prediction
Predict y at a new x within the data range.
Residual check
Look for outliers and curved patterns.
Common Least Squares Mistakes to Avoid
- Plot the points first. A straight line only suits roughly linear data.
- The line passes through the point (x̄, ȳ).
- One outlier can pull the line. Check the residual chart.
- Do not extrapolate far past your x values.
Least Squares Terms and Definitions
- Residual
- The vertical gap between an observed y and the fitted ŷ.
Least Squares FAQs
What does least squares mean?
It means choosing the line whose squared vertical errors add up to the smallest possible total.
Why square the errors?
Squaring stops positive and negative errors cancelling and gives a smooth problem with a single exact answer.
What are the normal equations?
They are two equations, Σy = nb₀ + b₁Σx and Σxy = b₀Σx + b₁Σx², whose solution gives the slope and intercept.
Is least squares the same as linear regression?
Simple linear regression uses least squares to fit the line. Least squares is the method, and regression is the wider modelling idea.