Tutorials › Generalized Linear Models › Pearson and Deviance Residuals for Tables

Poisson Loglinear Models · Tutorial 456 of 981

Pearson and Deviance Residuals for Tables

Calculate and interpret cellwise Pearson and signed deviance residuals to locate patterns of lack of fit in contingency tables.

Intermediate 10 min read

What You'll Learn

  • Define Pearson and signed deviance residuals for observed and fitted cell counts
  • Calculate residuals when a cell has a zero observed count
  • Connect cellwise deviance contributions to the likelihood-ratio deviance
  • Relate signed deviance residuals to Pearson residuals for small relative discrepancies
  • Use residual signs and magnitudes to diagnose patterns without treating them as standalone tests

Why Look at Residuals Cell by Cell?

A goodness-of-fit statistic compresses discrepancies across a table into a single number. That number can indicate that a fitted loglinear model does not reproduce the observed counts well, but it does not tell us which cells contribute to the discrepancy. Cellwise residuals retain that information: their signs show whether a count is above or below its fitted value, and their magnitudes summarize how large the discrepancy is on a standardized scale.

Let \(Y_i\) be the observed count in cell \(i\), and let \(\hat{\mu}_i\) be its fitted mean under a Poisson loglinear model. The fitted mean is the model’s expected count for that cell. The residuals below compare \(Y_i\) with \(\hat{\mu}_i\); they are descriptive diagnostics, not by themselves a collection of formal cellwise hypothesis tests.

Definitions

Definition: For a cell with observed count \(y_i\) and positive fitted mean \(\hat{\mu}_i\), its Pearson residual is $$ r_{Pi}=\frac{y_i-\hat{\mu}_i}{\sqrt{\hat{\mu}_i}}. $$ Its signed deviance residual is $$ r_{Di}=\operatorname{sign}(y_i-\hat{\mu}_i)\sqrt{d_i}, \qquad d_i=2\left[y_i\log\left(\frac{y_i}{\hat{\mu}_i}\right)-(y_i-\hat{\mu}_i)\right]. $$ Here \(d_i\) is the cell’s deviance contribution. When \(y_i=0\), define \(y_i\log(y_i/\hat{\mu}_i)=0\), so \(d_i=2\hat{\mu}_i\). If \(y_i=\hat{\mu}_i\), both residuals are zero.

The Pearson residual divides the raw difference by the square root of the fitted mean, the Poisson standard deviation evaluated at that mean. The signed deviance residual uses the square root of the cell’s likelihood-ratio contribution and assigns the sign of the raw difference. Positive residuals mean the observed count exceeds its fitted count; negative residuals mean it falls short.

For a table, calculate these quantities separately for each cell. The overall Pearson statistic and likelihood-ratio deviance are sums of squared residuals:

$$ X^2=\sum_i r_{Pi}^2 =\sum_i\frac{(y_i-\hat{\mu}_i)^2}{\hat{\mu}_i}, \qquad G^2=\sum_i d_i=\sum_i r_{Di}^2. $$

These equalities do not mean that the two residuals are interchangeable cell by cell. Pearson residuals measure discrepancies relative to the fitted Poisson standard deviation; deviance residuals measure each cell’s contribution to the likelihood-ratio comparison with a saturated fit.

Why the Deviance Contribution Is Nonnegative

A cell’s deviance contribution cannot be negative. To see this, set \(u=y_i/\hat{\mu}_i\). For \(u>0\), define \(h(u)=u\log u-u+1\). Since \(h'(u)=\log u\), the function decreases up to \(u=1\) and increases after \(u=1\), where it equals zero. At \(u=0\), its continuous extension is \(h(0)=1\). Therefore \(h(u)\geq 0\), with equality only at \(u=1\). As \(d_i=2\hat{\mu}_i h(u)\), the contribution is nonnegative and is zero exactly when \(y_i=\hat{\mu}_i\).

Result: For independent Poisson cell counts with fitted means \(\hat{\mu}_i>0\), the likelihood-ratio deviance against the saturated model is the sum of the nonnegative cell contributions \(d_i\). The signed deviance residual squares to the corresponding contribution: \(r_{Di}^2=d_i\).

To verify the connection to the likelihood, the Poisson log-likelihood, apart from constants that do not depend on the means, is \(\ell(\boldsymbol{\mu})=\sum_i[y_i\log(\mu_i)-\mu_i]\). The saturated fit sets each mean to \(y_i\), with the boundary value zero allowed when the observed count is zero. Subtracting the fitted-model log-likelihood from the saturated log-likelihood and multiplying by two gives

$$ 2\{\ell_{\mathrm{sat}}-\ell_{\mathrm{fit}}\} =2\sum_i\left[y_i\log\left(\frac{y_i}{\hat{\mu}_i}\right)-(y_i-\hat{\mu}_i)\right] =\sum_i d_i. $$

For a zero observed count, the likelihood term \(y_i\log y_i\) is interpreted by its limit, zero, and the contribution reduces to \(2\hat{\mu}_i\). Thus the cellwise definition also agrees with the likelihood calculation at zero.

Worked Example: A Positive and a Zero Count

Suppose a Poisson model gives fitted mean \(\hat{\mu}=9\) for a cell with observed count \(y=12\). Its Pearson residual is $$ r_P=\frac{12-9}{\sqrt{9}}=1. $$ The deviance contribution and signed deviance residual are $$ d=2\left[12\log(12/9)-3\right] =2\left[12\log(4/3)-3\right]\approx 0.9044, \qquad r_D\approx \sqrt{0.9044}=0.9510. $$ Both residuals are positive because the observed count is above the fitted mean.

Now consider a different cell with \(y=0\) and \(\hat{\mu}=4\). By the zero-count convention, $$ r_P=\frac{0-4}{\sqrt{4}}=-2, \qquad d=2(4)=8, \qquad r_D=-\sqrt{8}\approx -2.8284. $$ The negative signs indicate that the observed count is below its fitted mean. The deviance calculation requires no logarithm of zero.

Local Relationship Between the Two Residuals

When the observed count is close to its fitted mean relative to that mean, the two residuals are approximately equal. This is a local approximation, not an identity for all counts. To derive it, write \(y=\mu+\delta\), where \(\mu>0\), and set \(t=\delta/\mu\). For \(t>-1\), the deviance contribution is

$$ d=2\mu\left[(1+t)\log(1+t)-t\right]. $$

Let \(g(t)=(1+t)\log(1+t)-t\). Then \(g(0)=g'(0)=0\), and \(g''(t)=1/(1+t)\), so \(g''(0)=1\). Taylor’s theorem at zero therefore gives \(g(t)=t^2/2+o(t^2)\) as \(t\to0\). Substitution yields

$$ d=\mu t^2+o(\mu t^2) =\frac{\delta^2}{\mu}+o\left(\frac{\delta^2}{\mu}\right). $$

The leading term is exactly the squared Pearson residual, \(\delta^2/\mu\). Taking square roots and restoring the sign shows that, as the relative discrepancy \(\delta/\mu\) tends to zero, the signed deviance residual is approximately \(\delta/\sqrt{\mu}\), the Pearson residual. Larger relative discrepancies can produce noticeably different magnitudes, especially when one count is near zero.

Worked Example: Residuals in a Two-Way Independence Table

Consider the observed counts below. The row totals are 20 and 40, the column totals are 30 and 30, and the grand total is 60. Under independence, the fitted count in a cell is its row total multiplied by its column total and divided by 60. The fitted table is therefore

Column 1Column 2Row total
Row 1: observed15520
Row 1: fitted101020
Row 2: observed152540
Row 2: fitted202040

For the first cell, \(y=15\) and \(\hat{\mu}=10\). Thus \(r_P=5/\sqrt{10}\approx1.5811\), and $$ d=2[15\log(15/10)-5]\approx2.163953, \qquad r_D\approx1.4710. $$ For the second cell, \(y=5\) and \(\hat{\mu}=10\). Thus \(r_P=-5/\sqrt{10}\approx-1.5811\), and $$ d=2[5\log(5/10)+5]\approx3.068528, \qquad r_D\approx-1.7517. $$ For the third cell, \(y=15\) and \(\hat{\mu}=20\). Thus \(r_P=-5/\sqrt{20}\approx-1.1180\), and $$ d=2[15\log(15/20)+5]\approx1.369538, \qquad r_D\approx-1.1703. $$ For the fourth cell, \(y=25\) and \(\hat{\mu}=20\). Thus \(r_P=5/\sqrt{20}\approx1.1180\), and $$ d=2[25\log(25/20)-5]\approx1.157178, \qquad r_D\approx1.0757. $$

The Pearson statistic is \(2.5+2.5+1.25+1.25=7.5\). Summing the unrounded deviance contributions gives \(G^2\approx7.759197\). The signs show that the first and fourth cells exceed their fitted counts, while the second and third fall short. The second cell has the largest absolute deviance residual here, so it makes the largest individual contribution to the deviance.

Worked Example: Equal Pearson Magnitudes Need Not Mean Equal Deviance Magnitudes

Suppose two independent Poisson counts have known fitted means of 4. Compare an observed count of 0 in the first cell with an observed count of 8 in the second. For the first cell, $$ r_P=(0-4)/\sqrt{4}=-2,\qquad d=2(4)=8,\qquad r_D=-\sqrt{8}\approx-2.8284. $$ For the second cell, $$ r_P=(8-4)/\sqrt{4}=2, \qquad d=2[8\log(8/4)-4] =2[8\log 2-4]\approx3.0904, \qquad r_D\approx1.7580. $$

The two Pearson residuals have equal absolute magnitude, but their signed deviance residuals do not. The deviance measure responds asymmetrically to discrepancies below and above a mean, particularly when the count is far from that mean. The example is a reminder to calculate the chosen residual rather than infer its value from another residual’s magnitude.

Using Residual Patterns Responsibly

A residual table is most useful when read in context. First identify which cells have the largest absolute residuals; then note whether those cells are above or below their fitted counts. A coherent pattern across cells can reveal where the model’s fitted structure is inadequate. For example, in the independence-table calculation, the directions of the discrepancies indicate which combinations occur more or less often than the fitted counts imply.

Residual signs and magnitudes should not be mistaken for formal, independent cellwise tests. In a fitted table, the model estimates parameters using the observed counts, and the fitted counts can be linked by constraints such as the table margins. Consequently, the cell residuals need not behave as independent standard normal variables. A large residual flags a cell for investigation; it does not, on its own, establish statistical significance or identify a cause.

A further practical distinction is that the deviance residual is signed, while the deviance contribution is not: \(d_i=r_{Di}^2\). A cell with a large negative residual contributes just as much to the total deviance as a cell with a positive residual of the same magnitude. Keeping both the sign and the squared contribution in view separates direction from overall lack of fit.

Common mistake: Treating a Pearson residual and a signed deviance residual as identical because both compare observed with fitted counts. They use different scales: \(r_{Pi}=(y_i-\hat{\mu}_i)/\sqrt{\hat{\mu}_i}\), whereas \(r_{Di}\) is the signed square root of a likelihood-ratio contribution. They are approximately equal for small relative discrepancies, but can differ substantially otherwise. Calculate each from its own definition and do not interpret either as an automatic cellwise test.

Check Your Understanding

Use the definitions and reasoning above to answer the following.

  1. For a cell with observed count 18 and fitted mean 12, calculate its Pearson residual and write the expression for its signed deviance residual.
  2. What are the deviance contribution and signed deviance residual when the observed count is zero and the fitted mean is 3?
  3. Why is a cell’s deviance contribution always nonnegative, and when is it zero?
  4. In a table, what does a negative signed residual say about the observed count relative to its fitted count?
  5. Why should a large cellwise residual be treated as a diagnostic flag rather than, by itself, a formal significance test?
Clinical Biostats