Mathematics & Statistics

Linear Regression Calculator

Least-squares best-fit line y = a + bx: slope, intercept, r, R², standard errors and prediction.

Inputs

Five data points rising from left to right with a straight best-fit line through them; one vertical residual from a point to the line is marked.xyy = a + bxresidual
Least squares chooses the line y = a + bx that makes the sum of the squared vertical distances (residuals) from the points to the line as small as possible. Diagram not to scale.

Separate the numbers with commas, spaces or semicolons. Use a dot for decimals.

Optional. Predicting far outside the range of the x data is extrapolation and may be unreliable.

Result

Best-fit line
y = 0.05 + 1.99 x
Slope b
1.99
Intercept a
0.05
Correlation coefficient r
0.99865176
Coefficient of determination R²
0.99730533
Predicted y at x
11.99
Standard error of the estimate s
0.18885621
Standard error of the slope
0.059721576
Standard error of the intercept
0.19807406
Number of points n
5points
Working
x̄ = 3, ȳ = 6.02 Sxx = Σ(x − x̄)² = 10, Sxy = Σ(x − x̄)(y − ȳ) = 19.9, Syy = 39.708 b = Sxy ÷ Sxx = 1.99 a = ȳ − b x̄ = 0.05

The least-squares line is y = 0.05 + 1.99 x, with R² = 0.99730533 (r = 0.99865176). At x = 6 it predicts y = 11.99.

  • x = 6 is outside the x data (1 to 5): the prediction is an extrapolation.

Educational estimate — check the assumptions below.

About this calculator

Linear regression fits the straight line y = a + bx that makes the sum of squared vertical distances from the points to the line as small as possible (least squares). Engineers use it for calibration curves, wear and drift trends, and any roughly straight-line relationship. The correlation coefficient r (−1 to 1) and R² (0 to 1) say how closely the points follow the line.

Formula

b = Sxy ÷ Sxx    a = ȳ − b x̄

Sxx = Σ(x − x̄)²   Sxy = Σ(x − x̄)(y − ȳ)   Syy = Σ(y − ȳ)²

r = Sxy ÷ √(Sxx Syy)    s = √(Σ(y − ŷ)² ÷ (n − 2))

SE(b) = s ÷ √Sxx    SE(a) = s √(1/n + x̄² ÷ Sxx)

Sums are taken about the means, which is more accurate than the textbook shortcut formulas with Σx² and Σxy.

Worked example

x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1:

  • x̄ = 3, ȳ = 6.02; Sxx = 10, Sxy = 19.9
  • b = 19.9 ÷ 10 = 1.99; a = 6.02 − 1.99 × 3 = 0.05 → y = 0.05 + 1.99x
  • Syy = 39.708 → r = 19.9 ÷ √(10 × 39.708) = 0.998652, R² = 0.997305
  • At x = 6: ŷ = 0.05 + 1.99 × 6 = 11.99 (an extrapolation)

Assumptions and limitations

  • Ordinary least squares: the x values are taken as exact and all the scatter is in y.
  • A high R² shows a good straight-line fit, not cause and effect. Look at the data: a curved relationship can still give a high R².
  • Standard errors assume independent, normally distributed residuals with constant variance.

References

  • NIST/SEMATECH e-Handbook of Statistical Methods, section 4.1.4.1 (linear least squares regression).
  • Montgomery, D. C. & Runger, G. C. Applied Statistics and Probability for Engineers — simple linear regression.