Skip to contents

Fits a loglinear model to a contingency table by iterative proportional fitting (IPF), as stats::loglin() does, but keeps the fitted table after every step, where one step rescales the fit to match one of the model's margins. The steps can be drawn as expected-area mosaics (geom_mosaic(expected = <table>, area = "expected")) to show the fit developing: in the "pressure" model of Friendly (1995), each step frees one set of partitions and lets that margin come to balance, and G² (the energy needed to hold the partitions) falls.

Usage

ipf_steps(table, formula, eps = 0.1, iter = 20L)

Arguments

table

The data, in any of the three forms of frequency data: a contingency table (a table or array with named dimnames, e.g. HairEyeColor); a data frame in frequency form, with one row per cell and a column of counts (e.g. as.data.frame(HairEyeColor)); or a data frame in case form, with one row per observation. In a data frame, the counts are the column named on the left of formula (Freq ~ Hair + Eye), otherwise a column Freq if there is one; without either, each row counts once. All other columns are the table's variables, so select the ones wanted first.

formula

The loglinear model, as for MASS::loglm(): variables by name or by position (~ Hair*Eye + Sex or ~ 1*2 + 3). A left-hand side only names the counts in a data frame, and is otherwise ignored. Alternatively, a list of margins, each a character vector of variable names, as for the margin argument of stats::loglin(). An empty list() is the equal-probability model: the starting table, with no steps.

eps, iter

Convergence, as in stats::loglin(): stop after the first cycle (one step for each margin) in which no fitted margin differed from the observed margin by more than eps, or after iter cycles.

Value

An object of class "ipf_steps", a list with:

steps

A data frame with one row per step: step (0 for the start), cycle, margin (the margin just fitted, e.g. "Hair:Eye"), G2 and X2 (likelihood-ratio and Pearson chi-square of the fit at that step), and deviation (the largest difference between the fitted and observed margin before the step).

fits

A list of fitted tables, fits[[k + 1]] for step k.

margins

The margins fitted, a list of character vectors.

table

The observed table.

converged, cycles

Whether IPF converged, and the number of cycles run.

Details

The margins fitted are the model's generating class (its highest-order terms), in the order MASS::loglm() uses: lower-order margins first. IPF starts from a table with all cells equal (step 0), so the first steps go from the equal-probability model towards the model fitted. The final fit agrees with stats::loglin() and MASS::loglm() with the same eps and iter.

For models with a closed-form fit (e.g. mutual or joint independence), the first cycle reaches the fit exactly; a second cycle confirms convergence.

References

Friendly, M. (1995). Conceptual and visual models for categorical data. The American Statistician, 49(2), 153–160. doi:10.1080/00031305.1995.10476134

Examples

# No three-way interaction: no closed form, so IPF iterates
steps <- ipf_steps(HairEyeColor, ~ (Hair + Eye + Sex)^2)
steps
#> IPF for margins {Hair:Eye, Hair:Sex, Eye:Sex}: 5 cycles, converged
#>  step cycle   margin      G2      X2 deviation
#>     0     0    start 475.118 557.622        NA
#>     1     1 Hair:Eye  21.810  21.455  82.00000
#>     2     1 Hair:Sex  11.764  11.771  17.50000
#>     3     1  Eye:Sex   7.588   7.548  11.74540
#>     4     2 Hair:Eye   7.554   7.508   0.88706
#>     5     2 Hair:Sex   6.936   6.991   3.32986
#>     6     2  Eye:Sex   6.805   6.863   1.95025
#>     7     3 Hair:Eye   6.798   6.864   0.38461
#>     8     3 Hair:Sex   6.771   6.864   0.74165
#>     9     3  Eye:Sex   6.764   6.859   0.45123
#>    10     4 Hair:Eye   6.763   6.861   0.10019
#>    11     4 Hair:Sex   6.762   6.866   0.17316
#>    12     4  Eye:Sex   6.761   6.866   0.10740
#>    13     5 Hair:Eye   6.761   6.867   0.02430
#>    14     5 Hair:Sex   6.761   6.868   0.04094
#>    15     5  Eye:Sex   6.761   6.868   0.02552

# The fit after the first step, which matches the Hair x Eye margin
ftable(steps$fits[[2]])
#>             Sex Male Female
#> Hair  Eye                  
#> Black Brown     34.0   34.0
#>       Blue      10.0   10.0
#>       Hazel      7.5    7.5
#>       Green      2.5    2.5
#> Brown Brown     59.5   59.5
#>       Blue      42.0   42.0
#>       Hazel     27.0   27.0
#>       Green     14.5   14.5
#> Red   Brown     13.0   13.0
#>       Blue       8.5    8.5
#>       Hazel      7.0    7.0
#>       Green      7.0    7.0
#> Blond Brown      3.5    3.5
#>       Blue      47.0   47.0
#>       Hazel      5.0    5.0
#>       Green      8.0    8.0

# The same from a data frame in frequency form
hec <- as.data.frame(HairEyeColor)
ipf_steps(hec, Freq ~ (Hair + Eye + Sex)^2)$cycles
#> [1] 5

# Draw a step as an expected-area mosaic with the observations as points
ggplot(hec, aes(x = product(Sex, Hair, Eye), weight = Freq)) +
  geom_mosaic(expected = steps$fits[[2]], area = "expected",
              jitter = TRUE, jitter_colour = "black", seed = 1995) +
  scale_fill_residual(limits = c(-4, 4), breaks = c(-4, -2, 0, 2, 4)) +
  labs(subtitle = "After step 1: Hair x Eye balanced") +
  theme_mosaic()