01 / 21

Deviance &
Goodness-of-Fit

Understand how deviance measures model quality, compare nested models using likelihood ratios, and assess fit with the chi-squared distribution.

Statistics

What Is Deviance?

Deviance is a measure of how well a statistical model fits the observed data. It is based on the likelihood function and is defined as negative two times the log-likelihood of the fitted model:

Think of deviance as the "distance" between what your model predicts and what the data actually show. It generalizes the residual sum of squares from linear regression to any likelihood-based model.

The Saturated Model Baseline

Saturated Model
Has as many parameters as data points. It fits the data perfectly, achieving the maximum possible likelihood. Its scaled deviance is 0.
Fitted Model
Uses fewer parameters (parsimony). \( L(\hat{\theta}) \le L_s \). Deviance measures how much worse it fits compared to the saturated model.

Why Deviance Matters

1
Unified framework: Works for GLMs (logistic, Poisson, gamma) where \( R^2 \) and residual sums of squares do not apply directly.
2
Model comparison: The difference in deviance between nested models follows a \( \chi^2 \) distribution, enabling formal hypothesis tests.
3
Diagnostics: Individual deviance residuals pinpoint which observations contribute most to poor fit.
4
Connection to AIC: \( \text{AIC} = D + 2p \), where \( p \) = number of parameters. Deviance is the engine behind information criteria.

Core Formula: Scaled Deviance

$$D = -2 \sum_{i=1}^{n} \left[ y_i \ln\!\left(\frac{\hat{\mu}_i}{y_i}\right) + (n_i - y_i)\ln\!\left(\frac{n_i - \hat{\mu}_i}{n_i - y_i}\right) \right]$$
Binomial deviance example (logistic regression)

The \( \chi^2 \) Reference Distribution

Deviance vs. Pearson \( \chi^2 \)

Deviance \( D \) Pearson \( X^2 \)
Formula \( -2 \ln \frac{L(\hat{\theta})}{L_s} \) \( \sum \frac{(O_i - E_i)^2}{E_i} \)
Basis Likelihood ratio Squared residuals
Decomposition Additive in deviance residuals Additive in Pearson residuals
Null dist. \( \chi^2_{n-p} \) (approx.) \( \chi^2_{n-p} \) (approx.)
Preferred when Comparing nested models Checking individual cell fit

The Likelihood Ratio Test

To compare a reduced model (null, \( p_0 \) parameters) against a full model (alternative, \( p_1 \) parameters), compute the deviance difference:

Reduced Model
Deviance = \( D_0 \)
Full Model
Deviance = \( D_1 \)
\( \Delta D = D_0 - D_1 \)
\( \text{df} = p_1 - p_0 \)

How to Apply the Deviance Test

1
Fit the reduced model (null hypothesis) and record its deviance \( D_0 \) with \( p_0 \) parameters.
2
Fit the full model (alternative) and record its deviance \( D_1 \) with \( p_1 \) parameters.
3
Compute \( \Delta D = D_0 - D_1 \) and degrees of freedom \( \text{df} = p_1 - p_0 \).
4
Compare \( \Delta D \) to \( \chi^2_{\alpha,\,\text{df}} \). If \( \Delta D > \chi^2_{\text{crit}} \), reject \( H_0 \): the extra terms improve the model.
5
Report the p-value: \( p = P(\chi^2_{\text{df}} \geq \Delta D) \). Software (R, Python) calculates this directly.

Example 1: Logistic Regression

A logistic regression predicting disease status (\( n = 200 \)) with age alone gives deviance \( D_0 = 245.3 \) (\( p_0 = 2 \)). Adding smoking status gives \( D_1 = 231.8 \) (\( p_1 = 3 \)).

Example 2: Poisson Regression

Modeling insect counts (\( n = 50 \) traps): an intercept-only Poisson model has \( D_0 = 78.6 \) (\( p_0 = 1 \)). Adding temperature and humidity gives \( D_1 = 52.1 \) (\( p_1 = 3 \)).

Example 3: Absolute Goodness-of-Fit

Unlike the likelihood ratio test (which compares two models), the absolute goodness-of-fit test checks whether a single model fits the data at all, using the residual deviance directly.

Deviance Residuals

The total deviance decomposes into contributions from each observation. The deviance residual for observation \( i \) is:

\( |d_i| < 2 \)
Observation is consistent with the model
\( 2 \le |d_i| < 3 \)
Observation is somewhat unusual: investigate
\( |d_i| \ge 3 \)
Likely outlier or evidence of poor fit

Detecting Overdispersion

When the residual deviance is much larger than its degrees of freedom, the variance in the data exceeds what the model assumes: this is overdispersion.

\( \hat{\phi} \approx 1 \)
Good fit: variance matches the model assumption
\( \hat{\phi} \gg 1 \)
Overdispersion: standard errors are too small, p-values too optimistic

Common Mistakes

✕ Using Deviance with Sparse Data
The \( \chi^2 \) approximation breaks down when expected counts are small (\( < 5 \)). Use exact tests or simulation instead.
✕ Comparing Non-Nested Models
The deviance difference test requires the reduced model to be a special case of the full model. For non-nested models, use AIC or BIC.
✕ Ignoring Overdispersion
If \( D / \text{df} \gg 1 \), the \( \chi^2 \) test is invalid. Adjust with a dispersion parameter or switch to a quasi-likelihood approach.

Summary of Key Quantities

Quantity Formula Used For
Deviance \( -2 \ln \frac{L(\hat{\theta})}{L_s} \) Absolute GOF test
Deviance difference \( \Delta D = D_0 - D_1 \) Likelihood ratio test
Pearson \( X^2 \) \( \sum \frac{(O - E)^2}{E} \) Cell-level GOF
Dispersion \( \hat{\phi} \) \( D / (n - p) \) Overdispersion check
AIC \( D + 2p \) Model selection

Question 1 of 5

A logistic regression model has deviance \( D_0 = 180.2 \) with 3 parameters, and adding a predictor gives \( D_1 = 172.6 \) with 4 parameters. What is the test statistic for the likelihood ratio test?

Question 2 of 5

Under the null hypothesis that a model fits well, what is the approximate distribution of the residual deviance \( D \)?

Question 3 of 5

A Poisson GLM gives residual deviance \( D = 142.7 \) on 60 degrees of freedom. What does the ratio \( D / \text{df} \approx 2.38 \) suggest?

Question 4 of 5

Which of the following is a valid use of the deviance difference test (\( \Delta D \))?

Question 5 of 5

If a model's AIC is defined as \( \text{AIC} = D + 2p \), what happens to the AIC when you add a parameter that reduces the deviance by exactly 2?