The idea
Key formulas
Simple linear regression
Residual
Loading lessons…
Just a moment, getting everything ready. The first load can take up to a minute.
Machine learning
Fit the line that minimizes squared residuals and interpret its slope and goodness of fit.
Linear regression describes a relationship between an input and a numeric outcome using a line. In simple linear regression, the prediction at input is
where is the slope, is the intercept, and is the predicted output. The slope tells how much the predicted output changes for a one-unit increase in . The intercept is the model's prediction at . Whether that value is meaningful depends on the context and whether zero is a sensible input value.
Observed data points usually do not fall exactly on one line. For point , the model predicts , and its is
A positive residual means the point lies above the fitted line and the model underpredicted it; a negative residual means it lies below and was overpredicted. Residuals are vertical differences in the outcome, not distances measured perpendicular to the line.
The ordinary least-squares line chooses and to minimize the sum of squared residuals:
Why squares? Squaring makes positive and negative errors unable to cancel, gives larger misses more weight, and creates an objective with a convenient algebraic solution. For simple regression with a nonconstant input, its least-squares coefficients can be written
This construction makes the fitted line pass through the point , the averages of the observed inputs and outputs. The line is “best” according to this particular squared-vertical-error criterion; another loss or a different model can yield a different fit.
The squares also explain why a few distant points may have a large influence. A residual of contributes to SSE, while a residual of contributes only . This weighting is often useful when large misses should count strongly, but it makes the fit sensitive to unusual observations. Least squares does not automatically distinguish a data-entry mistake from a valid but rare case; both appear as residuals to the optimization.
For simple regression with an intercept, the residuals of the fitted line sum to zero, and the fitted line passes through the sample means. These properties follow from minimizing the squared errors with respect to both the slope and intercept. They are useful arithmetic checks, but do not mean that every residual is small or that errors are unstructured. Residuals can cancel in their signed sum while still having a large total squared magnitude.
The coefficient of determination compares squared residual error with the error from a baseline that always predicts the observed mean:
When the outcome varies, means predictions match all observed values, and means the model's squared error is the same as the mean-prediction baseline. It can be negative when predictions do worse than that baseline. It is a measure of fit relative to the baseline, not a probability that a prediction is correct or proof that the model captures a causal mechanism.
With features, the linear model becomes
Each coefficient describes the model's predicted change for its feature when the other features are held fixed. In matrix notation, predictions are , where each row of contains one example's feature values; a separate intercept can be included as a column of ones. Least squares chooses coefficients to minimize , the squared residual sum. When features are strongly correlated, the design matrix can be close to singular and estimated coefficients may be sensitive to small data changes.