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
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) | Line | r | R² |
|---|---|---|---|
| (1, 2), (2, 4), (3, 5), (4, 4), (5, 5) | y = 0.6x + 2.2 | ≈ 0.7745966692 | 0.6 |
| (1, 3), (2, 5), (3, 7), (4, 9) | y = 2x + 1 | 1 | 1 |
| (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.9838699101 | 0.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 = 5 | undefined | undefined |
Type one point per line in the tool, such as 1,2.
The working for the first data set
| Quantity | Value |
|---|---|
| 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 / Sxx | 0.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 toolsP-Value Calculator
P-value from a z, t, chi-square or F statistic and its degrees of freedom, left-, right- or two-tailed, with an optional comparison against alpha.
Permutation and Combination Calculator
Counts permutations nPr and combinations nCr for n and r up to 1000, with or without repetition. Every digit is exact and the formula is worked through.
Standard Deviation
Standard deviation calculator for a list of numbers: population σ and sample s side by side, both variances, plus mean, median, minimum, maximum, range and sum.
Matrix Calculator
Add, subtract and multiply matrices up to 10 × 10, or get the determinant, inverse, transpose, rank and row-reduced form. Answers are exact fractions.
RREF Calculator
Reduce a matrix of up to 6 × 6 to reduced row echelon form by Gauss-Jordan elimination, with rank, pivot columns and row steps, as decimals or exact fractions.
System of Equations Solver
Solve 2 or 3 linear equations exactly, with Gauss-Jordan or Cramer’s rule steps and a graph of two lines. Says when there is no solution or infinitely many.