Linear Regression Calculator
Paste two columns and get the regression line, then predict y for any x.
https://flxcode.com/linear-regression-calculator/
One pair per line, x then y, separated by a tab, space, comma or semicolon. Paste two columns straight from a spreadsheet. A header row is ignored.
Example Linear regression
Least-squares regression line
Read 8 pairs. Ignored the header "Temp (C) Sales".
With fewer than 10 pairs the line can move a lot with one point.
The slope p-value is the same as the p-value of the correlation r: both test whether the slope is 0.
| Pairs (n) | 8 |
|---|---|
| Slope (b) | 11.9023 |
| Intercept (a) | 42.0493 |
| R² (share of y explained by x) | 0.996839 |
| Correlation r | 0.998418 |
| Standard error of the estimate (s) | 4.44145 |
| Standard error of the slope | 0.273611 |
| p-value for the slope (two-tailed, df = n − 2) | 9.879e−9 |
| Predicted y at x = 24 | 327.704 |
| In Excel or Sheets | =SLOPE(B1:B8, A1:A8), =INTERCEPT(B1:B8, A1:A8), =RSQ(B1:B8, A1:A8), =STEYX(B1:B8, A1:A8) |
The steps, with your numbers
Means: x̄ = 22.25, ȳ = 306.875
Σ(x − x̄)(y − ȳ) = 3,136.25 and Σ(x − x̄)² = 263.5
Slope b = 3,136.25 / 263.5 = 11.9023
Intercept a = ȳ − b × x̄ = 306.875 − 11.9023 × 22.25 = 42.0493
Residual of each pair = y − (a + b × x). Their squares add up to SSE = 118.359
R² = 1 − SSE / Σ(y − ȳ)² = 1 − 118.359 / 37,446.9 = 0.996839
s = √(SSE / (n − 2)) = √(118.359 / 6) = 4.44145
t = b / (s / √Σ(x − x̄)²) = 11.9023 / 0.273611 = 43.5007, with 6 degrees of freedom
Prediction: ŷ = a + b × x = 42.0493 + 11.9023 × 24 = 327.704
Residuals (actual y minus the line)
| x | y | Predicted y | Residual |
|---|---|---|---|
| 14 | 215 | 208.681 | 6.31879 |
| 16 | 230 | 232.486 | -2.48577 |
| 18 | 255 | 256.29 | -1.29032 |
| 21 | 290 | 291.997 | -1.99715 |
| 23 | 310 | 315.802 | -5.80171 |
| 26 | 355 | 351.509 | 3.49146 |
| 29 | 385 | 387.215 | -2.21537 |
| 31 | 415 | 411.02 | 3.98008 |
No ads. No account. Nothing you type or record is uploaded. We count page events with a daily code. Check it. What this tool keeps and sends.
Problem or idea? Email us
Quick start
- Paste x in the first column and y in the second. A header row is ignored.
- The equation of the line is the headline. R², the standard error and the slope p-value are in the table.
- Type an x to see the predicted y. Scroll down for the residuals.
The formulas
b = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² and a = ȳ − b × x̄
The line is ŷ = a + b × x. The residual of each point is y − ŷ, SSE is the sum of their squares, R² = 1 − SSE / Σ(y − ȳ)² and s = √(SSE / (n − 2)).
Worked examples
| Data | Line | R² | s |
|---|---|---|---|
| Temperature and ice cream sales, 8 days | y = 42.0493 + 11.9023 × x | 0.996839 | 4.44145 |
| Car age in years and price, 6 cars | y = 2,525 − 175 × x | 0.988473 | 79.0569 |
| Two points, (2, 5) and (6, 13) | y = 1 + 2 × x | 1 | none |
In the car example the slope is negative: each extra year takes about 175 off the price.
Predicting outside the data
Inside the range of your x values, a prediction is an interpolation. Outside it, it is an extrapolation, and the tool says so in a note. The line does not know whether the pattern keeps going. Four points on y = 2x + 1 predict 21 at x = 10, which is only as good as the straight-line guess.
Regression or correlation?
Regression gives the line and predictions. The correlation coefficient calculator leads with r, the strength and direction of the relationship, and adds the Spearman rank correlation.
Numbers this uses
- Method is ordinary least squares, y on x. The same line as SLOPE and INTERCEPT in Excel.
- Residual table is first 50 rows, copy all. The copy has every row, tab separated, for a spreadsheet.
- Limit is 100,000 pairs. Kept low so the page stays quick on a phone.
Limits
It fits one straight line to one x. There is no curve fitting, no weights and no second predictor. The p-value assumes the residuals are roughly normal and similar in size across x. A single outlier can tilt the line, so look at the scatter plot.
In Excel and Google Sheets
Type the example in A1:B9: the headers in row 1, x in column A and y in column B. Excel and Sheets want y first. The formulas below work the same in both programs: every function name was checked against Google's function list.
| Task | Formula | Result |
|---|---|---|
| Slope | =SLOPE(B2:B9,A2:A9) | 11.9023 |
| Intercept | =INTERCEPT(B2:B9,A2:A9) | 42.0493 |
| R squared | =RSQ(B2:B9,A2:A9) | 0.996839 |
| Predicted y at x = 24 | =FORECAST.LINEAR(24,B2:B9,A2:A9) | 327.704 |
| Standard error of the estimate | =STEYX(B2:B9,A2:A9) | 4.44145 |
LINEST(known_y, known_x, TRUE, TRUE) returns the slope, the intercept and the fit statistics in one block. In Excel before 365, select a block of 5 rows by 2 columns and confirm with Ctrl+Shift+Enter.
You can also paste the column into the box above, or open an .xlsx or .csv file with the Open button, and use Download .xlsx to get these formulas filled in with your own numbers.
FAQ
What does linear regression do?
It finds the straight line that comes closest to your points, in the least-squares sense: the line that makes the sum of the squared vertical gaps as small as possible. The line is y = a + b × x, where b is the slope and a the intercept.
How do I use the line to predict y?
Type an x in the prediction box. The tool puts it into the equation. For the temperature and sales example, the line gives 327.704 sales at 24 degrees. It also says when your x is outside the range of your data.
What is R squared?
R² is the share of the variation in y that the line accounts for. In the default example it is 0.996839, so the line explains almost all of the variation. An R² of 0 means the line does no better than the average of y.
What is the standard error?
The standard error of the estimate, s, is a typical size of the residuals, the gaps between your points and the line: s = √(SSE / (n − 2)). It is in the units of y. For the default example it is 4.44145. It needs at least 3 pairs.
What is the p-value of the slope?
It tests whether the slope could be 0. It comes from t = b / SE(b) with n − 2 degrees of freedom. It is always the same number as the p-value of the correlation r, so the correlation calculator shows it too. For the default example it is 9.879e−9.
What is a residual?
A residual is a point's actual y minus the y the line predicts at its x. The table shows the first 50. Use "Copy all residuals" to paste every row into a spreadsheet. Residuals that are all the same sign, or that fan out, say a straight line may be the wrong shape.
More from FLXCode
- Correlation Coefficient CalculatorPearson r, r squared, Spearman rho and p-value from two pasted columns, with a scatter plot.
- Standard Deviation CalculatorPaste your numbers and get the sample and population standard deviation, with the steps shown.
- P-Value Calculatorp-value from a z, t, chi-square or F statistic, one or two tailed, with the tails shown.
- Variance CalculatorVariance of a list of numbers, sample or population, with the sum of squares and the steps.