Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 28 ยท Diagnostics

Residual Diagnostics

Check linearity, constant variance, normality and independence of OLS residuals in Statsmodels with plots and tests: Breusch-Pagan, Jarque-Bera, Q-Q plots, RESET and Durbin-Watson.

A regression always produces numbers, even from a badly chosen model. Diagnostics tell you whether the model's assumptions are believable, and so whether its standard errors and p-values deserve trust. Nearly every diagnostic studies the residuals, the leftovers after the model has done its best.

The classic OLS assumptions are:

  1. Linearity: the mean of y is a linear function of the coefficients.
  2. Constant variance (homoscedasticity): residual spread does not depend on x or on fitted values.
  3. Independence: residuals are not correlated with each other.
  4. Normality (mainly for small samples): residuals are roughly bell shaped.
flowchart LR
    A["Fit model"] --> B["Residuals vs fitted"]
    B -->|"curve"| C["Add terms / transform"]
    B -->|"funnel"| D["Robust SE, WLS or log y"]
    A --> E["Q-Q plot"]
    E -->|"heavy tails"| F["Check outliers, robust fit"]
    A --> G["Order / time plot"]
    G -->|"patterns"| H["HAC, GLSAR, time-series model"]

Each picture points at the assumption it tests and the usual remedy.

Tests with a simulated problem

We build one dataset that breaks assumptions on purpose: the true relation is curved (a linear fit is wrong) and the noise grows with x. Then we run the standard tests.

Python Four standard tests

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.

Read tests as alarms, not verdicts. A small p-value says "this assumption looks violated"; large samples flag trivial violations, small samples miss serious ones. Always look at pictures too.

Assumption Test Small p-value means
Constant variance het_breuschpagan, het_white Variance depends on predictors
Normality jarque_bera, omni_normtest Residuals are not normal
Independence durbin_watson, acorr_breusch_godfrey Autocorrelation
Linearity linear_reset, linear_harvey_collier Functional form is wrong

Plots

Three pictures cover most of what you need: residuals versus fitted values (curve means non-linearity, funnel means changing variance), a Q-Q plot (points should hug the line if residuals are normal) and a histogram.

Python Residual plots

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.

You should see a U-shaped pattern (the missing curve) and a funnel that widens to the right (variance grows).

Fixing what you find

Match the remedy to the symptom:

Python Does the fix help?

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.

Adding the squared term repairs the shape; taking logs of y tames the growing variance (compare the Breusch-Pagan p-values).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A residuals-vs-fitted plot shows a funnel that widens to the right. What does it suggest?

    A widening spread means the error variance grows with the fitted values.
  2. 2. In a Q-Q plot of residuals, points curving away from the line at the ends suggest what?

    Q-Q plots compare residual quantiles with normal quantiles; tails that bend away show non-normality.
  3. 3. Which test checks for a wrong functional form?

    RESET adds powers of the fitted values; if they help, the linear form is probably missing something.
  4. 4. Why should you not rely on test p-values alone?

    Statistical power depends on sample size, so combine tests with visual checks and judgment.

Summary

  • Diagnostics examine residuals for linearity, constant variance, independence and normality.
  • Use residual-vs-fitted, Q-Q and histogram plots, plus Breusch-Pagan, Jarque-Bera, Durbin-Watson and RESET.
  • Each symptom has a standard remedy: new terms, transformation, WLS or robust errors, time-series models.
  • Treat tests as alarms and re-check after every fix.

Next: Influence and Outliers