Search

Search papers and lessons

Residual Sum of Squares

Level 1 · 9 min read

The residual sum of squares adds up the squared gaps between what a model predicts and what was observed, giving one number that says how well a line fits.

On this page

  • Colour = topic mix
  • Discrete math
  • Inference
  • ReadReady to readNot yet

TL;DR

A model makes a prediction for each data point, and each prediction misses by some amount. The residual sum of squares, written RSS, squares each miss and adds them up. The result is one number: small when the predictions are close, large when they are not. It is the score that least-squares fitting tries to make as small as possible.

Builds on: Summation Notation · Leads to: F-Test, Mallows Cp, Forward Stepwise Selection

Where it's used

  • Fitting a straight line to measurements. A scientist measuring how a spring stretches under weight fits a line to the readings. The line with the smallest RSS is the one reported as the best fit. This is the core use of Linear Regression.
  • Choosing between models. A data analyst fits a model with three predictors and then one with five. The five-predictor model always has an RSS that is equal or lower, so the analyst needs a penalty for extra predictors. Mallows Cp is one such penalty, and it is built from RSS.
  • Testing whether a group of predictors helps. In a study of house prices, an analyst asks whether adding size, age and location together improves the fit. The drop in RSS between the two models is the raw material for the F-Test.

How it works

Take one tiny data set. There are four points, with x the input and y the observed output:

ixy
112
223
335
444

Compare two candidate lines. The model is a line that predicts a value of y for each x.

  1. Pick a line. Line A is y-hat = x + 1. Its predictions are 2, 3, 4 and 5 for x = 1, 2, 3, 4. Check: at x = 1 the line gives 1 + 1 = 2.
  2. Find each residual. A residual is the observed value minus the predicted value. For line A the residuals are 2 minus 2 = 0, 3 minus 3 = 0, 5 minus 4 = 1, and 4 minus 5 = -1. Check: the signs differ, so adding them directly would let positive and negative misses cancel.
  3. Square each residual. Squaring removes the sign and makes big misses count more. The squares are 0, 0, 1 and 1. Check: a miss of 2 would count four times as much as a miss of 1.
  4. Add the squares. RSS for line A is 0 + 0 + 1 + 1 = 2.
  5. Compare a second line. The line with the smallest RSS fits best. The least-squares line for these points is y-hat = 1.5 + 0.8x (the math section shows how to find it), with predictions 2.3, 3.1, 3.9 and 4.7. Its residuals are -0.3, -0.1, 1.1 and -0.7. Their squares are 0.09, 0.01, 1.21 and 0.49, so RSS = 1.80. Check: 1.80 is smaller than 2, so this line fits better than line A.

A flat line at y = 3.5 gives residuals of -1.5, -0.5, 1.5 and 0.5, and RSS = 5.0. Three lines, three scores: 5.0, 2.0 and 1.8. The score tells you which line to prefer.

flowchart LR
  D["Data points"] --> M["Candidate line"]
  M --> P["Predictions"]
  P --> R["Residuals: observed minus predicted"]
  R --> S["Square each residual"]
  S --> T["Add them up: RSS"]

The math

A residual for data point i is the gap between the observed value and the prediction:

ei=yi−y^ie_i = y_i - \hat{y}_i

The residual sum of squares adds the squared residuals over all n data points:

RSS=∑i=1nei2=∑i=1n(yi−y^i)2\text{RSS} = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} \left(y_i - \hat{y}_i\right)^2

Step 1 in symbols

For a line y^i=β0+β1xi\hat{y}_i = \beta_0 + \beta_1 x_i, the residual of point i is yi−β0−β1xiy_i - \beta_0 - \beta_1 x_i. The line is set by two numbers, the intercept β0\beta_0 and the slope β1\beta_1.

Step 2 in symbols

Check Line A on the data, with β0=1\beta_0 = 1 and β1=1\beta_1 = 1:

RSS=(2−2)2+(3−3)2+(5−4)2+(4−5)2=0+0+1+1=2\text{RSS} = (2-2)^2 + (3-3)^2 + (5-4)^2 + (4-5)^2 = 0 + 0 + 1 + 1 = 2

Step 3 in symbols

The least-squares line is the pair (β0,β1)(\beta_0, \beta_1) that makes RSS as small as possible. For this data, the minimum is at β0=1.5\beta_0 = 1.5 and β1=0.8\beta_1 = 0.8, with RSS = 1.80. The formula for the best slope is

β^1=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2\hat{\beta}_1 = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sum_i (x_i - \bar{x})^2}

where xˉ\bar{x} and yˉ\bar{y} are the averages of x and y. Here xˉ=2.5\bar{x} = 2.5 and yˉ=3.5\bar{y} = 3.5. The numerator adds the four products (xi−xˉ)(yi−yˉ)(x_i - \bar{x})(y_i - \bar{y}): (−1.5)(−1.5)+(−0.5)(−0.5)+(0.5)(1.5)+(1.5)(0.5)=2.25+0.25+0.75+0.75=4(-1.5)(-1.5) + (-0.5)(-0.5) + (0.5)(1.5) + (1.5)(0.5) = 2.25 + 0.25 + 0.75 + 0.75 = 4. The denominator adds the squared distances (xi−xˉ)2(x_i - \bar{x})^2: 2.25+0.25+0.25+2.25=52.25 + 0.25 + 0.25 + 2.25 = 5. So the slope is 4/5=0.84 / 5 = 0.8. The intercept then follows from the fact that the line passes through the point of averages: β^0=yˉ−β^1xˉ=3.5−0.8×2.5=1.5\hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x} = 3.5 - 0.8 \times 2.5 = 1.5.

Worked examples

Example
1. Two points (tiny)

Data: (1, 1) and (2, 3). Line: y-hat = x.

  1. Predictions: 1 and 2.
  2. Residuals: 1 minus 1 = 0, and 3 minus 2 = 1.
  3. Squares: 0 and 1.
  4. RSS = 0 + 1 = 1.

Interpretation: the line misses only the second point, by 1, so the score is 1.

Example
2. Four points (standard)

Data: the four points from the lesson, with the flat line y-hat = 3.5.

  1. Predictions: 3.5 for every point.
  2. Residuals: subtract 3.5 from each observed value. The observed values are 2, 3, 5 and 4, so the residuals are 2 minus 3.5 = -1.5, 3 minus 3.5 = -0.5, 5 minus 3.5 = 1.5, and 4 minus 3.5 = 0.5.
  3. Squares: 2.25, 0.25, 2.25 and 0.25.
  4. RSS = 2.25 + 0.25 + 2.25 + 0.25 = 5.0.

Interpretation: the flat line misses by a lot at both ends, so its RSS is the largest of the three lines.

Example
3. A spring (realistic)

A spring is measured at four loads. The observed stretches are 2.0, 4.1, 5.9 and 8.2 cm. A model predicts 2 cm per kilogram, so the predicted stretches at loads 1, 2, 3 and 4 kg are 2, 4, 6 and 8 cm.

  1. Residuals: 2.0 minus 2 = 0.0, 4.1 minus 4 = 0.1, 5.9 minus 6 = -0.1, and 8.2 minus 8 = 0.2.
  2. Squares: 0.00, 0.01, 0.01 and 0.04.
  3. RSS = 0.00 + 0.01 + 0.01 + 0.04 = 0.06.
  4. Interpretation: a small RSS means the 2 cm per kilogram model fits these readings closely. The largest single miss is 0.2 cm.

Try it: a model predicts 3 cm per kilogram for the same spring, so its predictions are 3, 6, 9 and 12 cm. The observed stretches are 2.0, 4.1, 5.9 and 8.2 cm. Find RSS.

Answer: the residuals are -1.0, -1.9, -3.1 and -3.8. Their squares are 1.00, 3.61, 9.61 and 14.44. RSS = 1.00 + 3.61 + 9.61 + 14.44 = 28.66. The much larger value shows the 3 cm model fits far worse than the 2 cm model.

Common mistakes and tradeoffs

It is tempting to add the residuals directly to measure the fit. In fact the positive and negative misses cancel, and a line can have a total residual of zero while missing every point badly. Squaring fixes this.

It is tempting to think a smaller RSS always means a better model. In fact adding predictors never increases RSS on the training data, so RSS alone will always favour the bigger model. To compare models with different numbers of predictors, use a penalty such as Mallows Cp or a test such as the F-Test.

It is tempting to compare RSS across data sets of different sizes. In fact RSS grows with the number of points, so use the average, Mean Squared Error, which is RSS divided by n, when the sample sizes differ.

When to use it: RSS is the default measure for least-squares fitting and for comparing nested linear models on the same data.

When not to use it: when the errors are very unequal in size or when outliers dominate, because squaring gives big misses extra weight. Robust measures such as the absolute error are alternatives. When the errors have different variances, Weighted Least Squares is the better tool.

History

In the late 1700s, astronomers and surveyors had many measurements that disagreed slightly, and they needed a way to combine them into one estimate. Adrien-Marie Legendre published the least-squares method in 1805. Carl Friedrich Gauss later said he had used it since about 1795, and in 1801 he used it to predict where the asteroid Ceres would reappear. The idea of minimising the sum of squared errors spread from astronomy into statistics. Its main appeal was that it reduced a set of disagreeing measurements to one line, with a simple score that could be computed by hand.

Recap

  • A residual is the observed value minus the predicted value.
  • Squaring the residuals removes the sign and gives large misses more weight.
  • RSS is the sum of the squared residuals, and least squares chooses the line that makes it smallest.
  • RSS always falls when you add predictors, so comparing models needs a penalty or a test.
  • Read next: F-Test uses the drop in RSS to test whether a group of predictors helps; Mallows Cp and Forward Stepwise Selection use RSS to choose a model.

Check yourself

QuestionIn one sentence, what does the residual sum of squares measure?Show answer
It measures the total squared gap between each observed value and its prediction, so a smaller RSS means the predictions are closer to the data.
QuestionA model predicts 5, 7 and 9 for three observations that are 6, 6 and 10. What is the RSS?Show answer
The residuals are 6 minus 5 = 1, 6 minus 7 = -1, and 10 minus 9 = 1. Their squares are 1, 1 and 1. RSS = 1 + 1 + 1 = 3.
QuestionWhy do we square the residuals instead of adding them directly?Show answer
Adding them directly lets positive and negative residuals cancel, so a poor line can look good. Squaring makes every residual non-negative, so the total grows with every miss, and it gives large misses more weight.
QuestionYou add a new predictor to a regression and the RSS falls a little. Does this prove the new predictor is useful?Show answer
No. RSS on the training data never rises when a predictor is added, even a useless one. You need a test or a penalty, such as the F-Test or Mallows Cp, to decide whether the drop is larger than chance would give.

Sources