Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 23

Deviance of GLMs: Calculation and Interpretation

Recommended resources

Hello! Welcome to the sixth lesson in our intensive midterm preparation.

In the last lesson, we walked through the mechanics of fitting a GLM using the Iteratively Reweighted Least Squares (IRWLS) algorithm. We now know how a model's parameters are estimated. But that raises a critical question: once we have our fitted model, how do we know if it's any good?

Today, we will answer that question by exploring deviance. Your goal for this lesson is to learn how to compute the deviance of a fitted GLM and interpret it as a measure of model fit. Deviance is the fundamental tool for assessing performance in GLMs, playing a role analogous to the residual sum of squares (RSS) in ordinary linear regression.

1. What is Deviance?

In linear regression, we measure how well a model fits by summing the squared differences between the observed values () and the fitted values (). In GLMs, we use a more general concept based on likelihoods.

Deviance compares the log-likelihood of your fitted model to the log-likelihood of a "perfect" model, called the saturated model.

Here are the three models we use as benchmarks:

  1. Saturated Model: A model that fits the data perfectly. It has as many parameters as there are data points, so each fitted value is exactly equal to the observed value . This represents the best possible fit. Let's call its log-likelihood .
  2. Proposed Model: This is your GLM, with parameters (). It generates fitted values . Its maximized log-likelihood is .
  3. Null Model: The simplest possible model, containing only an intercept (). It assumes the predictors have no effect. Its maximized log-likelihood is .

Using these, we define two key quantities:

  • Residual Deviance: This measures the "badness-of-fit" of your proposed model compared to the perfect saturated model.

    A smaller residual deviance means your model is closer to a perfect fit.

  • Null Deviance: This measures the "badness-of-fit" of the intercept-only null model.

    The null deviance serves as a baseline for how well you can predict the response without using any predictors.


2. Calculating Deviance: A Concrete Example

The definitions above can seem abstract. The best way to understand them is to see them calculated step-by-step. The following video provides an excellent walkthrough for a simple Poisson regression model.

Poisson regression - How to calculate likelihood and deviance

Watch this video from TileStats to see how the log-likelihood is calculated for the saturated, null, and proposed models, and how these are combined to compute the null and residual deviance.

Follow the video from 02:08 to 13:06. The key steps are: (03:13 - 09:27) Calculating the log-likelihood for the proposed model. (09:27 - 11:01) Calculating the log-likelihood for the null model. (11:01 - 12:19) Calculating the log-likelihood for the saturated model. (12:19 - 13:06) Plugging these into the deviance formulas.\nFocus on the process of using the appropriate mean (µ) for each model to find the log-likelihood.

As you saw in the video, the process involves:

  1. For each model (proposed, null, saturated), determine the mean prediction for each data point .
  2. Use the probability mass/density function (e.g., Poisson PMF) to find the probability of observing each given its corresponding .
  3. Sum the logarithms of these probabilities to get the log-likelihood for that model.
  4. Finally, plug the log-likelihoods into the deviance formulas.

3. Interpreting Deviance as a Measure of Fit

Now that we can compute deviance, how do we use it?

Analogy to R-squared

The null deviance represents the total variation in the data (like Total Sum of Squares in linear regression), and the residual deviance represents the unexplained variation (like Residual Sum of Squares).

We can construct a "pseudo R-squared" to describe the proportion of deviance explained by the predictors in our model:

A higher suggests your model's predictors are explaining more of the variation in the response.

Goodness-of-Fit Test

The residual deviance can be used to formally test the goodness-of-fit of your model.

  • Null Hypothesis (): Your proposed model fits the data adequately.
  • Test Statistic: The residual deviance, .
  • Distribution: If is true and the sample size is large enough, follows a chi-squared distribution with degrees of freedom, where is the number of observations and is the number of parameters in your model.

You calculate the deviance, find the p-value from the distribution, and if (e.g., 0.05), you reject . This would mean there is significant evidence that your model does not fit the data well.

Goodness of Fit Statistics for Poisson Regression, Gabriele Durrant (part 2 of 3)

The following video from the National Centre for Research Methods discusses using the deviance (referred to as the log-likelihood ratio test statistic) for a goodness-of-fit test.

Watch from 07:04 to 09:31. This clip explains how to use the deviance statistic with a chi-square distribution to test for model fit and importantly, notes the limitation that the approximation is poor if expected cell counts are small (e.g., less than 5).


4. Practical Shortcuts and Considerations

Manually calculating log-likelihoods is tedious. For exams and real-world analysis, you'll rely on simplified formulas and software output.

Deviance Formulas for Common Distributions

The general deviance formula simplifies for specific distributions. Memorizing these can be a huge advantage on an exam.

[PDF] GLM I: Introduction to Generalized Linear Models

These presentation slides contain a very useful summary of the deviance formulas for several common distributions.

Go to slide 24, titled 'Deviance'. It presents a table with the individual contribution to deviance for Normal, Poisson, Gamma, and Inverse Gaussian distributions. Slide 39 repeats this table for emphasis. This is a key slide to remember.

Here's a summary of those key formulas, where :

Distribution Contribution to Deviance () Key Insight
Normal This is just the squared residual! The total deviance is the Residual Sum of Squares (RSS).
Poisson This is the standard for count data. If the model includes an intercept, , and the second term sums to zero.
Gamma Used for skewed, positive continuous data.

Reading Software Output

In practice, you'll get deviance values from software output. It's crucial to know what you're looking at. The slides from LINK show several examples. Notice in the R output on slide 25 (Gamma Log GLM–Intercept and Slope) how the summary includes:

  • Null deviance: 252.05 on 199 degrees of freedom
  • Residual deviance: 206.47 on 198 degrees of freedom

This tells you the deviance of the intercept-only model and your fitted model, along with their respective degrees of freedom.

GLM Output Example

This R output for a Gamma GLM shows the Null deviance (for a model with only an intercept) and the Residual deviance (for the model including the settle.delay predictor). The reduction in deviance from 252.05 to 206.47 indicates the predictor is improving the model fit.

Scaled vs. Unscaled Deviance

There's one final, critical detail: the dispersion parameter .

  • For Poisson and Binomial models, is fixed at 1. The deviance we've calculated is what we use.
  • For Normal, Gamma, and other distributions, is not 1 and must be estimated from the data.

This leads to two types of deviance:

  • Unscaled Deviance: The value calculated from the formulas above. This is what R and other software packages typically report as "deviance."
  • Scaled Deviance: . This is the quantity that is actually used for hypothesis testing (i.e., the value you compare to a distribution).

[PDF] TMA4315 Generalized linear models H2018 - NTNU

The distinction between scaled and unscaled deviance is often a point of confusion. This document explains it clearly.

Read the section titled 'Scaled and unscaled deviance' on page 39. It explicitly states that R reports the unscaled deviance and shows how the scaled deviance is calculated for different models. This is an essential detail for correctly interpreting software output.


Conclusion

In this lesson, we demystified deviance, the primary tool for assessing fit in GLMs. You learned how to compute it from log-likelihoods and how to interpret it for model assessment.

Key Takeaways:

  • Deviance measures the discrepancy between your model and a perfect (saturated) model. Lower is better.
  • Residual Deviance measures your model's fit, while Null Deviance provides a baseline for a model with no predictors.
  • The reduction in deviance from null to residual acts like an statistic, showing the explanatory power of your predictors.
  • For a goodness-of-fit test, the scaled deviance () is compared to a distribution with degrees of freedom.
  • Remember that for Poisson and Binomial models, , so scaled and unscaled deviance are the same.

You've learned how to evaluate a single model. But what if you have two different models and want to know which one is statistically better? In our next lesson, we will use deviance to do just that by performing a Likelihood Ratio Test (LRT) to compare nested GLMs.