Saved

Press the star next to a tool's name to save it.

All my tools

Linear Regression Calculator

This linear regression calculator finds the least squares line of best fit for your x, y data, with the slope, intercept, correlation r, R², standard error and predictions.

Restored your last values ·

Paste two columns from a spreadsheet, or type one pair per line. Commas, spaces or tabs between x and y all work.

Everything is worked out in your browser. Your data isn't sent anywhere.

Line of best fit

–

Enter at least two x, y pairs and press Calculate.

A linear regression calculator finds the straight line that best fits a set of points, so you can describe how one value changes with another and make a prediction. Paste your x and y values, one pair per line, and this calculator gives the equation of the line of best fit, the slope and intercept, the correlation coefficient r, R², and the predicted y for any x you choose.

What linear regression does

The National Institute of Standards and Technology (NIST) calls linear least squares regression by far the most widely used modeling method, and it's what most people mean when they say they used regression or least squares. The method of least squares, developed in the late 1700s and early 1800s by Gauss, Legendre and possibly Robert Adrain, picks the line that makes the sum of the squared distances between the data and the line as small as possible. The simple version here fits one explanatory variable, x, to one response, y.

The formulas

With n pairs, the mean of x written x̄ and the mean of y written ȳ:

  • Slope b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
  • Intercept a = ȳ − b × x̄
  • Line: y = a + b·x
  • Correlation r = Σ(x − x̄)(y − ȳ) ÷ √(Σ(x − x̄)² × Σ(y − ȳ)²)
  • R² = r², the share of the variation in y that the line explains
  • Standard error of the estimate = √(Σ(y − predicted y)² ÷ (n − 2))

How to use the linear regression calculator

  1. Paste or type your data, one x, y pair per line. You can copy two columns straight from Excel or Google Sheets.
  2. Optionally enter an x value to predict its y.
  3. Press Calculate to see the equation, the slope and intercept, r and R², the means and the standard error.

A worked example

Take five points: (1, 2), (2, 4), (3, 5), (4, 4) and (5, 5). The means are x̄ = 3 and ȳ = 4. The sum of (x − x̄)(y − ȳ) is 6 and the sum of (x − x̄)² is 10, so the slope is 6 ÷ 10 = 0.6. The intercept is 4 − 0.6 × 3 = 2.2, giving the line y = 2.2 + 0.6x. The sum of (y − ȳ)² is 6, so r = 6 ÷ √(10 × 6) = 0.7746 and R² = 0.6: the line explains 60% of the variation in y. At x = 6 the line predicts y = 2.2 + 0.6 × 6 = 5.8.

Reading the results

The slope is how much y changes, on average, when x goes up by one. A slope of 0.6 means each extra unit of x adds about 0.6 to y. The intercept is the predicted y when x is zero, which only makes practical sense if zero is within or near your data. The correlation r runs from −1 to 1: values near 1 mean a strong upward straight-line pattern, near −1 a strong downward one, and near 0 little straight-line relationship. R² runs from 0 to 1 and tells you the share of the ups and downs in y that the line accounts for.

Where it's used

  • Business: sales against advertising spend, or costs against units produced.
  • Science and engineering: calibration lines that turn an instrument reading into a measurement.
  • Schoolwork: fitting a trend line in statistics, physics or economics.
  • Personal data: weight against weeks of training, or a bill against the number of days in the month.

When a straight line is the wrong model

Least squares will always return a line, even when the points curve, cluster or contain an outlier, so look at a scatter plot before trusting the result. A single extreme point can pull the slope a long way. Correlation also doesn't prove that x causes y: both may follow a third factor. And predictions far outside the range of your x values assume the straight-line pattern continues, which it often doesn't. For a fuller summary of one column, the standard deviation calculator and the average calculator help, and the z-score calculator shows how unusual a single value is.

Spreadsheet check

You can check the results in Excel or Google Sheets with your x values in column A and your y values in column B. The SLOPE, INTERCEPT, CORREL and RSQ functions return the same slope, intercept, r and R² as this calculator, and FORECAST predicts a y value from an x. If a spreadsheet gives a different answer, the usual reason is a blank cell, a number stored as text, or the two columns swapped. Pasting the same two columns here is a quick way to confirm.

Interpolation and extrapolation

Predicting y for an x that lies between your smallest and largest x values is called interpolation, and it's usually reasonable when the points follow a straight line. Predicting beyond that range is extrapolation, and the further you go, the less reliable it gets, because nothing in the data shows the pattern continuing. In the example, a prediction at x = 6 is just past the data, while one at x = 60 would rest on an assumption the five points can't support.

Common mistakes

  • Swapping x and y. Regressing y on x gives a different line from x on y.
  • Reading R² as accuracy. A high R² can still hide a curved pattern.
  • Ignoring units. The slope is in units of y per unit of x.
  • Using too few points. Two points always fit perfectly, which tells you nothing.

What next

Use this often? Add Linear Regre to your Home screen.

Frequently asked questions

What does a linear regression calculator do?

It fits the straight line y = a + bx that makes the squared distances to your points as small as possible, and reports the slope, intercept, r and R².

How do I enter data?

One x, y pair per line. You can paste two columns from a spreadsheet; commas, spaces or tabs all work.

What is a good R² value?

It depends on the field. R² is the share of variation in y the line explains, from 0 to 1. Check a scatter plot as well.

What's the difference between r and R²?

r is the correlation, from −1 to 1, and shows direction. R² is r squared, from 0 to 1, and shows how much variation the line explains.

Can I predict values with it?

Yes. Enter an x value to get the predicted y, but avoid predicting far outside the range of your data.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Math Tools