Linear Regression Calculator

Fit the least-squares regression line y = mx + b to paired data. Get the slope, intercept, correlation r, R², a scatter plot with the fitted line and predictions for any X.

Linear regression explained, step by step

Each concept has a plain-English definition, the exact formula this calculator uses, a small diagram and a worked example — all in one card so the maths lines up with the visual.

1. The best-fit line y = m·x + b

Linear regression finds a single straight line through your scatter. Once you have the slope m and intercept b, you can describe how Y changes with X and predict Y for any x — even values you never observed.

ŷ = m·x + b
mslope — change in Y per +1 in X·bintercept — predicted Y when x = 0
Example
Given
study hours vs exam score
Substitute
m = 5.8
b = 46.6
Answer
ŷ = 5.8·x + 46.6

2. The least-squares idea

For every point measure the vertical gap between the observed y and the line — the residual (y − ŷ). Square each gap (so signs don't cancel and big misses are punished). The least-squares line is the unique line that minimises Σ(y − ŷ)².

minimise SSE = Σ(yᵢ − ŷᵢ)²
Example
Given
residuals = −2, +3, +1, −4
Substitute
SSE = 4 + 9 + 1 + 16
= 30
Answer
line that makes SSE smallest is the fit

3. Slope and intercept formulas

Solving the least-squares problem in closed form gives compact formulas in terms of Σx, Σy, Σxy and Σx². The intercept is chosen so the fitted line passes through the point of means (x̄, ȳ).

m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)  ·  b
= ȳ − m·x̄
nnumber of paired points·Σsum over all points
n = 5 Σx = 15 Σy = 320
Σxy = 1018 Σx² = 55
m = (5·1018 − 15·320) / (5·55 − 15²)
= 290 / 50 = 5.8
b = (320 − 5.8·15) / 5 = 46.6
Example
Given
n = 5
Σx = 15
Σy = 320
Σxy = 1018
Σx² = 55
Substitute
m = 290/50 · b
= (320 − 87)/5
Answer
m = 5.8
b = 46.6

4. What R² tells you

R² is the share of Y's total variance the line explains, from 0 to 1. R² = 0.82 means the line accounts for 82% of the scatter; the remaining 18% is noise. Higher isn't always better — R² = 1.00 on three points is a straight-line-through-three-points, not a great model.

= 1 − SS_res / SS_tot  ·  r
= sign(m)·√R²
Example
Given
SS_tot = 400
SS_res = 72
Substitute
R² = 1 − 72/400
Answer
R² = 0.82 (82% of variance explained)

5. Predicting Y at a new X

Once you have m and b, plugging in any x gives the fitted value ŷ. Predictions inside the range of your observed X are usually reliable; predictions far outside that range (extrapolation) are guesses — the relationship may not hold there.

ŷ = m·x + b
Example
Given
ŷ = 5.8·x + 46.6
predict at x = 6
Substitute
5.8·6 + 46.6
Answer
ŷ = 81.4

Features of this calculator

  • Least-squares slope m and intercept b of the best-fit line y = mx + b
  • Correlation coefficient r and coefficient of determination R²
  • Optional prediction: enter any X and get the fitted ŷ on the line
  • Scatter plot with the regression line, thin dashed residuals, and the prediction point marked
  • Step-by-step working — Σx, Σy, Σxy, Σx², slope substitution, intercept, R²
  • Handles negative numbers, decimals, and messy pasted data (currency, thousand separators)
  • Copy the result summary or download the whole panel — plot and steps — as an image

Frequently asked questions

+What's the difference between correlation and regression?

Correlation summarises how tightly X and Y move together with one symmetric number (r). Regression fits a directional line to predict Y from X — swap the axes and you get a different slope.

+What does a negative slope mean?

Y decreases as X increases. Each one-unit rise in X lowers the predicted Y by |m| units and the line tilts downward.

+Can R² be negative?

For the standard least-squares line on your training data, no — R² is between 0 and 1. Negative R² only appears when you apply a model to a different dataset or force a line through the origin.

+How many data points do I need?

Two points are enough to draw a line, but the fit is meaningless. Aim for at least 8–10 points, more if the data is noisy.

+Should the intercept always make physical sense?

Not always. If X = 0 lies outside the range you sampled, the intercept is a mathematical anchor for the line, not a real-world prediction.

Related calculators