Linear Regression Calculator
Paste two columns to fit the straight line that best predicts y from x…
Paste two columns to fit the straight line that best predicts y from x, with the uncertainty in its slope, a residual plot and a prediction for any x.
Read 12 pairs; left out the header row (“Distance (km)”, “Fare”).
Type an x
More options95% confidence
ExampleLine of best fit
ŷ = 3.8427 + 1.674x
Each 1 more Distance (km) goes with 1.674 more Fare (95% CI 1.653 to 1.695). R² = 0.9997: the line accounts for 99.97% of the variation in Fare.
Paste your own two columns, or drag a point.
Drag any point and watch the line and R² change. With the plot focused, arrow keys move the highlighted point (the one furthest from the line) and [ ] pick another.
Residuals: what the line misses
No pattern here is good news for a straight line; a curve or a funnel means the line misses something.
- R² (r)
- 0.9997 (0.9998)
- Slope p-value
- 9.28 × 10⁻¹⁹
- Residual SE
- 0.1089
How this works: 9 sources,
How this works
Method
Simple linear regression fits the straight line ŷ = a + bx that makes the squared vertical distances from the points to the line as small as possible. In the example, twelve taxi trips give fare = 3.84 + 1.674 × distance: about 1.67 per kilometre plus 3.84 to start, which is how most meters are set up. Full method
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods, 4.1.4.1 Linear Least Squares Regression
- Anscombe, Graphs in statistical analysis (The American Statistician, 1973)
- NIST/SEMATECH e-Handbook of Statistical Methods
- Abramowitz and Stegun, Handbook of Mathematical Functions, chapters 6, 7 and 26 (1964)
- SciPy 1.18 scipy.stats (reference values for the tests)
- statsmodels 0.15 (reference values for the A/B test and regression intervals)
- Welch, The generalization of Student’s problem when several different population variances are involved (Biometrika) (1947)
- Fleiss, Levin and Paik, Statistical Methods for Rates and Proportions, 3rd edition (sample size for two proportions) (2003)
- Hyndman and Fan, Sample quantiles in statistical packages (The American Statistician) (1996)
How it’s tested
2 worked examples for this page are checked by automated tests before every release: given the inputs, the tool must show the expected answer.
Privacy
On your device. Nothing you enter or open leaves this browser. About privacy labels
Changes
- First published: live curves with draggable cut-offs, and tests on pasted data (t-tests, ANOVA, chi-square, Fisher’s exact, correlation, regression, A/B), checked against SciPy.
How least-squares regression works
Simple linear regression fits the straight line ŷ = a + bx that makes the squared vertical distances from the points to the line as small as possible. In the example, twelve taxi trips give fare = 3.84 + 1.674 × distance: about 1.67 per kilometre plus 3.84 to start, which is how most meters are set up.
The slope is Sxy ÷ Sxx, the cross-products of the deviations over the squared x deviations, and the line always passes through the point of means (x̄, ȳ). What the line misses are the residuals; their standard deviation, with n − 2 degrees of freedom, is the residual standard error s. It sets the standard errors of the slope (s ÷ √Sxx) and the intercept, and from those come their t tests and confidence intervals.
R² is the share of the variation in y the line explains. A high R² does not prove a straight line is right: check the residual plot, where the points should scatter evenly around zero. A curve in the residuals means a straight line is the wrong shape; a funnel means the spread grows with x. And predictions far outside the range of your x values are guesses, however good the fit.
b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² · a = ȳ − b x̄ · SE(b) = s ÷ √Σ(x − x̄)²
- Least squares chooses the coefficients that minimise the sum of the squared residuals; it is the most widely used method for fitting a line. Source: NIST/SEMATECH e-Handbook of Statistical Methods, 4.1.4.1 Linear Least Squares Regression.
- Plotting the data and the residuals matters: data sets with identical regression lines and R² can have completely different patterns. Source: Anscombe, Graphs in statistical analysis (The American Statistician, 1973).
Predictions from the example’s line
The taxi example’s fitted fare at different distances, with the 95% confidence interval for the average fare at that distance and the 95% prediction interval for a single trip.
| Distance (km) | Predicted fare | 95% CI (average) | 95% prediction interval |
|---|---|---|---|
| 1 | 5.52 | 5.4 to 5.64 | 5.25 to 5.79 |
| 2 | 7.19 | 7.09 to 7.29 | 6.93 to 7.45 |
| 3 | 8.86 | 8.78 to 8.95 | 8.61 to 9.12 |
| 5 | 12.21 | 12.14 to 12.28 | 11.96 to 12.47 |
| 8 | 17.23 | 17.15 to 17.32 | 16.98 to 17.49 |
| 10 | 20.58 | 20.46 to 20.7 | 20.31 to 20.85 |
| 12 | 23.93 | 23.78 to 24.08 | 23.64 to 24.22 |
| 15 | 28.95 | 28.74 to 29.17 | 28.63 to 29.27 |
| 20 | 37.32 | 37.01 to 37.64 | 36.92 to 37.72 |
Beyond 12.4 km the line is extrapolating past the trips it was fitted on.
Common questions
What is the difference between the confidence interval and the prediction interval?
The confidence interval is for the average y at a given x (the line itself); the prediction interval is for one new observation, so it adds the scatter of single points and is always wider.
Is a high R² enough to trust the line?
No. Look at the residual plot: an even band around zero is what a straight line should leave. A curve, a funnel or one point far off all mean the model needs a second look, whatever R² says.
What does the slope’s p-value test?
Whether the true slope could be zero, meaning y doesn’t change with x at all. It is the same p-value as the test of the correlation r.
Can I fit a curve here?
This page fits a straight line. For a curve such as exponential growth, transform first (fit y against log x, or log y against x) and paste the transformed columns.