Linear Regression Calculator
The least-squares line of best fit through paired data, with r squared and predictions.
Result
Line of best fit: y = 0.6x + 2.2
Exact least squares on the points entered. A fitted line is not a forecast.
- Slope, b
- 0.6
- Intercept, a
- 2.2
- r squared
- 0.6
- Standard error of the estimate
- 0.894427
- Standard error of the slope
- 0.282843
- Number of points
- 5
https://calcgallery.com/statistics/linear-regression-calculator/
Results follow the mathematical operation and values shown. Check that you selected the intended operation, convention and dataset type before relying on the result. Read the full disclaimer
How to use the Linear Regression Calculator
- Enter your pairs of values, one x and y per row. At least three pairs.
- Optionally enter an x to predict y at.
- Read the line of best fit, then the slope, intercept, r squared, standard errors and the prediction.
The formulas
slope b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²intercept a = ȳ − b × x̄line y = a + b xr² = 1 − (sum of squared residuals) ÷ Σ(y − ȳ)²standard error of the estimate = √(sum of squared residuals ÷ (n − 2))
The line always passes through the point (x̄, ȳ), the means of the two variables. The residuals are the vertical gaps between each point and the line.
Measuring the fit
- r squared is the share of the variation in y the line explains. It equals the square of the correlation coefficient on the same data, and the two pages are tested against each other.
- The standard error of the estimate is the typical size of a residual, in the units of y. It indicates how far actual values tend to sit from the line.
- The standard error of the slope measures how precisely the slope is pinned down by this data. A slope several times its standard error is unlikely to be chance.
When a line cannot be fitted
If every x is the same, the points stand in a vertical column. A vertical line has no slope, and least squares cannot fit one. The page refuses rather than returning an infinite slope.
With your numbers
- b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² = 6 ÷ 10 = 0.6
- a = ȳ − b x̄ = 4 − 0.6 × 3 = 2.2
- y = 0.6x + 2.2
Quick reference
Exact least squares on the points entered. A fitted line is not a forecast.
| Example | Line of best fit |
|---|---|
| Five points, predict at 6 | y = 0.6x + 2.2 |
| Perfect line y = x | y = 1x + 0 |
| Falling line y = 12 − 2x | y = −2x + 12 |
| Hours studied and score, predict at 6 | y = 3.609589x + 57.308219 |
| Advertising and sales | y = 1.2x + 13 |
| Negative values | y = 1.9x + 0.8 |
Common questions about the Linear Regression Calculator
- What does the line of best fit minimise?
- The sum of the squared vertical distances from each point to the line, hence least squares. Squaring stops points above and below from cancelling out and weights large misses more than small ones. It is the standard definition, and what spreadsheet trend lines and the SLOPE and INTERCEPT functions use.
- Why is my prediction flagged?
- Because the x you entered is outside the range of your data. The line describes the relationship where you have observations, and says nothing reliable about what happens beyond them. A plant’s growth over its first month, extended for a year, would predict a tree. The page still gives the number, but marks it as extrapolation.
- Why do I need at least three points?
- Two points always lie exactly on a line, with an r squared of 1, which tells you nothing about how well a line describes the relationship. The standard error divides by n − 2, so with two points it is undefined. Three is the minimum for any measure of fit, and more is better.
- How do I read the slope?
- As the change in y for each one-unit increase in x. A slope of 0.6 in the worked example means that each extra unit of x goes with an average rise of 0.6 in y. The intercept is the value of y the line gives at x = 0, which is only meaningful if x = 0 is within, or close to, the data.