Linear Regression Calculator, Line of Best Fit
Paste two columns of numbers and get the ordinary-least-squares best-fit line ŷ = a + bx, slope, intercept, r, R², standard error, a slope t-test with p-value and 95% confidence interval, a residuals table, and a scatter plot. Free, matched to scipy, and it runs entirely in your browser.
How it works
The calculator fits a straight line to your paired data using ordinary least squares (OLS), the method that chooses the slope and intercept minimising the sum of squared vertical distances (residuals) between the points and the line. The formulas below are the canonical simple-regression definitions from the NIST/SEMATECH e-Handbook of Statistical Methods §4.1.4.1.
For n pairs (xᵢ, yᵢ), first take the means x̄ and ȳ, then the sums of squares and cross-products of the deviations:
- Sxx = Σ(xᵢ − x̄)²
- Syy = Σ(yᵢ − ȳ)²
- Sxy = Σ(xᵢ − x̄)(yᵢ − ȳ)
The slope is b = Sxy / Sxx and the intercept is a = ȳ − b·x̄, so the fitted line is ŷ = a + bx and always passes through (x̄, ȳ). The strength of the fit comes from the correlation r = Sxy / √(Sxx·Syy) and R² = r², which is the proportion of the variance in y explained by the line (exact for one-predictor OLS).
To judge whether the slope is real rather than sampling noise, the tool computes the error sum of squares SSE = Syy − b·Sxy, the standard error of the estimate s = √(SSE / (n − 2)), and the standard error of the slope SE(b) = s / √Sxx. The slope test statistic is t = b / SE(b) on df = n − 2 degrees of freedom; its two-tailed p-value uses the regularized incomplete beta identity p = I_x(df/2, ½) with x = df/(df + t²), the same identity behind scipy.stats.linregress. The 95% confidence interval for the slope is b ± t*₀.₉₇₅,df · SE(b), where the critical value comes from the same Student-t distribution.
Every result is cross-checked two ways: the deviation-score slope above is reconciled against the independent raw-score formula b = [n·Σxy − Σx·Σy] / [n·Σx² − (Σx)²], and the residuals are shown summing to zero, a property every correct least-squares fit must satisfy. All of this runs client-side; nothing is uploaded.
Worked examples
Frequently asked questions
Sources & references
- NIST/SEMATECH e-Handbook §4.1.4.1: Linear Least Squares Regression
- NIST/SEMATECH e-Handbook §1.3.5.13: Measures of correlation and the standard error of the estimate
- SciPy: scipy.stats.linregress (reference implementation for slope, intercept, r, stderr, p-value)
- NIST/SEMATECH e-Handbook §1.3.6.7.2: Critical values of the Student-t distribution
These are stable mathematical definitions, not rates that change. The formulas and worked examples on this page were last cross-checked against the NIST e-Handbook and scipy.stats.linregress on 2026-07-10.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us, our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] , most fixes ship within 24 hours.