The line of best fit
Simple linear regression finds the straight line y = a + bx that best describes how y changes with x. “Best” means it minimizes the sum of squared vertical distances between the points and the line: the least-squares criterion.
The formulas
- Slope b = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²)
- Intercept a = (Σy − bΣx) ÷ n
- Correlation r = (nΣxy − ΣxΣy) ÷ √[(nΣx² − (Σx)²)(nΣy² − (Σy)²)]
- R² = r², the share of the variance of y explained by x
Worked example
| x | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| y | 2 | 4.1 | 5.9 | 8.2 | 9.9 |
- Σx = 15, Σy = 30.1, Σxy = 110.2, Σx² = 55.
- Slope: (5 × 110.2 − 15 × 30.1) ÷ (5 × 55 − 15²) = 99.5 ÷ 50 = 1.99.
- Intercept: (30.1 − 1.99 × 15) ÷ 5 = 0.05.
- Line: y = 0.05 + 1.99x.
- r ≈ 0.9992, R² ≈ 0.998.
- Prediction for x = 6: 0.05 + 1.99 × 6 = 11.99.
Reading r
| |r| | Strength of the linear relationship | |—|—| | 0.9 to 1 | very strong | | 0.7 to 0.9 | strong | | 0.4 to 0.7 | moderate | | below 0.4 | weak |
The sign of r gives the direction: positive when y rises with x, negative when it falls.
Assumptions to check
- Linearity: plot the data first. A curved pattern needs a different model.
- Outliers: one extreme point can change the slope a lot. Check whether it is an error.
- Constant spread: the scatter around the line should be similar across x values.
- Independence: observations should not depend on each other, as they often do in time series.
Correlation is not causation
Ice cream sales and drownings both rise in summer, but neither causes the other: temperature drives both. Regression describes association; establishing cause requires experiments or careful study design.
Beyond two variables
Multiple regression adds more predictors, and polynomial or logarithmic models handle curves. Spreadsheets offer these through functions such as SLOPE, INTERCEPT, CORREL and LINEST, which give the same results as this calculator for a straight line.