Least Squares Regression Calculator

Paste x,y pairs like 1,2 3,5 4,7 — separated by spaces, semicolons, or new lines — and get the line of best fit ŷ = mx + b, the correlation coefficient r, and r² instantly.

Put this calculator on your website — free

Copy one snippet and give your visitors a working Least Squares Regression Calculator.

How least squares regression works

Scatter your points on a graph and imagine sliding a ruler around until it looks like the best straight line through them. “Looks best” needs a definition, and least squares picks a specific one: for each point, measure the vertical gap between the point and the line (how far off the line’s prediction of y is), square it, and add all the squares up. The least squares line is the unique line that makes this total as small as possible. Why vertical gaps? Because the line is a prediction machine — you feed it x, it predicts y — so the error that matters is the error in y. Why squared? Two honest reasons: squaring stops positive and negative misses from canceling (a line threading between wildly scattered points shouldn’t score as perfect), and it punishes one big miss more than several small ones, which drags the line toward not-being-terribly-wrong anywhere. A bonus reason mathematicians love: squares make the minimization solvable with one clean formula — no searching required.

Paste your pairs in any reasonable format — 1,2 3,5 4,7, one pair per line, or semicolon-separated — and you get the fitted line ŷ = mx + b, the correlation coefficient r, and r².

The formula

m = Σ(x − x̄)(y − ȳ) ⁄ Σ(x − x̄)²     b = ȳ − m·x̄

Here x̄ and ȳ are the means of the x and y values, the sums run over all n points, m is the slope, and b is the y-intercept. The numerator measures how x and y move together (their co-variation); the denominator measures how much x varies on its own. The intercept formula encodes a lovely fact: the least squares line always passes exactly through the point of means (x̄, ȳ), the center of gravity of your data. The correlation is r = Σ(x − x̄)(y − ȳ) ⁄ √(Σ(x − x̄)² · Σ(y − ȳ)²), always between −1 and 1.

Worked example

Points: (1, 2), (2, 3), (3, 5), (4, 6). The means are x̄ = 2.5 and ȳ = 4. The deviations from the means give Σ(x − x̄)(y − ȳ) = (−1.5)(−2) + (−0.5)(−1) + (0.5)(1) + (1.5)(2) = 3 + 0.5 + 0.5 + 3 = 7, and Σ(x − x̄)² = 2.25 + 0.25 + 0.25 + 2.25 = 5. So m = 7⁄5 = 1.4 and b = 4 − 1.4 × 2.5 = 0.5: the line is ŷ = 1.4x + 0.5. With Σ(y − ȳ)² = 4 + 1 + 1 + 4 = 10, the correlation is r = 7⁄√50 ≈ 0.98995 and r² ≈ 0.98. Paste 1,2 2,3 3,5 4,6 above and you’ll get exactly these numbers.

r versus r² (and the sentence every statistics teacher repeats)

The two numbers answer different questions. r tells you the direction and tightness of the linear relationship: +1 is a perfect uphill line, −1 a perfect downhill line, 0 no linear trend at all. tells you the share of the variation in y that the line accounts for: r = 0.9 sounds impressive until you notice r² = 0.81, meaning 19% of the variation in y has nothing to do with your line. Since squaring discards the sign, r² alone can’t tell you whether the trend goes up or down — report both. And the mandatory warning, which is mandatory because it is genuinely easy to forget: correlation is not causation — a strong r says x and y move together, not that x makes y move (ice cream sales and drowning deaths correlate beautifully; the cause of both is summer). One practical caution deserves equal billing: least squares is sensitive to outliers, because squaring amplifies big misses. A single typo’d point can swing the whole line, so eyeball your data before you trust the fit.

Frequently asked questions

What does the least squares regression line actually minimize?

The sum of the squared vertical distances between each data point and the line. Vertical, because the line predicts y from x, so prediction error is measured in y. Squared, so that misses above and below the line can't cancel out, and so that one large miss costs more than several small ones. Among all possible straight lines, exactly one minimizes this total — that's the regression line.

What is the difference between r and r²?

r (the correlation coefficient) runs from −1 to +1 and tells you the direction and tightness of the linear relationship. r² tells you the fraction of the variation in y that the line accounts for. An r of 0.9 means r² = 0.81 — the line explains 81% of the variation, and 19% is something else. Report both: r² alone loses the sign, so it can't tell you whether the trend is up or down.

How many data points do I need for linear regression?

Mathematically, two points with different x values define a line (a perfect, meaningless fit). Statistically, you want considerably more — with fewer than about 10 points, one outlier can swing the slope dramatically, and r becomes unstable. This calculator accepts any number of pairs from two up; just remember that a fit through three points deserves less trust than one through thirty.

Why does the calculator warn me when all my x values are the same?

Points that share one x value lie on a vertical line, and a vertical line has undefined slope — you can't write it as y = mx + b. The least squares slope formula divides by the spread of the x values, so identical x values mean dividing by zero. If your data really is vertical, regression of y on x is the wrong tool; swap the roles of x and y instead.

Does a strong correlation mean x causes y?

No — this is the most repeated warning in statistics because it's the most repeated mistake. A high r means x and y move together; it says nothing about why. Ice cream sales correlate strongly with drowning deaths, caused by neither: hot weather drives both. Establishing causation takes controlled experiments or careful causal inference, not a regression line, however beautiful its r² may be.

Related calculators