Residuals for mosaic plots: definitions and when to use them

Purpose

A shaded mosaic plot colours each tile according to how far the observed count of the cell is from the count expected under a model, usually the model of (mutual) independence. The measure of distance is a residual. vcd::mosaic() uses Pearson residuals; vcdinteractive computes six kinds of residuals, lets you switch among them in the interactive plot, and uses them to test each cell.

This vignette gives the definition of each residual, its properties and the situations in which it is the appropriate choice.

Notation

For a contingency table with \(N\) cells, let, for cell \(i\),

All quantities are computed by mosaic_cells():

cells <- mosaic_cells(HairEyeColor)
head(cells[, c("Hair", "Eye", "Sex", "observed", "expected", "leverage",
               residual_types)])
#>    Hair   Eye  Sex observed expected leverage pearson standardized deviance
#> 1 Black Brown Male       32     18.9    0.265   3.009        3.509    2.735
#> 2 Brown Brown Male       53     50.1    0.413   0.411        0.537    0.407
#> 3   Red Brown Male       10     12.4    0.234  -0.690       -0.789   -0.715
#> 4 Blond Brown Male        3     22.2    0.281  -4.080       -4.811   -5.144
#> 5 Black  Blue Male       11     18.5    0.261  -1.741       -2.025   -1.884
#> 6 Brown  Blue Male       50     49.0    0.409   0.150        0.195    0.149
#>   freeman_tukey anscombe robust
#> 1         2.646    2.739  3.253
#> 2         0.438    0.407 -0.783
#> 3        -0.644   -0.715 -1.235
#> 4        -5.753   -5.214 -3.417
#> 5        -1.876   -1.887 -0.574
#> 6         0.184    0.149  1.146

The residuals

Pearson residuals ("pearson")

\[r_i^{P} = \frac{o_i - e_i}{\sqrt{e_i}}, \qquad \sum_i \left(r_i^{P}\right)^2 = X^2 .\]

When to use them. For describing the lack of fit and for comparability with vcd and with the published literature. Not the best choice for deciding which cells are significant.

Adjusted standardized residuals ("standardized")

\[r_i^{S} = \frac{o_i - e_i}{\sqrt{e_i (1 - h_i)}} = \frac{r_i^{P}}{\sqrt{1 - h_i}} .\]

In a two-way table under independence this is Haberman’s (1973) adjusted residual, \[r_{jk}^{S} = \frac{o_{jk} - e_{jk}}{\sqrt{e_{jk} (1 - p_{j+}) (1 - p_{+k})}} ,\] with \(p_{j+}\) and \(p_{+k}\) the marginal proportions.

When to use them. For testing single cells and for the list of significant cells: this is the default of imosaic() and significant_cells(), and the residual recommended by Agresti (2013).

Deviance residuals ("deviance")

\[r_i^{D} = \operatorname{sign}(o_i - e_i)\,\sqrt{2\left[o_i \log\frac{o_i}{e_i} - (o_i - e_i)\right]}, \qquad \sum_i \left(r_i^{D}\right)^2 = G^2 ,\] with \(o_i \log(o_i/e_i) = 0\) when \(o_i = 0\), so that an empty cell has \(r_i^{D} = -\sqrt{2 e_i}\).

When to use them. When the analysis is based on \(G^2\) (model selection by likelihood-ratio tests, e.g. with vcdExtra::seq_loglm()), so that the shading agrees with the statistic being reported.

Freeman–Tukey residuals ("freeman_tukey")

\[r_i^{FT} = \sqrt{o_i} + \sqrt{o_i + 1} - \sqrt{4 e_i + 1} .\]

When to use them. With sparse tables or small expected counts (say \(e_i < 5\)), where Pearson residuals are unstable and dominated by cells with tiny \(e_i\).

Anscombe residuals ("anscombe")

\[r_i^{A} = \frac{3}{2}\,\frac{o_i^{2/3} - e_i^{2/3}}{e_i^{1/6}} .\]

When to use them. When the normality of the residuals matters, e.g. for normal probability plots of the residuals, or with small and skewed counts.

Robust residuals ("robust")

\[r_i^{R} = \frac{o_i - \tilde e_i}{\sqrt{\tilde e_i}} ,\] where \(\tilde e_i\) are the fitted values of the same log-linear model estimated robustly:

Why it matters: the classical fitted values are computed from the margins, which also contain the outlying cells. A single very large cell inflates the expected counts of its whole row and column. Its own residual is then reduced (masking), and the other cells of the same row and column get spuriously negative residuals (swamping). A robust fit is not pulled towards the outlying cells, so they stand out and the other cells are left alone (Kuhnt, 2004).

When to use them. To find outlying cells, i.e. when you expect the model to hold for most of the table except for a few cells. They are not meaningful when the model is wrong almost everywhere (e.g. mutual independence for Titanic): then the robust fit follows the largest group of cells and the robust residuals become huge elsewhere. The p-values of the robust residuals ignore leverage and the variability of the robust fit and are only indicative.

Example: masking and swamping

A \(5 \times 5\) table of independent Poisson counts, with one planted outlier in cell (b, C):

set.seed(1)
tab <- as.table(matrix(rpois(25, 40), 5,
                dimnames = list(A = letters[1:5], B = LETTERS[1:5])))
tab[2, 3] <- 400
cells <- mosaic_cells(tab)
# row b and column C, which contain the outlier
sub <- cells[cells$A == "b" | cells$B == "C",
             c("A", "B", "observed", "expected", "expected_robust",
               "pearson", "standardized", "robust")]
format(sub, digits = 2)
#>    A B observed expected expected_robust pearson standardized robust
#> 2  b A       48       83              44    -3.8         -5.4  0.655
#> 7  b B       44       88              46    -4.7         -6.6 -0.360
#> 11 a C       42       82              41    -4.5         -6.2  0.117
#> 12 b C      400      226              45    11.6         19.4 52.957
#> 13 c C       38       86              43    -5.2         -7.3 -0.769
#> 14 d C       37       83              42    -5.1         -7.1 -0.720
#> 15 e C       39       79              39    -4.5         -6.2 -0.072
#> 17 b D       45       90              48    -4.7         -6.7 -0.366
#> 22 b E       27       77              42    -5.7         -8.0 -2.352

With the classical fit the expected count of the outlier is far above the 40 of the other cells. The outlier’s residual is reduced, and every other cell of row b and column C looks under-represented. With the robust fit only the outlier stands out.

Testing cells

For every type except "robust", the p-value of a cell is \[p_i = 2\,\Phi\!\left(-\frac{|r_i|}{\sqrt{1 - h_i}}\right),\] i.e. the residual is standardized by its leverage before being compared with \(N(0,1)\). For Pearson residuals this gives the same p-value as the adjusted standardized residual. For robust residuals \(p_i = 2\,\Phi(-|r_i^{R}|)\).

Testing all \(N\) cells is a multiple-testing problem. significant_cells() and the interactive plot adjust the p-values with stats::p.adjust():

significant_cells(HairEyeColor, type = "standardized", adjust = "holm")
#>    Hair   Eye    Sex observed expected  residual      p_value   p_adjusted
#> 1 Blond  Blue Female       64 24.38614  9.594597 8.424580e-22 2.695866e-20
#> 2 Blond Brown Female        4 24.95326 -5.032668 4.837014e-07 1.499474e-05
#> 3 Blond Brown   Male        3 22.24268 -4.811205 1.500234e-06 4.500702e-05
#> 4 Black Brown Female       36 21.22010  3.805191 1.416950e-04 4.109155e-03
#> 5 Brown  Blue Female       34 54.91682 -3.762966 1.679098e-04 4.701473e-03
#> 6 Black Brown   Male       32 18.91504  3.509362 4.491827e-04 1.212793e-02
#>   direction
#> 1      over
#> 2     under
#> 3     under
#> 4      over
#> 5     under
#> 6      over

Shading

As in vcd::shading_hcl(), tiles are coloured with blue hues for positive residuals (more observations than expected) and red hues for negative ones. Saturation steps up at \(|r| = 2\) and \(|r| = 4\) (argument breaks). For adjusted standardized, Freeman–Tukey and Anscombe residuals these cut-offs correspond roughly to cell-wise two-sided levels of 5% and 0.01%. For Pearson residuals they are conservative, as explained above. The black outline marks the cells that are significant after the multiplicity correction, independently of the colour.

Choosing a residual: summary

Residual Formula Sum of squares Use it for
Pearson \((o-e)/\sqrt e\) \(X^2\) describing lack of fit; comparability with vcd
Adjusted standardized \((o-e)/\sqrt{e(1-h)}\) – testing single cells (default)
Deviance \(\pm\sqrt{2[o\log(o/e)-(o-e)]}\) \(G^2\) analyses based on likelihood-ratio tests
Freeman–Tukey \(\sqrt o+\sqrt{o+1}-\sqrt{4e+1}\) FT statistic sparse tables, small expected counts
Anscombe \(\tfrac32(o^{2/3}-e^{2/3})/e^{1/6}\) – near-normal residuals, skewed small counts
Robust \((o-\tilde e)/\sqrt{\tilde e}\) – outlying cells, masking and swamping

In the interactive plot

imosaic() receives all the residuals and their p-values at once. In the side panel:

imosaic(HairEyeColor, residuals = "standardized", height = 560)

References