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.
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 − ŷ)².
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̄, ȳ).
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.
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.
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.