Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 38 ยท Regression and Matrices

Residual Plots and Checking Model Fit

Use residplot to see what a regression line misses: spot curvature, uneven spread and outliers, and compare linear, polynomial and lowess fits.

A regression line can look convincing and still be wrong. The residuals, the gaps between each point and the line, tell you what the line missed. If the line captured the real pattern, the residuals look like random noise around zero. If they show a curve, a funnel or a few giant misses, the model needs work.

Seaborn draws this check for you with residplot(). In this lesson you read residual plots, learn the three shapes to look for, and fix a bad fit.

What a residual plot shows

For each observation the residual is actual - predicted. A residual plot puts the predictor on the x axis and the residual on the y axis, with a reference line at zero. Good fits look like a shapeless cloud centred on that line.

Reading a residual plot

Random cloud The model captured the pattern.
A curve The true relation bends. Add a term.
A funnel Spread changes with x. Try a transform.
Far-away dots Outliers pull the line.

Check residuals before you trust any fitted line.

1 / 2

residplot in action

residplot() fits a linear model of y on x and plots what is left. Here the truth is a parabola, so a straight line is the wrong model, and the residual plot gives it away:

Python Residuals of a wrong linear fit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The left panel hides the problem. The right panel shows residuals that are positive at both ends and negative in the middle: a clear U. That is the signature of a missing squared term.

Fixing the fit and re-checking

residplot() takes order just like regplot(). Raise it and see whether the pattern disappears. The next example computes the spread of residuals numerically too, so you can compare without squinting:

Python Linear versus quadratic residuals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The residual standard deviation falls sharply with order=2 and the picture becomes a flat, shapeless cloud. Add lowess=True to draw a smoother through the residuals: a flat line at zero is the goal, a bent one confirms a pattern.

lowess=True and robust=True need statsmodels. In the runner add "statsmodels" to the micropip.install list, as in regression plots.

Seeing a funnel

When the spread grows with x, the data is heteroscedastic. Ordinary least squares still draws a line, but its confidence band is untrustworthy. A residual plot shows the funnel immediately, and a log transform often repairs it:

Python Funnel residuals and a log fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Outliers and robust fits

One extreme point can tilt an ordinary fit. robust=True down-weights such points. Compare the lines:

Python Outliers and robust regression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A robust fit is not a licence to ignore outliers. Look at them first: they may be typos, or they may be the most interesting rows you have.

A short checklist

  1. Fit the line with regplot().
  2. Plot residplot() for the same x and y.
  3. Curve in the residuals: raise order or transform x.
  4. Funnel: transform y, or report that the band is only approximate.
  5. Outliers: investigate, then decide whether robust=True is appropriate.

Residual plots only judge the fit for one predictor at a time. With many predictors, use a statistics library such as statsmodels for full diagnostics and keep seaborn for the pictures.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A residual plot shows a clear U shape. What does that suggest?

    Systematic curvature in the residuals means the straight line missed a bend in the relationship.
  2. 2. What should the residuals of a good fit look like?

    Residuals should look like noise: no pattern, centred on zero. A perfectly flat line would mean a perfect fit, which real data rarely has.
  3. 3. The residuals fan out as x grows. A common first fix is to:

    A log transform often stabilises a spread that grows with the level.
  4. 4. Which argument makes regplot resist outliers?

    robust=True uses a robust linear model (it needs statsmodels).

Summary

  • A residual is actual minus predicted; residplot() plots them against x.
  • Curves mean a missing term, funnels mean uneven spread, far dots mean outliers.
  • order, lowess and robust work in residplot() as in regplot().
  • Check residuals before you describe a trend in a report.

Next: Heatmaps