Skip to main content

DigitalCalculators.net

Linear Regression Calculator

Find the line of best fit y = a + bx for your data with least squares, plus the correlation coefficient r, R², predictions and steps.

Results

ItemValue

Steps

StepWorking
Uses ordinary least squares. Separate x and y with a comma, space or tab. Use a period for decimals. At least 3 points are needed, and the x values cannot all be the same.

Quick Answer

Linear regression finds the straight line y = a + bx that best fits your data. The slope is b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) and the intercept is a = (Σy − bΣx) ÷ n. For the sample data above, the line is y = 2.2 + 0.6x, with r = 0.7746.

Last updated: 2 November 2026 ·How we calculate this: ordinary least squares

Table of Contents

How This Linear Regression Calculator Works

Enter your data as x, y pairs, one per line. The calculator adds up x, y, xy and x², then uses the least squares formulas to find the slope and intercept of the best-fit line. It also gives the correlation coefficient r, R², and a predicted y value for any x you enter.

Quick explanation: the best-fit line makes the squared vertical distances from the points as small as possible.

  • Slope: how much y changes for each 1 unit increase in x.
  • Intercept: the value of y when x is 0.
  • r: how closely the points follow a straight line, from −1 to 1.

How to Use the Linear Regression Calculator

  1. Type or paste your data, one x, y pair per line.
  2. Optionally, enter an x value to predict y.
  3. Press Calculate.
  4. Read the line of best fit, r and R².
  5. Follow the steps to check the math by hand.

Core Formulas

  • Slope: b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²)
  • Intercept: a = (Σy − bΣx) ÷ n
  • Correlation: r = (nΣxy − ΣxΣy) ÷ √((nΣx² − (Σx)²)(nΣy² − (Σy)²))
  • R²: r × r, the share of the variation in y explained by x

Key Terms

  • Regression line: the straight line that best fits the data.
  • Least squares: the method that minimizes the sum of squared residuals.
  • Residual: the actual y minus the predicted y.
  • Correlation coefficient (r): a measure from −1 to 1 of how well points fit a line.
  • Coefficient of determination (R²): the share of variation in y explained by x.

Worked Example

Hours studied and test scores: (2, 65), (3, 70), (5, 80), (6, 82), (8, 90).

  • Slope = 4.1404 points per hour, intercept = 57.5263
  • Line: score = 57.53 + 4.14 × hours, with r = 0.9945
  • Predicted score after 7 hours = 86.5

Real-Life Examples

  • Study time: the example above predicts about 87 points after 7 hours.
  • Sales trends: fit monthly sales against month number to see the trend per month.
  • Science labs: fit measurements to find a rate, such as speed from distance and time.
  • Pricing: fit house price against square footage to estimate price per square foot.

Correlation vs Causation

  • Correlation: two things move together.
  • Causation: one thing causes the other.
  • Caution: a strong r does not prove cause. A third factor may drive both.

Sample Data and Fitted Values

Line: y = 2.2 + 0.6x. Residuals add up to zero.

xyFitted yResidual
122.80-0.80
243.400.60
354.001.00
444.60-0.60
555.20-0.20

Reading r and R²

  • r near 1: a strong upward straight-line pattern.
  • r near −1: a strong downward straight-line pattern.
  • r near 0: little straight-line relationship.
  • R² of 0.8: 80% of the variation in y is explained by the line.

Common Mistakes to Avoid

  • Swapping x and y. x is the input or cause you control; y is the outcome.
  • Predicting far outside the data. The line may not hold beyond your range of x.
  • Fitting a line to a curve. Plot the data first to check it looks straight.
  • Trusting a few points. More data gives a more reliable line.
  • Ignoring outliers. One unusual point can tilt the line.

Frequently Asked Questions

Find Σx, Σy, Σxy and Σx², then use b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) and a = (Σy − bΣx) ÷ n.

How much y changes on average for each 1 unit increase in x.

The predicted y when x is 0, which may not make sense if 0 is outside your data.

It depends on the field. In physics, above 0.9 is common. In social sciences, much lower values can still be useful.

r shows strength and direction. R² is r squared and shows the share of variation explained.

At least 3, but more points give a more reliable result.

Yes. Copy two columns and paste them. Tabs between values work.

A straight line will fit poorly. Consider a different model, such as exponential or polynomial.

No. It always falls between −1 and 1.

A method that chooses the line with the smallest sum of squared vertical distances to the points.

No. It only shows that two variables move together.

Residuals show how far each point is from the line. Plotting them helps you spot a curve, outliers or a spread that grows with x, which suggest a straight line is not the best model.

Put the x value into the equation y = a + bx, or enter it in the prediction box.

References & Sources

SourceUsed ForLink
Wikipedia, Simple linear regression Least squares formulas for slope and intercept. https://en.wikipedia.org/wiki/Simple_linear_regression
NIST/SEMATECH e-Handbook, Linear least squares regression How least squares fitting works. https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm