Skip to main content
CalcChef

Linear Regression Calculator

Least-squares line y = mx + b for your (x, y) data, with Pearson r, R², the number of points used and predictions of y from x or x from y. How it works

Use the Linear Regression Calculator

One point per line: x and y separated by a comma, semicolon, tab or space. Use a point for decimals (2.5, not 2,5). Lines that are not two numbers are skipped and listed below.

Equation
y = 0.6x + 2.2

Fit

Points used (n)

5

Slope (m)

0.6

Intercept (b)

2.2

Correlation (r)

≈ 0.7745966692

R²

0.6

123452345xy
Dots are your points, the line is the fit.

Least squares on exact fractions of the numbers you typed, from mean-centred sums. Predictions outside the range of your x values are extrapolations.

About the Linear Regression Calculator

For the points (1, 2), (2, 4), (3, 5), (4, 4) and (5, 5) the least-squares line is y = 0.6x + 2.2, with r ≈ 0.7745966692 and R² = 0.6. This linear regression calculator fits y = mx + b to your (x, y) data, shows Pearson r, R² and the number of points used, and predicts y from x or x from y.

Worked regressions

Data (x, y)LinerR²
(1, 2), (2, 4), (3, 5), (4, 4), (5, 5)y = 0.6x + 2.2≈ 0.77459666920.6
(1, 3), (2, 5), (3, 7), (4, 9)y = 2x + 111
(1, 52), (2, 55), (3, 61), (4, 64), (5, 68)y = 4.1x + 47.7≈ 0.9943960626≈ 0.9888235294
(1, 10), (2, 8), (3, 7), (4, 4), (5, 1)y = -2.2x + 12.6≈ -0.98386991010.968
(2020, 150), (2021, 163), (2022, 171), (2023, 188), (2024, 199)y = 12.3x − 24696.4≈ 0.995437588≈ 0.9908959916
(1, 5), (2, 5), (3, 5)y = 5undefinedundefined

Type one point per line in the tool, such as 1,2.

The working for the first data set

QuantityValue
Points used (n)5
Mean of x (x̄)3
Mean of y (ȳ)4
Sxx = Σ(x − x̄)²10
Syy = Σ(y − ȳ)²6
Sxy = Σ(x − x̄)(y − ȳ)6
Slope m = Sxy / Sxx0.6
Intercept b = ȳ − m·x̄2.2
R² = Sxy² / (Sxx·Syy)0.6

Assumptions

  • A straight line fits: y changes by the same amount for each step in x. Check by plotting the residuals, y minus the fitted value, against x and looking for a bend. For the first data set they are -0.8, 0.6, 1, -0.6 and -0.2, with no steady curve, though five points show little.
  • The errors are independent: one residual says nothing about the next, which data collected in time order can break. Check with a lag plot of each residual against the one before it. For the sales by year they are 0.4, 1.1, -3.2, 1.5 and 0.2.
  • The spread is the same at every x. Check by plotting the residuals against the fitted values: a horn shape that widens to one side means unequal variance.
  • The errors are roughly normal. The fitted line does not need this, but confidence intervals and p-values do, and this tool gives neither. Check with a histogram or a normal probability plot of the residuals.

Frequently asked questions

What do the slope, intercept and R² tell me?
The slope is the change in y for each 1 added to x: in y = 12.3x − 24696.4, sales rise by 12.3 a year. The intercept is y at x = 0, which for years is a number far outside the data with no meaning of its own. R² is the share of the variation in y the line explains: 0.98 means the points sit close to the line, 0.6 means much more scatter. A high R² does not show that x causes y, and a curved pattern can still give a high R².
What is the difference between r and R²?
r is the correlation, from -1 to 1, and its sign follows the slope. R² is r squared, the share of the variation in y the line explains. For (1, 10), (2, 8), (3, 7), (4, 4), (5, 1), r ≈ -0.9838699101 and R² = 0.968.
How do you predict a value from the regression line?
Substitute into the line. With y = 0.6x + 2.2, x = 6 gives y = 5.8, and y = 5 gives x ≈ 4.666666667. Type x or y into the prediction boxes, and the scatter plot marks each prediction as a diamond on the line. A prediction outside the range of your x values is an extrapolation.
How should I enter the data?
One point per line, x then y, separated by a comma, semicolon, tab or space: 1,2 or 1 2. Use a point for decimals. Lines that are not two numbers, such as a header row, are skipped and listed, and the tool shows how many points it used. Up to 10,000 points are accepted.
Why does it say R² is undefined?
Every y value is the same, so there is no variation for the line to explain. For (1, 5), (2, 5), (3, 5) the line is y = 5, and r and R² are undefined. If every x is the same, no line can be fitted at all.
Can I use years or other large x values?
Yes. The sums are exact, so years such as 2020 to 2024 work: sales of 150, 163, 171, 188 and 199 give y = 12.3x − 24696.4. Points at x = 100000001, 100000002 and 100000003 also fit without losing digits.

Related Tools

View all tools