Tutorials › Introduction to Statistics › Why Least Squares Squares the Errors

Least-Squares Regression Lines · Tutorial 263 of 996

Why Least Squares Squares the Errors

Learn how signed errors, absolute errors, and squared errors behave, and why regression uses the sum of squared errors to compare fits.

Beginner 10 min read

What You'll Learn

  • Define signed, absolute, and squared prediction errors.
  • Explain how positive and negative errors can cancel in a signed total.
  • Compare how absolute and squared errors penalize a large error.
  • Calculate and compare error totals for candidate fitted lines.
  • Explain why squared error is convenient to minimize mathematically.
  • Recognize that least squares is a choice of fitting criterion, not a guarantee of perfect predictions.

Why the Way We Add Errors Matters

A fitted line makes a prediction for each observed value of the response. The difference between an observed response and its prediction is a prediction error, also called a residual. When we compare possible fitted lines, we need a rule for combining their individual errors into one measure of overall fit. That rule matters: adding signed errors, adding absolute errors, and adding squared errors can give very different impressions of the same predictions.

Least-squares regression uses the sum of squared errors. Squaring prevents positive and negative errors from cancelling, and makes a large error count more than several small errors with the same combined absolute size. It also gives a smooth function of the line’s coefficients, which is convenient to minimize.

Definition: For observation \(i\), let \(y_i\) be its observed response and \(\hat{y}_i\) its predicted response from a candidate line. The signed error is \(e_i=y_i-\hat{y}_i\). Three ways to combine errors over \(n\) observations are the signed total \(\sum_{i=1}^{n}e_i\), the total absolute error \(\sum_{i=1}^{n}|e_i|\), and the sum of squared errors \(\sum_{i=1}^{n}e_i^2\). Least squares chooses the regression line to minimize the sum of squared errors.

All three totals compare the same observations using the same predictions, but they answer different questions. A signed total preserves direction: errors above and below predictions have opposite signs. An absolute-error total measures the combined size of errors without their direction. A squared-error total also ignores direction, while assigning a greater penalty to larger errors. The squared total is expressed in squared response units; if responses are measured in centimeters, for example, the sum of squared errors is in square centimeters.

Signed Errors Can Cancel

A positive signed error means the observed response is above its prediction; a negative signed error means it is below. Adding the signed errors can therefore make a poor set of predictions look deceptively balanced. In particular, the total can be zero even though some predictions are wrong.

Worked Example: A Zero Signed Total Does Not Mean Zero Error

Suppose two hypothetical response values are both 10 units. A candidate line predicts 8 units for the first and 12 units for the second. Calculate the three error totals.

The signed errors, observed minus predicted, are \(10-8=2\) units and \(10-12=-2\) units. Their signed total is \(2+(-2)=0\) units. The errors cancel, although neither prediction is exact.

The total absolute error is \(|2|+|-2|=2+2=4\) units. The sum of squared errors is \(2^2+(-2)^2=4+4=8\) squared units. Both totals show that the candidate line has errors, unlike the signed total.

For comparison, a line predicting 10 units for both observations has signed total 0, total absolute error 0, and sum of squared errors 0. Thus a signed total of zero alone cannot distinguish a perfect fit from one with errors in opposite directions.

For a least-squares regression line with an intercept, the signed residuals sum to zero. This is a balance property of that fitted line, not evidence that all its predictions are exact. It is one reason the signed total is not the criterion used to choose the line: minimizing a signed total would not distinguish many sets of positive and negative errors at all.

Absolute Errors and Squared Errors Penalize Differently

Absolute errors and squared errors both prevent cancellation, but they do not give the same weight to a large miss. To see the difference, compare errors with the same total absolute size. If an error has magnitude \(a\), its absolute contribution is \(a\), while its squared contribution is \(a^2\). Doubling the error magnitude doubles its absolute contribution but quadruples its squared contribution.

Worked Example: Equal Absolute Totals, Different Squared Totals

Consider two hypothetical sets of errors, all measured in the same response units: Set A has errors \(2\) and \(2\); Set B has errors \(3\) and \(1\). Compare their absolute and squared totals.

For Set A, the total absolute error is \(|2|+|2|=4\) units, and the sum of squared errors is \(2^2+2^2=4+4=8\) squared units.

For Set B, the total absolute error is \(|3|+|1|=4\) units, while the sum of squared errors is \(3^2+1^2=9+1=10\) squared units. Both sets have the same total absolute error, but the squared-error criterion gives the set containing the error of magnitude 3 a larger total.

The contrast is stronger if the errors are \(4\) and \(0\): their absolute total is still 4 units, but their squared total is \(4^2+0^2=16\) squared units. Squared error places comparatively greater weight on concentrating the error in one large miss.

This extra weight is a feature of the criterion, not a claim that every large error is especially harmful in every application. If a few unusually large errors should not dominate the assessment, a different criterion may be appropriate. Least squares is useful when penalizing large misses strongly is a reasonable choice for the problem.

Why Squaring Is Convenient to Minimize

A second reason for squaring is mathematical. Absolute value creates a corner at zero, while the square changes smoothly through zero. For a fixed observed response \(y_i\), the squared error as a function of a prediction \(\hat{y}_i\) is \((y_i-\hat{y}_i)^2\). Its derivative with respect to the prediction is \(-2(y_i-\hat{y}_i)\), which exists at every prediction, including an exact prediction. That smoothness makes calculus-based optimization of a regression line straightforward.

A simple constant-prediction problem shows how the square leads to a precise minimization result. Suppose a model must use the same prediction \(c\) for every response. The best value of \(c\) under squared error minimizes the sum of squared differences from the observations.

$$ Q(c)=\sum_{i=1}^{n}(y_i-c)^2 $$

Differentiate with respect to \(c\), set the derivative equal to zero, and solve. The second derivative is positive, so the stationary value is the minimum.

$$ \begin{aligned} Q'(c)&=-2\sum_{i=1}^{n}(y_i-c)=0\\ \sum_{i=1}^{n}(y_i-c)&=0\\ \sum_{i=1}^{n}y_i-nc&=0\\ c&=\frac{\sum_{i=1}^{n}y_i}{n}=\bar{x}_y \end{aligned} $$

Here \(\bar{x}_y\) denotes the arithmetic mean of the response values \(y_1,\ldots,y_n\). Equivalently, the minimizing constant is the mean response. To check that it is a minimum rather than a maximum, \(Q''(c)=2n>0\) for \(n>0\). This derivation illustrates how a squared-error objective can be minimized using a smooth function; in regression, the predictions depend on the line’s intercept and slope instead of one constant.

Comparing Candidate Regression Lines

To compare candidate lines using least squares, calculate the predictions for the same observations, find each signed error, square each error, and add. A candidate with the smaller sum of squared errors is preferred by this criterion. The comparison is about the observed data and the stated criterion; it does not establish that one line will predict every future response better.

Worked Example: Comparing Two Candidate Lines

For three hypothetical observations, the predictor values are \(x=0,1,2\), and the responses are \(y=1,2,5\). Compare the candidate lines \(\hat{y}=1+2x\) and \(\hat{y}=2+x\) using signed totals, total absolute error, and sum of squared errors.

For the first line, the predictions at \(x=0,1,2\) are \(1,3,5\). The signed errors \(y_i-\hat{y}_i\) are \(1-1=0\), \(2-3=-1\), and \(5-5=0\). Their signed total is \(0+(-1)+0=-1\) response unit; their total absolute error is \(0+1+0=1\) response unit; and their sum of squared errors is \(0^2+(-1)^2+0^2=1\) squared response unit.

For the second line, the predictions are \(2,3,4\). Its signed errors are \(1-2=-1\), \(2-3=-1\), and \(5-4=1\). Their signed total is \(-1+(-1)+1=-1\) response unit. The total absolute error is \(1+1+1=3\) response units, and the sum of squared errors is \((-1)^2+(-1)^2+1^2=3\) squared response units.

The signed totals are equal, so they do not distinguish these candidates. Both the absolute-error and squared-error totals are smaller for the first line: 1 rather than 3. Least squares therefore prefers the first candidate among these two. This comparison does not say that the first line is perfect; it says only that its squared errors sum to less on these three observations.

What the Criterion Does—and Does Not—Tell You

Least squares gives a clear, calculable rule for choosing a line: minimize the sum of squared residuals. It does not make all residuals small, remove the effect of unusual observations, or guarantee that a straight line is a suitable description of the relationship. A large residual can have substantial influence on the squared total, while patterns or limitations in the data still require attention.

The three criteria can be summarized as follows:

CriterionCan positive and negative errors cancel?Effect of a larger error
Signed totalYesContribution grows with magnitude but retains its sign
Total absolute errorNoContribution grows in direct proportion to magnitude
Sum of squared errorsNoContribution grows with the square of magnitude

A common mistake is to argue, “The signed errors add to zero, so the line has no prediction error.” The correction is that opposite-signed errors can cancel in a total even when individual predictions are wrong. Inspect the individual errors or use a nonnegative error criterion such as total absolute error or sum of squared errors. A related mistake is to treat the squared-error total as if it were measured in the original response units; it is measured in squared units.

Key takeaway: Least squares squares residuals because squaring prevents cancellation, emphasizes larger prediction errors, and produces an objective that is smooth to minimize. It is a specific choice about how to value errors, not a universal guarantee of the best fit for every purpose.

Check Your Understanding

Try these questions before reviewing the examples.

  1. Two errors are \(+5\) and \(-5\) response units. What are their signed total, total absolute error, and sum of squared errors?
  2. Why can two candidate fits have the same signed total even when their prediction accuracy differs?
  3. Compare the squared-error contributions of errors with magnitudes 2 and 4. How does the larger error’s contribution compare with the smaller one?
  4. Two error sets are \((2,2)\) and \((3,1)\). Which has the larger sum of squared errors, and what do their total absolute errors have in common?
  5. In the constant-prediction derivation, why does \(Q''(c)=2n>0\) show that the stationary value is a minimum?