How the Least Squares Regression Line works
The least squares regression line is the straight line y = a + bx that best fits a set of (x, y) data points by minimizing the sum of the squared vertical distances ("residuals") between each point and the line. It is the standard method for finding a linear trend through scattered data in statistics, science, and business analysis.
Formula and method
For n data points, the slope and intercept that minimize the sum of squared residuals are given in closed form: the slope is b = (nΣxy − ΣxΣy) / (nΣx² − (Σx)²) and the intercept is a = (Σy − bΣx) / n = ȳ − b·x̄, where x̄ and ȳ are the mean of the x and y values. These come from setting the partial derivatives of the sum of squared errors, Σ(y − a − bx)², with respect to a and b equal to zero and solving the resulting "normal equations." Once you have a and b, the fitted line y = a + bx lets you predict y for any x. The coefficient of determination, R² = 1 − SSE/SST (where SSE is the sum of squared residuals and SST is the total sum of squared deviations of y from its mean), tells you what fraction of the variation in y is explained by the linear relationship with x.
Common sources of error
- Mismatched list lengths: the X and Y lists must have the same number of values, each x paired with its corresponding y in order.
- Too few points: fewer than 3 points gives an unreliable or trivial fit — 2 points always produce a "perfect" line with no meaningful residuals.
- Extrapolating too far: predicting y far outside the range of your x values assumes the linear trend continues, which is often not true in practice.
- Confusing correlation with causation: a strong R² shows the two variables move together linearly — it does not prove that x causes y.
Interpreting your results
Check the sign and size of the slope first: a positive b means y tends to increase as x increases, a negative b means it decreases, and the magnitude tells you how much y changes per unit of x. Then look at R²: values near 1 indicate the points sit close to the fitted line, while values near 0 indicate a weak or non-existent linear relationship (the data may still follow a nonlinear pattern that this straight-line fit cannot capture). Always plot or scan your raw data — least squares fits a line even to data that clearly is not linear, so R² and residuals should be reviewed together with a quick look at the numbers.