Home / Math / Linear Regression Calculator
Linear Regression Calculator: Line of Best Fit
Paste two lists of numbers of equal length. The tool fits the least-squares line y = a + bx and reports the slope, intercept, correlation, R squared, standard error and residuals.
Least squares: what the line minimises
The line of best fit y = a + bx is chosen so that the sum of the squared vertical distances from the points to the line is as small as possible. Squaring stops positive and negative misses from cancelling and penalises large misses more. Calculus gives closed-form answers: b = Sxy ÷ Sxx and a = ȳ − b × x̄.
Here Sxx is the sum of (x − x̄)2 and Sxy is the sum of (x − x̄)(y − ȳ). The fitted line always passes through the point of means (x̄, ȳ). For x = 1 to 5 and y = 2, 4, 5, 4, 5, the means are 3 and 4, Sxx = 10 and Sxy = 6, so b = 0.6 and a = 2.2.
Correlation r and R squared
The correlation coefficient r = Sxy ÷ √(Sxx × Syy) runs from −1 to +1. Values near +1 mean a strong upward straight-line pattern, near −1 a strong downward one, and near 0 little straight-line relationship. R squared is r squared for a simple line fit: the proportion of the variation in y that the line accounts for. In the example r is about 0.775 and R squared is exactly 0.6.
The standard error of the estimate, √(SSE ÷ (n − 2)), is the typical size of a residual in the units of y. It needs at least three points because two points always fit a line perfectly.
Cautions when interpreting a fit
- Causation. A strong correlation does not show that x causes y; a third factor may drive both.
- Extrapolation. Predicting outside the range of your x values assumes the straight-line pattern continues, which is often false.
- Outliers. A single extreme point can tilt the line noticeably. Look at the residual column to spot points far from the fit.
- Curved data. A high R squared can still hide a curve. Plot the data when you can, and check that residuals have no obvious pattern.
Entering data
Type the x values in the first box and the y values in the second, in the same order, so the first x pairs with the first y. Separate numbers with commas, spaces or semicolons and use a point for decimals. The lists must have equal length, and the x values must not all be identical; otherwise the slope is undefined because the line would be vertical.
This is simple linear regression with one predictor. Multiple predictors, weighted fits, significance tests and confidence intervals for the slope are outside its scope. For a trend over time, use 1, 2, 3 and so on as x, or the actual year numbers.
Worked example
Five data points with x from 1 to 5 and y values 2, 4, 5, 4 and 5. The tool fits the line and predicts y when x is 6.
Inputs
| X values | 1, 2, 3, 4, 5 |
|---|---|
| Y values | 2, 4, 5, 4, 5 |
| Predict y for x = | 6 |
Result
R squared = 0.6: the line accounts for that share of the variation in y.
| Number of points | 5 |
|---|---|
| Slope (b) | 0.6 |
| Intercept (a) | 2.2 |
| Correlation coefficient (r) | 0.774597 |
| R squared | 0.6 |
| Mean of X, mean of Y | 3, 4 |
| Standard error of the estimate | 0.894427 |
| Predicted y at x = 6 | 5.8 |
Data, fitted values and residuals
| x | y | Fitted y | Residual |
|---|---|---|---|
| 1 | 2 | 2.8 | -0.8 |
| 2 | 4 | 3.4 | 0.6 |
| 3 | 5 | 4 | 1 |
| 4 | 4 | 4.6 | -0.6 |
| 5 | 5 | 5.2 | -0.2 |
- Correlation does not prove one variable causes the other.
- The prediction lies outside your x range, so it is an extrapolation and less reliable.
Accuracy note: Results use standard double-precision arithmetic, so very large or very small values may be rounded. Check important results by another method.
Frequently asked questions
What does the slope tell me?
The slope is the average change in y for each one-unit increase in x along the fitted line. A slope of 0.6 means y rises by about 0.6 for each extra unit of x.
What is a good R squared value?
It depends on the field. Physical measurements may reach values close to 1, while behavioural or economic data often have much lower values. Judge it against your own context.
Why does the line not pass through every point?
Real data contain scatter. The least-squares line balances the misses so that the sum of squared residuals is as small as possible, and it passes through the means.
Can I use dates as x values?
Yes, but convert them to numbers first, such as years or day counts. The slope is then the change in y per year or per day.
What is a residual?
A residual is the observed y minus the fitted y at the same x. Positive residuals sit above the line and negative ones below it, and they sum to zero.
Related calculators
References and sources
- Draper, N. R. and Smith, H. Applied Regression Analysis, 3rd edition, Wiley.
- Moore, D. S., McCabe, G. P. and Craig, B. A. Introduction to the Practice of Statistics, chapter on simple linear regression.