Math calculator

Linear Regression Calculator

The line of best fit, and what it is fitting.

The line of best fit

One point per line, as x and y.

5 points

y = 0.6x + 2.2

r² is 0.6, so 60.0% of the variation in y is accounted for by the straight-line relationship with x.

Slope

0.6

change in y per unit of x

Intercept

2.2

y where x is zero

Correlation r

0.77459667

−1 to 1, direction and strength

0.6

share of variation accounted for

Mean point

(3, 4)

the line always passes through it

Slope of x on y

1

a different line, not the reciprocal

Sum of squared residuals

2.4

what the fit minimises

Standard error

0.89442719

typical residual size

  • Least squares minimises the squared VERTICAL distances from the points to the line — not the perpendicular distances. That asymmetry is why the fit treats x as known and y as measured.
  • Because of that asymmetry, regressing x on y gives a different line, not the same one rearranged. The two agree only when the fit is perfect.
  • The line always passes through the mean point — the average x and the average y — whatever the data looks like.

A high r² says the points sit near a line. It says nothing about whether one variable causes the other.

What this tool shows

Least squares minimises the squared vertical distances, not the perpendicular ones. That asymmetry is why regressing x on y gives a different line — not the same line rearranged — and both are reported here.

  • The least-squares line of best fit
  • Correlation r and the coefficient r²
  • The regression of x on y as well
  • Residuals and the standard error
  • Why the line passes through the mean point
  • Where a straight line cannot be fitted
Slope and intercept r and r² Both regressions Residuals

One point per line, as x and y.

Updated 7 September 2026 · Works in any browser, no installation

The least-squares line minimises the squared vertical distances from the points to it. The slope is the covariance of x and y divided by the variance of x, and the intercept follows from the line having to pass through the mean point — which it always does, whatever the data looks like.

At a glance

Formula shown
The slope is Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², and the intercept is ȳ − slope × x̄. That second expression is why the line always passes through the mean point. r² is the square of the correlation coefficient.
Scenario support
Finding a trend in measurements; calibrating an instrument against known values; estimating a rate of change from noisy data.
Educational estimate
Planning support from the values you enter — not professional advice.

What least squares minimises

The line is chosen to make the total squared distance from the points to it as small as possible. Two details in that sentence do real work.

Squared, not absolute. Squaring makes the problem solvable in closed form — the answer is a formula rather than a search. It also means a point twice as far off contributes four times as much, so a single outlier can move the line a long way.

Vertical, not perpendicular. The distance measured is straight up or down to the line, not the shortest distance to it. That encodes an assumption: x is known and y is the thing measured with error.

When both variables carry error, that assumption is wrong and a different method — total least squares, which does use perpendicular distances — is the honest one. Most textbook problems quietly assume it does not matter.

Two regressions, not one

This follows directly, and it surprises people: regressing x on y is a different line.

Not the same line rearranged. Swapping the roles swaps which distances are being minimised, so the fit changes. The y-on-x slope is not the reciprocal of the x-on-y slope, and this page reports both so the gap is visible.

The two lines always cross at the mean point, and the angle between them narrows as the correlation strengthens. They coincide only when the fit is perfect — when every point is already on a line and there is nothing left to minimise.

Which one to use is decided by the question, not by the data. Predicting y from x wants y on x. Predicting x from y wants x on y. Using the wrong one is a real error and it biases the prediction towards the mean.

What r² does not say

r² is the share of the variation in y accounted for by the straight-line relationship with x. It runs from 0 to 1, and it is over-read constantly.

It does not say the relationship is linear. Curved data can produce a high r² over a short range. The residuals are what reveal that — if they bend, a straight line was the wrong model however good the number looks.

It does not say anything about causation. Two series that both grow over time correlate beautifully and may have nothing to do with one another.

It is inflated by few points. Two points always give r² = 1, because a line through both has no residuals at all. That is not a good fit; it is no information.

Anscombe’s quartet is the standard demonstration: four datasets with the same slope, intercept and r², one of which is a clean line and one of which is a curve. Plotting them is the only thing that tells them apart.

When it cannot be done

Three cases where this page refuses rather than returning something.

Every point shares an x value. The best fit is a vertical line, and a vertical line has no slope. It is not that the arithmetic is hard — the vertical distances being minimised are infinite, so the problem as posed has no answer.

Fewer than two points. One point has infinitely many lines through it.

Every point identical. There is no variation to explain, so there is nothing to fit.

A near-vertical set is worth watching too. The arithmetic succeeds, but the slope is enormous and extremely sensitive to any one point — the answer is technically right and practically meaningless.

Where it gets used

Calibration. Measure known standards, fit a line, and use it to convert future readings. The intercept is the offset and the slope is the scale.

Trend estimation. Sales against month, temperature against year. The slope is the rate of change, and the standard error says how confident that rate is.

The basis of nearly everything else. Multiple regression, logistic regression and most of machine learning start from this idea and generalise it. Least squares is also a projection — the fitted values are the projection of the data onto the space of straight lines, which is why the residuals come out perpendicular to it.

Sources and methodology

Least squares and its interpretation are standard statistics; these are the references.

Method. The slope is computed from centred sums rather than from raw sums of squares, which is the numerically stable arrangement — the raw form subtracts two large nearly-equal quantities and loses precision on data far from the origin. A point set sharing a single x value is refused with the reason rather than returning an infinity: least squares minimises vertical distances, and for a vertical line those are unbounded. The suite re-derives every fit from the raw-sum formula and checks the two agree. That engine is verified on every change against 119 hand-written assertions, including that the fitted line passes through the mean point on every one of five hundred generated datasets, and that the x-on-y slope differs from the reciprocal of the y-on-x slope whenever the fit is imperfect. The count and the per-case breakdown are published on the formula verification page.

Related calculators

Where this goes next:

Standard DeviationSample and population standard deviation, plus variance, mean, median, quartiles, z-scores, outliers, and confidence intervals.
SlopeSlope as an exact fraction, with the vertical case reported as undefined rather than as a division by zero — and the angle, gradient percentage and both related slopes alongside it.
Line EquationThe equation of a line in all three standard forms from two points or from a point and a slope, with the intercepts and the vertical case that has no y = mx + c form at all.
Percentage ChangeWork out percentage increase or decrease, reverse change, loss recovery, percentage points, multi-period change, and CAGR.
Average PercentageAveraging 50% and 100% gives 99.02%, not 75%, when the groups are 2 and 100 — the page weights by group size and shows the naive answer beside it.
Percent ErrorPercent error against an accepted value, rounded to your significant figures, for one measurement, repeated trials, or a whole column of pairs.

More in Math, or browse all calculators.

Read the guide

r² is expressed as a share of the total variation in y, which is what the Standard Deviation Calculator measures directly.

Educational use disclaimer

This is an educational tool, not a statistics package. It fits a straight line and reports the fit; it does not test whether a straight line was the right model.

How we calculate · Found an error? email us

Authorship & verification

Written and maintained by , a business operator who builds spreadsheet-based calculators.

What's changed (3 updates)

Published 7 September 2026

  1. Published the linear regression page reporting the regression of x on y alongside the regression of y on x, since least squares minimises vertical distances and swapping the roles changes what is being minimised — the two are different lines, not one rearranged.
  2. Refuses a point set that shares a single x value with the reason rather than returning an infinity: the best fit there is vertical, and the vertical distances least squares minimises are unbounded.
  3. Computed from centred sums rather than raw sums of squares, which is the numerically stable arrangement for data far from the origin.

Add this calculator to your site

Responsive embed — and private: nothing your visitors type leaves their browser.