Linear Regression Calculator
Find the line of best fit and R² from your data points.
Format: x,y — one pair per line. Blank lines and invalid rows are ignored.
| Slope (m) | Intercept (b) | R² | n | Mean X | Mean Y |
|---|---|---|---|---|---|
| 2.034286 | -0.053333 | 0.9979 | 6 | 3.5000 | 7.0667 |
| x | y | predicted | residual |
|---|---|---|---|
| 1.0000 | 2.1000 | 1.9810 | 0.1190 |
| 2.0000 | 3.9000 | 4.0152 | -0.1152 |
| 3.0000 | 6.2000 | 6.0495 | 0.1505 |
| 4.0000 | 7.8000 | 8.0838 | -0.2838 |
| 5.0000 | 10.1000 | 10.1181 | -0.0181 |
| 6.0000 | 12.3000 | 12.1524 | 0.1476 |
About Linear Regression
Linear regression finds the straight line that best fits a set of data points by minimizing the sum of squared residuals (ordinary least-squares). The formula produces slope m and intercept b so you can express any trend as y = mx + band predict new values. R² (coefficient of determination) measures fit quality: >0.9 is a strong relationship, 0.7–0.9 moderate, and below 0.7 weak. The correlation coefficient r adds direction — positive for upward trends, negative for downward. All calculations run locally in your browser; no data is sent to any server.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Fitting a line to a small set of paired values.
- Getting an R-squared figure to see how much variance the line explains.
- Predicting a value from the fitted line.
- Checking a slope you estimated by eye from a scatter plot.
- Producing a fit for a lab report or an exercise.
Frequently Asked Questions
- What does least squares actually minimise?
- The sum of the SQUARED vertical distances from each point to the line. Squaring is what makes the solution unique and computable in closed form, and it is also why a single distant outlier moves the line so much — its error is squared along with everything else.
- What does R² tell me?
- The proportion of the variance in y explained by the line, from 0 to 1. An R² of 0.95 means 95% of the variation is accounted for — and it says nothing about whether a straight line is the right model, which is the thing people read into it.
- Can a high R² still be the wrong model?
- Easily. Anscombe's quartet is the classic demonstration: four datasets with identical means, variances, regression lines and R², one of which is a clean parabola and another a single outlier driving everything. Plotting the residuals is what catches it.
- Does the slope mean x causes y?
- No, and this is the most consequential misreading of a regression. It measures association; causation requires an argument the arithmetic cannot supply. A confounder affecting both variables produces a strong, significant, entirely non-causal slope.
- Is it safe to predict outside the data range?
- No — extrapolation assumes the relationship continues, which the data cannot show. A linear fit over a narrow range routinely projects to absurd values well outside it, and biological and economic relationships in particular flatten or reverse at their extremes.
Common errors and gotchas
- Reading correlation as causation, which the arithmetic cannot distinguish.
- Extrapolating beyond the data range, where the fit has no support.
- Trusting R-squared alone, when a high value can still come with a badly shaped residual pattern.
- Fitting a line to data that is plainly curved.
- Letting a single outlier determine the slope without noticing.