Statistics

Linear Regression Calculator

Linear regression finds the straight line that best fits a set of data points. This calculator computes the least-squares slope and intercept, plus the correlation coefficient r and r², from your paired x and y values.

Linear Regression Calculator

Least-squares line and correlation for paired data.

Try:
Answery = 0.8x + 1.8 (r = 0.852803)
  1. Meansx̄ = 3, ȳ = 4.2
  2. Sums of squaresSxx = 10, Sxy = 8
  3. Slopeb = Sxy / Sxx = 0.8
  4. Intercepta = ȳ − b·x̄ = 1.8
  5. Correlationr = 0.852803, r² = 0.727273
  6. Regression liney = 0.8x + 1.8

A line, and a verdict on the line

Given a scatter of points, infinitely many straight lines could be drawn and none will pass through them all. Least squares picks one by a precise criterion: the line whose total squared vertical gap to the points is as small as possible.

This calculator computes that line from paired x and y values, and reports the correlation alongside it — because a line can always be fitted, and the correlation is what says whether fitting one was reasonable.

The slope and intercept say what the relationship is; the correlation says how well a straight line describes it at all. Quoting the first without the second is how regression output most often misleads, since the arithmetic succeeds as readily on data with no trend as on data with a strong one.

The intermediate sums are reported too. The slope is a ratio of two of them, so a surprising slope can be traced to whichever sum is behaving unexpectedly rather than recomputed from scratch.

How to use this calculator

  1. Enter the x values Separated by commas, spaces or semicolons. These are the explanatory values, treated as known when predicting.
  2. Enter the y values in the same order Pairing is by position, so the third y belongs to the third x. The two lists must contain the same number of values.
  3. Check the means before anything else They confirm both lists parsed as intended. The fitted line always passes through the point formed by the two means, which is a useful anchor when sketching it.
  4. Read the correlation with the line Both r and its square are given. They decide whether the slope is worth quoting, so read them before using the equation.

The formula, and where it comes from

b = Sxy / Sxx a = ȳ − b·x̄ r = Sxy / √(Sxx·Syy) y = a + bx

The two sums driving everything are built from deviations, not raw values. Sxx adds the squared distances of each x from its mean; Sxy adds the products of the two deviations for each pair — positive when both fall the same side of their means, negative when opposite.

The slope is their ratio. Because Sxx cannot be negative, the sign of the slope is the sign of Sxy, which is exactly the statement that the line rises when large x values tend to accompany large y values.

The intercept is then forced rather than fitted independently: it is whatever value makes the line pass through the point of means. That is a consequence of the least-squares criterion, not a convention, and it is why only one of the two quantities has to be minimised for.

The correlation divides the same Sxy by the geometric mean of the two spreads, which normalises it onto the range from −1 to 1. It shares its numerator with the slope but is scale-free, so multiplying every y by ten changes the slope tenfold and leaves the correlation untouched.

What each input means

x Explanatory values — form field “x values (comma-separated)”
The list treated as known. All values being identical leaves Sxx at zero and no line can be fitted, which the solver reports explicitly.
y Response values — form field “y values (comma-separated)”
The list being predicted. It must have the same length as the x list, since pairing is positional.
b, a Slope and intercept
The change in y per unit of x, and the fitted value of y at x = 0 — which may lie far outside the data.
r, r² Correlation and its square
How closely the points cluster around the line, from −1 to 1, and the proportion of variation in y that the line accounts for.

Worked examples

Every number below is produced by the same calculation engine the tool above runs. Nothing here is typed by hand, so the walkthrough cannot drift from what you get when you enter the same values yourself.

Five points with a loose upward trend

The x values 1 to 5 against y values that rise overall but not monotonically, dipping at the fourth point.

Inputs x values (comma-separated) = 1, 2, 3, 4, 5, y values (comma-separated) = 2, 4, 5, 4, 6

  1. Means x̄ = 3, ȳ = 4.2
  2. Sums of squares Sxx = 10, Sxy = 8
  3. Slope b = Sxy / Sxx = 0.8
  4. Intercept a = ȳ − b·x̄ = 1.8
  5. Correlation r = 0.852803, r² = 0.727273
  6. Regression line y = 0.8x + 1.8

Result y = 0.8x + 1.8 (r = 0.852803)

The slope is positive despite one pair moving the wrong way. Least squares weighs every point, so a single reversal does not overturn a trend the rest of the data supports — it only reduces the correlation.

The correlation is high but well short of 1, which is the honest description of this data: a clear upward tendency with visible scatter around it. Reporting the slope without that qualification would overstate what five points establish.

A strong linear relationship

Four points where y rises by roughly ten for every ten in x, with only slight departures from a straight line.

Inputs x values (comma-separated) = 10, 20, 30, 40, y values (comma-separated) = 15, 25, 36, 44

  1. Means x̄ = 25, ȳ = 30
  2. Sums of squares Sxx = 500, Sxy = 490
  3. Slope b = Sxy / Sxx = 0.98
  4. Intercept a = ȳ − b·x̄ = 5.5
  5. Correlation r = 0.998131, r² = 0.996266
  6. Regression line y = 0.98x + 5.5

Result y = 0.98x + 5.5 (r = 0.998131)

The correlation is very close to 1 and the squared value close to 1 as well, meaning almost all the variation in y is accounted for by x. This is what a genuinely linear relationship looks like in the numbers.

The intercept is a fitted constant at x = 0, which lies well outside the observed range of 10 to 40. It anchors the line rather than describing anything observed, and reading it as a prediction would be extrapolation.

Reading the result

What the slope means

The average change in y associated with a one-unit increase in x, across the range observed. It carries the units of y divided by the units of x, and quoting it without them invites misreading.

Correlation is not causation, and r² is not accuracy

A high correlation says the points lie close to a line, nothing about why. And r² is the share of variance the line accounts for within this data, not a measure of how well it will predict new observations.

A line fits anything

The arithmetic succeeds on data with no relationship, on a curve, and on a cloud of noise. Only the correlation, and ideally a look at the scatter, distinguishes a meaningful fit from a formal one.

When you would use this

Quantifying a trend

Where two measured quantities appear related, the slope puts a number on it and the correlation says how firmly the data supports it.

Interpolating within the observed range

The fitted equation estimates y for an x between those measured. Within the range, and with a high correlation, that is the model's most defensible use.

Assumptions and limitations

What this calculator assumes

  • The two lists have equal length and are paired by position, with at least two points.
  • Only vertical distances are minimised, so the roles of the two variables are not interchangeable.
  • Every entry parses as a finite number; a malformed list is rejected rather than partially read.
  • The x values are not all identical — that case leaves no spread to fit against and is reported as an error.

Where it stops being the right tool

  • A straight line only: no polynomial, exponential or logarithmic fitting.
  • Two variables, so multiple regression on several explanatory columns is out of scope.
  • No confidence intervals, standard errors or significance test for the slope.

Common mistakes

Swapping the two lists

Why it happens. Both are just columns of numbers and the calculation runs either way. Regressing x on y minimises horizontal distances instead, and gives a different line — not the reciprocal of the first.

How to avoid it. Put the quantity you would predict from into the x field. Only the correlation is unchanged by the swap, which is a way to confirm the lists were entered as intended.

Extrapolating beyond the data

Why it happens. The equation accepts any x, so nothing prevents substituting a value far outside the observed range. The linearity was only ever established where there were points.

How to avoid it. Keep predictions within the range of the x values, and treat the intercept as a fitted constant unless zero genuinely falls inside that range.

Quoting a slope with a weak correlation

Why it happens. The slope is the headline number and it is reported with full precision regardless of how well the line fits, which makes it look equally authoritative in both cases.

How to avoid it. Read the correlation first. A value near zero means the slope is describing noise, however many decimal places it carries.

Key terms

Frequently asked questions

What is the least-squares line?

The straight line minimising the total of the squared vertical distances from the data points to the line. Squaring stops positive and negative gaps cancelling, and it penalises large misses more heavily than small ones.

What does the correlation coefficient mean?

It runs from −1 to 1 and measures how closely the points cluster around a straight line. Values near either extreme indicate a strong linear relationship, values near zero a weak one. Its sign always matches the sign of the slope.

What is r²?

The square of the correlation, read as the proportion of the variation in y that the fitted line accounts for. An r² of 0.9 means the line explains ninety per cent of that variation, leaving a tenth to everything else.

Why does the line always pass through the means?

Because the intercept is computed as the value that forces it to. That is a property of the least-squares solution rather than an added constraint, and it makes the point of means a reliable anchor when sketching the line by hand.