Skip to content
induwara.lk
Premium
induwara.lkStatistics · Regression

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.

By Induwara AshinsanaUpdated Jul 10, 2026
Linear regression calculator

Numbers separated by commas, spaces, or new lines. Paste two Excel columns here to fill both.

Must have the same count as X — each X needs a matching Y.

Examples
Decimals
Regression equation
ŷ = 2.2000 + 0.6000x
Slope (b)
0.6000
Change in y per +1 in x
Intercept (a)
2.2000
ŷ where x = 0
R² (determination)
0.6000
60.0% of variance explained
Correlation r
0.7746
Moderate fit

Scatter plot

with fitted line ŷ = 2.200 + 0.600x
XY0.765.241.825.18(1.0000, 2.0000) · ŷ=2.8000 · residual -0.8000(2.0000, 4.0000) · ŷ=3.4000 · residual 0.6000(3.0000, 5.0000) · ŷ=4.0000 · residual 1.0000(4.0000, 4.0000) · ŷ=4.6000 · residual -0.6000(5.0000, 5.0000) · ŷ=5.2000 · residual -0.2000

Solid line is the fit; dashed amber drops are the residuals (yᵢ − ŷᵢ) that least squares minimises.

Slope significance & interval

t = b / SE(b)
2.1213
Degrees of freedom
3
p-value (two-tailed)
0.1240
At α = 0.05Not significant
Std error of estimate (s)
0.8944
95% CI for slope
[-0.3001, 1.5001]

Cross-check. The deviation-score formula gives slope b = 0.6000; the independent raw-score formula [n·Σxy − Σx·Σy] / [n·Σx² − (Σx)²] gives 0.6000. They reconcile, as they must — and both match scipy.stats.linregress.

Residuals & working

#xᵢyᵢŷᵢ = a + b·xᵢresidual eᵢ
11.00002.00002.8000-0.8000
22.00004.00003.40000.6000
33.00005.00004.00001.0000
44.00004.00004.6000-0.6000
55.00005.00005.2000-0.2000
Σ residuals (self-check ≈ 0)-0.0000
x̄ = 15.0000 / 5 = 3.0000 · ȳ = 20.0000 / 5 = 4.0000
Sxx = 10.0000 · Sxy = 6.0000 · Syy = 6.0000
b = Sxy / Sxx = 6.0000 / 10.0000 = 0.6000
a = ȳ − b·x̄ = 4.00000.6000·3.0000 = 2.2000
SSE = 2.4000 · SE(b) = 0.2828 · SE(a) = 0.9381

Method: ordinary least squares, b = Σ(xᵢ−x̄)(yᵢ−ȳ)/Σ(xᵢ−x̄)² and a = ȳ − b·x̄; slope test t = b/SE(b) with df = n−2 — NIST e-Handbook §4.1.4.1, matched to scipy.stats.linregress. Nothing leaves this page.

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

Classic five points

ŷ = 2.2 + 0.6x

  1. X = [1, 2, 3, 4, 5], Y = [2, 4, 5, 4, 5]; n = 5
  2. x̄ = 3, ȳ = 4 · Sxy = 6, Sxx = 10, Syy = 6
  3. b = Sxy/Sxx = 6/10 = 0.6
  4. a = ȳ − b·x̄ = 4 − 0.6·3 = 2.2
  5. r = 6/√60 = 0.7746 → R² = 0.6000 (60% explained)
  6. SSE = 6 − 0.6·6 = 2.4 → s = √(2.4/3) = 0.8944
  7. SE(b) = 0.8944/√10 = 0.2828 → t = 0.6/0.2828 = 2.121, df = 3
  8. p = 0.1240 → not significant at α = 0.05
  9. Predict x = 6: ŷ = 2.2 + 0.6·6 = 5.8

Study hours vs exam marks (significant fit)

ŷ = 45.8 + 3.2x

  1. X = [2, 4, 6, 8, 10] hours, Y = [52, 58, 67, 70, 78] marks; n = 5
  2. x̄ = 6, ȳ = 65 · Sxy = 128, Sxx = 40, Syy = 416
  3. b = 128/40 = 3.2 → each extra hour ≈ +3.2 marks
  4. a = 65 − 3.2·6 = 45.8
  5. r = 128/√16640 = 0.9923 → R² = 0.9846 (98.5% explained)
  6. SSE = 416 − 3.2·128 = 6.4 → s = √(6.4/3) = 1.4606
  7. SE(b) = 1.4606/√40 = 0.2309 → t = 3.2/0.2309 = 13.86, df = 3
  8. p ≈ 0.0008 → highly significant; 95% CI = [2.465, 3.935]
  9. Predict x = 7 hours: ŷ = 45.8 + 3.2·7 = 68.2 marks

Negative slope (downward trend)

ŷ = 1.6 − 1.7x

  1. X = [−2, −1, 0, 1, 2], Y = [5, 3, 2, 0, −2]; n = 5
  2. x̄ = 0, ȳ = 1.6 · Sxy = −17, Sxx = 10, Syy = 29.2
  3. b = −17/10 = −1.7 (y falls 1.7 per +1 in x)
  4. a = 1.6 − (−1.7)·0 = 1.6
  5. r = −17/√292 = −0.9948 → R² = 0.9897
  6. Residuals (0, −0.3, 0.4, 0.1, −0.2) sum to 0 ✓
  7. t = −1.7/0.1 = −17.0, df = 3 → p ≈ 0.0004, significant

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

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.