Linear Regression Calculator
Fit a least-squares line to paired data — slope, intercept, correlation coefficient r, R², and a prediction for any x.
How to use this calculator
- 1Paste your x values and your y values, separated by commas, spaces or new lines — they must pair up, so both lists need the same count.
- 2Read the fitted equation and R² to judge how well a straight line describes the data.
- 3Optionally set a value of x to get a prediction from the fitted line.
How the calculation works
b = Σ((xᵢ − x̄)(yᵢ − ȳ)) / Σ((xᵢ − x̄)²) a = ȳ − b·x̄ r = Σ((xᵢ − x̄)(yᵢ − ȳ)) / √(Σ(xᵢ − x̄)² · Σ(yᵢ − ȳ)²)- b
- Slope — how much y changes per one-unit increase in x
- a
- Intercept — the fitted value of y when x is zero
- x̄, ȳ
- The means of the x and y values
- r
- Pearson correlation coefficient, between −1 and +1
Least squares chooses the one line that minimises the sum of the squared vertical distances between each point and the line. Squaring is what makes the solution unique and gives a closed form; it also means outliers pull the line disproportionately, since a point twice as far off contributes four times the penalty.
This calculator uses the mean-deviation form above rather than the algebraically equivalent (nΣxy − ΣxΣy) shortcut. They give the same answer in exact arithmetic, but the shortcut subtracts two large and nearly equal numbers, which loses significant digits badly when the data has a large mean relative to its spread — year-indexed data being the classic case.
R² is simply r² for a simple linear regression with one predictor, which is why the two are reported together here.
Worked example
Eight points with a clear upward trend
- 1.Means: x̄ = 36 ÷ 8 = 4.5, ȳ = 44 ÷ 8 = 5.5.
- 2.The x deviations are −3.5, −2.5, −1.5, −0.5, 0.5, 1.5, 2.5, 3.5 and the y deviations are −3.5, −1.5, −0.5, −1.5, −0.5, 1.5, 2.5, 3.5.
- 3.Multiplying pairwise and summing: Σ((xᵢ − x̄)(yᵢ − ȳ)) = 38. Squaring and summing the x deviations: Σ((xᵢ − x̄)²) = 42.
- 4.Slope: b = 38 ÷ 42 = 0.904762.
- 5.Intercept: a = 5.5 − 0.904762 × 4.5 = 1.428571.
- 6.The y deviations squared also sum to 38, so r = 38 ÷ √(42 × 38) = 0.951190, and R² = 0.904762.
- 7.Predicting at x = 10: y = 1.428571 + 0.904762 × 10 = 10.476190.
Result: y = 0.9048x + 1.4286, r = 0.9512, R² = 0.9048
What a regression line actually is
Given a scatter of paired measurements, infinitely many straight lines could be drawn through them. Least-squares regression picks one specific line: the one that makes the total of the squared vertical gaps between each point and the line as small as possible. Those gaps are the residuals — what the model got wrong for each observation — and squaring them before adding means that being wrong by 4 in one place is penalised as much as being wrong by 2 in four places.
That choice is not arbitrary. Squaring makes the problem solvable in closed form (there is exactly one answer, given directly by the formulas above, with no searching required) and, under some standard assumptions about the errors, produces the estimate with the smallest variance among all unbiased linear estimators.
Reading r and R² correctly
The two headline numbers describe different things and are easy to conflate.
- r, the correlation coefficient — runs from −1 to +1 and captures both direction and strength. +1 is a perfect upward line, −1 a perfect downward one, 0 no linear relationship at all. Its sign always matches the slope's.
- R², the coefficient of determination — runs from 0 to 1 and is the share of the variation in y that the line accounts for. R² = 0.86 means about 86% of how y varies is tracked by x, leaving 14% unexplained by this model.
- Residual standard error — the typical size of a residual, in the units of y. Unlike r and R², which are unitless, this tells you how far off a prediction is likely to be in practical terms.
The limits worth knowing before trusting a fit
A high R² is not by itself evidence that a linear model is appropriate. Anscombe's quartet — four small datasets constructed in 1973 with nearly identical means, variances, regression lines and R² values but wildly different shapes when plotted — exists precisely to make this point: one is genuinely linear, one is a clean parabola, one is a perfect line dragged off by a single outlier, and one is a vertical stack plus one distant point. Summary statistics alone cannot distinguish them; plotting the data can.
Extrapolation is the other common trap. A line fitted across one range of x says nothing reliable about values far outside that range, where the underlying relationship may bend, saturate or reverse entirely. The prediction in this calculator is only as trustworthy as the assumption that the same linear pattern continues to the x you asked about.
Correlation and causation
A strong correlation establishes that two variables move together. It does not establish that one produces the other. Three explanations are always compatible with the same r: x causes y, y causes x, or some third variable drives both. Ice cream sales and drowning deaths correlate strongly across the year, but neither causes the other — warm weather drives both independently.
Distinguishing the possibilities requires something regression cannot supply on its own: a controlled experiment, a natural experiment, or subject-matter reasoning about plausible mechanisms. Regression measures association and quantifies it precisely; the causal interpretation has to come from outside the arithmetic.
What this assumes, and where it stops
Assumptions
- The relationship being modelled is linear. If the true pattern curves, a straight line will fit poorly even when r looks respectable.
- x is treated as the independent variable and y as the dependent one. Swapping them produces a different line (though the same r), so the ordering matters.
- All data points are weighted equally, and each pair is treated as an independent observation.
Limitations
- Least squares is sensitive to outliers — a single distant point can move the slope substantially, because its residual is squared.
- Only simple linear regression with one predictor is supported. Multiple predictors, polynomial terms and weighted fits are out of scope here.
- No significance test, confidence interval or p-value for the slope is produced. A non-zero slope from a small sample is not by itself evidence of a real relationship.
- Predictions outside the range of the supplied x values are extrapolation and can be badly wrong, however good the fit looks within the data.
Common questions
What is the difference between r and R²?
r shows direction and strength on a −1 to +1 scale, so its sign tells you whether the relationship is upward or downward. R² is just r squared for a simple regression, always between 0 and 1, and is interpreted as the proportion of the variation in y explained by x. An r of −0.9 and an r of +0.9 give the same R² of 0.81 — equally strong relationships pointing in opposite directions.
Does a high R² mean a straight line is the right model?
Not necessarily. Anscombe's quartet is the standard counterexample: four datasets with almost identical regression lines and R² values, only one of which is genuinely linear — the others include a clean curve and a line driven entirely by one outlier. Always plot the data as well as reading the summary statistics.
Can I swap x and y?
You can, but you will get a different line. Least squares minimises vertical distances specifically, so regressing y on x and x on y produce different slopes unless the fit is perfect. The correlation r is unchanged, since it treats both variables symmetrically. Put the variable you want to predict in y.
Formula and content last reviewed on .
Results are estimates for information only, not professional advice.
Related calculators
Tools people commonly use alongside the linear regression calculator.