liveBook · Manning

Residual error

As explained in Statistics Every Programmer Needs

Residual error is the difference between an observed response and its value predicted by a fitted model.

A residual is an observed response minus the response predicted by a fitted model: residual = y − ŷ. In linear regression, it is the observation’s vertical difference from the fitted line. Small residuals indicate that predictions are close to observations, but a good-looking fit alone does not establish that the relationship is meaningful or that the model’s assumptions hold. section 4.1.1 section 4.2.4

One runner’s predicted time

For a runner whose observed Stage 2 time is 191 minutes and fitted value is 195.78 minutes, the residual is approximately −4.78 minutes: the model predicted too high. Squaring this residual gives about 22.88 square minutes, its contribution to the sum of squared errors. section 4.2.3

Across observations, the sum of squared errors (SSE) adds each squared residual: SSE = Σ(y − ŷ)². For the 20-runner regression, `np.sum(lm.resid ** 2)` gives about 21,099.16. SSE is the portion of total response variability not explained by the regression; it also connects to R² through R² = 1 − SSE/SST. Examine fit with other measures rather than relying on one total. section 4.2.3 section 4.3

>>> plt.scatter(lm.fittedvalues, lm.resid)    #1
>>> plt.axhline(y = 0, color = 'red', 
>>>             linestyle = '--')     #2
>>> plt.title('Residuals Plot')     #3
>>> plt.xlabel('Fitted Values')     #4
>>> plt.ylabel('Residuals')   #5
>>> plt.show()    #6

Plot `lm.fittedvalues` against `lm.resid`, with a horizontal zero line, to check whether unexplained errors show a visible pattern. section 4.2.4

Use the fitted residuals to check assumptions before interpreting the regression:

  • Linearity: look for points scattered around zero rather than an evident pattern. A high R² does not establish a linear relationship. section 4.2.4
  • Independence: examine whether errors are correlated; `durbin_watson(lm.resid)` is the demonstrated test. section 4.2.4
  • Homoscedasticity and normality: use a Breusch–Pagan test for the former, and a Q–Q plot or Jarque–Bera test for the latter. These checks concern fitted residuals, not merely the original variables. section 4.2.4 section 4.2.1

A large residual can flag an unusual observation with substantial influence on a fitted line. Investigate it before changing the data: removing an apparent outlier may improve the observed R² while reducing generalizability to new observations. section 4.1.3

How Statistics Every Programmer Needs covers it

How deep it goes
Works from observed-minus-predicted differences through squared-error calculation and plotted and statistical diagnostics, without developing proofs.
Examples
Calculates a runner’s squared prediction error and plots fitted values against residuals in Python.
What it assumes
Assumes basic predictor, response, and fitted-value concepts while defining residuals as observed-versus-predicted differences.
What you can do after
Diagnose/debug with residual plots and tests, and calculate SSE from a fitted regression’s residuals.
How it connects
Connects residuals to linear regression, SSE, R-squared, outliers, linearity, independence, homoscedasticity, and normality.

Also explained in

Compare how 11 books explain residual error →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.