Linear Regression Calculator

Paste your X and Y values to compute the best-fit regression line.

How it is calculated

Enter the x values and the y values as two lists, separated by spaces, commas or semicolons. They must have the same length, at least two pairs, and be plain numbers with a decimal point (a decimal comma would be read as a separator and the lists would no longer match). With n pairs the tool computes slope = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²), intercept = (Σy − slope × Σx) ÷ n and r = (nΣxy − ΣxΣy) ÷ √[(nΣx² − (Σx)²)(nΣy² − (Σy)²)], and R² = r². All values show four decimals. If every x is identical there is no unique line and no result appears. If every y is identical the line is flat and r and R² are reported as 0.

Worked example

Five points: x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1. Sums: Σx = 15, Σy = 30.1, Σx² = 55, Σxy = 110.2. Slope = (5 × 110.2 − 15 × 30.1) ÷ (5 × 55 − 15²) = 99.5 ÷ 50 = 1.99. Intercept = (30.1 − 1.99 × 15) ÷ 5 = 0.05. The line is y = 1.9900x + 0.0500, with r = 0.9987 and R² = 0.9973, so almost all the variation in y is explained by x.

Try this example →

Common mistakes

  • Typing decimals with a comma (1,5), which is treated as a list separator and splits one number into two; use a dot.
  • Listing x and y in different orders or lengths, so the pairs are misaligned; a single stray non-numeric token makes the whole input invalid.
  • Swapping x and y; the regression of y on x is a different line from x on y, and only r and R² stay the same.
  • Reading a high R² as proof of cause, or extrapolating far outside the range of the x data.
  • Expecting a meaningful R² when all y values are equal; the fit is flat, but the tool then reports r and R² as 0.

About this calculator

A linear regression calculator fits a straight line y = mx + b to a set of (x, y) data points by minimizing the sum of squared vertical distances between the line and the points — the method of least squares. The slope is m = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) and the intercept is b = (Σy − mΣx) / n; the calculator also reports the Pearson correlation coefficient r and the coefficient of determination R², which tells you what fraction of the variance in y is explained by x.

This is the same math behind the trendline feature in Excel or Google Sheets, and it underpins everything from stock-price trend analysis to lab-instrument calibration curves. Students, scientists, engineers, and analysts use it to quantify a relationship between two variables, forecast a y value for a new x, and judge — via R² — how reliable that forecast actually is.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    When x values are years such as 2015 to 2024, the intercept is the value at year zero, which is meaningless and often huge. Subtract the first year from every x so the series reads 0, 1, 2 and so on: the slope stays the same and the intercept becomes the fitted value in your first year. What kind of data do you usually fit, and how many points do you have?

Popular calculators

All Calculators