liveBook · Manning

Residual error

As explained in Grokking Statistics

A residual is the difference between an observed response value and the value predicted by a fitted model.

A residual is the gap between an observed response and the value predicted by a fitted line. Subtract the prediction from the actual value: positive residuals sit above the line, and negative residuals sit below it. section 7.9

Residuals help quantify how closely a regression fits its data:

  • Square each residual and add the results to obtain the sum of squares, a loss measure for the line. section 7.9
  • Smaller overall residuals indicate points closer to the fitted line; larger residuals indicate a looser fit. section 7.9
  • Use residual calculations alongside visualizations and other checks rather than treating one score as proof of a good model. section 7.11 section 7.7

Sports drink sales

For each temperature, calculate predicted_y = m * x + b, then add (actual_y - predicted_y) ** 2 to a running total. The resulting sum of squares measures the line's total squared error.

import numpy as np

x = np.array([51.5, 55.1, 63.0, 71.2, 73.0, 77.1, 
    88.1, 91.2, 93.0, 101.6])

y = np.array([100.98, 137.44, 237.8, 254.96, 263.45, 
    353.89, 450.32, 521.34, 503.13, 573.91])

m = 9.74  #1
b = -405.26  #1

sum_of_squares = 0  #2
for i in range(len(x)):  #2
    actual_y = y[i]
    predicted_y = m * x[i] + b
    sum_of_squares += (actual_y - predicted_y) ** 2

print(f"Sum of squares: {sum_of_squares:.2f}")  #3

Inspect the short NumPy loop that computes each residual, squares it, and accumulates the regression loss. section 7.9

How Grokking Statistics covers it

How deep it goes
Defines residuals, explains their signs, squares them into loss, and stops short of detailed residual diagnostics.
Examples
Uses temperature and sports drink sales, plotted data points, fitted lines, and Python sum-of-squares calculations.
What it assumes
Teaches predicted and observed values from zero, while assuming familiarity with a fitted linear regression line.
What you can do after
Diagnose regression fit by calculating residuals, summing their squares, and interpreting the result with visual checks.
How it connects
Connects residuals to linear regression, predicted values, sum of squares, RMSE, R2, and model evaluation.

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 Grokking Statistics. The section links point to the parts of the book it draws on.