Statistics & probability

Three way contingency table log linear model calculator

Fit selected hierarchical log-linear interactions to a positive three-way contingency table using iterative proportional fitting and report deviance diagnostics.

Your numbers

Three integers from 2 to 4.
A changes slowest, C fastest; exactly A×B×C positive integer counts.
Generating margins define included interactions; saturated fits every positive count.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Fit selected hierarchical log-linear interactions to a positive three-way contingency table using iterative proportional fitting and report deviance diagnostics.

THE FORMULAIPF: fitted cell←fitted cell×observed generating-margin total/current fitted margin; G²=2Σ y log(y/μ̂); df=cells−model rank.

A worked example

The default 2×2×2 pairwise model fits counts [9,11,8,12,12,8,11,9], giving deviance≈7.518041 on 1 residual degree of freedom and asymptotic p≈0.006108.

Good to know

Complete positive-integer three-way table with 2–4 levels per factor and count≤100000. Six declared hierarchical models only. IPF≤10000 iterations, margin residual≤1e−10×total; ordinary model rank and asymptotic deviance test. No zero cells, structural zeros, survey weights, ordinal constraints, sparse exact inference or automatic selection.

Represent a three-way count table

A three-way contingency table records counts for combinations of three categorical factors, here called A, B and C. Enter their three dimension sizes and a flattened list of cell counts. A changes slowest, B changes in the middle and C changes fastest, so the index order is part of the data specification rather than a cosmetic labeling choice.

The supported table has two to four levels per factor and strictly positive integer counts in every cell. This restriction avoids structural-zero and boundary-maximum-likelihood cases that require additional analysis. A missing observation, an impossible combination and a genuinely observed zero are scientifically different situations and should not be replaced by a positive pseudocount merely to pass an input check.

Observed and fitted three-way table countsDefault pairwise log-linear model081614.58Cell order; C varies fastestBlue: observed countsGreen: fitted counts
Scroll across to see the full diagram.
The fitted pairwise margins match the observed margins, while the remaining three-way pattern yields deviance approximately7.518 on one degree of freedom.

Compare this with the Matrix balancing calculator with target row column totals. Fit declared hierarchical log-linear interactions to a three-way count table and compare fitted counts, deviance and residuals.

Specify interactions hierarchically

A log-linear model describes which factor interactions are included in the log expected counts. The independence option includes the grand mean and three main effects only. The pairwise option also includes AB, AC and BC interactions while excluding the three-factor interaction, allowing pairwise associations without assuming a fully unrestricted table.

The conditional models use two selected pairwise margins, such as AB and BC, together with all effects required by hierarchy. In that example, A and C are conditionally independent given B. The saturated model includes ABC and reproduces every supplied positive count. Choosing a model is a statement about associations, not an automatic ranking of causal relationships between the factors.

Fit margins by proportional adjustment

Iterative proportional fitting starts from positive fitted counts and repeatedly rescales them to match each generating margin. When the margin constraints have converged, the fitted table gives the maximum-likelihood count estimates for the specified hierarchical model under the supported positive-table setting. Every complete iteration revisits all declared margins rather than adjusting only the visibly largest discrepancy.

The output reports the number of iterations and the largest remaining observed-versus-fitted margin difference. That residual supplies a direct convergence check. A stable-looking deviance alone would not prove that all defining margins were matched, and a sequence that reaches the iteration cap is rejected rather than treated as an automatically successful fit.

Follow the two-by-two-by-two example

The eight default counts are 12, 8, 5, 15, 9, 11, 14 and 6, totaling eighty observations. With all pairwise interactions included, the fitted counts are approximately 9, 11, 8, 12, 12, 8, 11 and 9. These fitted values retain the observed two-way margins while smoothing the remaining three-factor pattern.

The pairwise model has seven free parameters across eight cells, leaving one residual degree of freedom. The likelihood-ratio deviance is approximately 7.518041 and its ordinary asymptotic chi-square tail approximately 0.006108. That comparison concerns the omitted three-way interaction under this model, not a universal test that every pair of factors is independent.

Read deviance and residuals together

The likelihood-ratio deviance adds twice each observed count times the logarithm of observed divided by fitted count. Pearson's statistic instead adds squared count differences divided by fitted count. They need not be identical in a finite table, although both are compared with the same model degrees of freedom in their usual asymptotic interpretations.

Pearson residuals retain the sign of observed minus fitted. A positive residual identifies a cell containing more observations than predicted, while a negative residual identifies fewer. Their values help locate the pattern behind an overall statistic, but several cells and model choices create a multiplicity problem if they are inspected as independent confirmatory discoveries after fitting.

Check degrees of freedom and nesting

Free parameters are counted from the hierarchical main-effect and interaction dimensions. For example, a pairwise AB term contributes the number of A levels minus one multiplied by the number of B levels minus one. Adding all allowed terms gives the model rank for this complete positive table; residual degrees of freedom are cell count minus that rank.

The saturated fit has zero residual degrees of freedom and deviance zero up to numerical roundoff. No goodness-of-fit p-value is reported for that zero-degree-of-freedom comparison. Models can also be compared when properly nested, but this interface does not automatically select a model, correct a search across many alternatives or replace sparse-table inference with an exact procedure.

Separate fit from scientific interpretation

This calculation fits associations in one complete three-factor count table. It contains no sampling weights, repeated-person dependence, structural zeros, ordinal trend constraints or causal adjustment. A small p-value can reflect a scientifically important omitted interaction, a sampling design mismatch or an unsuitable asymptotic approximation, so interpretation needs more than the numerical optimizer's convergence status.

Record the factor order, level counts, generating margins and total observations with the fitted table. The returned fitted counts and residuals allow another analyst to reconstruct the model comparison. If the data include genuine zero cells or design-impossible combinations, use a method whose support and degrees of freedom explicitly match that setting rather than recoding the table silently.

A few questions, answered

What order are the cell counts entered in?

A is the outermost factor, B the middle factor and C varies fastest. Each count corresponds to that explicit factor-index triple.

Why are zero cells not supported?

Structural zeros and boundary likelihood cases need support-specific checks and degrees of freedom. This bounded interface requires positive cells and does not insert pseudocounts.

Does the pairwise model mean all factors are independent?

No. It includes every two-factor interaction while omitting the three-factor interaction. Mutual independence is a separate, more restrictive model option.

Why is there no p-value for the saturated model?

The saturated model fits every count and has zero residual degrees of freedom. Its zero deviance is not an informative goodness-of-fit test.

Find your calculator

Start typing to explore the library.