Tutorials › Generalized Linear Models › Iterative Proportional Fitting for Loglinear Models

Poisson Loglinear Models · Tutorial 462 of 981

Iterative Proportional Fitting for Loglinear Models

Learn how iterative proportional fitting adjusts fitted margins to observed margins and how to carry out and check the updates in two- and three-way tables.

Intermediate 11 min read

What You'll Learn

  • Derive the IPF multiplier for a selected margin from the Poisson log-likelihood.
  • Identify which observed margins define a hierarchical loglinear model.
  • Apply successive margin adjustments to a two-way independence table.
  • Work through updates for three-way models with two and three generating margins.
  • Explain why each positive IPF update matches its target margin and does not reduce likelihood.
  • Check whether a fitted table matches all the margins required by its model.

Why Adjust Fitted Margins?

For many loglinear models, the sufficient statistics are selected marginal totals. A finite maximum-likelihood fit reproduces those observed totals; if the MLE does not exist in the model’s parameter space, the observed margins need not be attained by a finite fitted table, but the model also constrains the pattern of counts across cells. Iterative proportional fitting (IPF) constructs a fitted table by adjusting its current cell means so that one required margin at a time matches the observed margin.

The method is useful because each adjustment is simple: within a stratum of the selected margin, multiply every current fitted count by the same observed-to-fitted ratio. Repeating these updates builds a table with the structure of the model while matching its required margins. The sequence of updates matters for intermediate tables; after one margin is matched, another update can disturb it, so margins must be checked again.

Definition and Update Rule

Definition: Let \(y_{i_1,\ldots,i_p}\) be observed counts in a finite contingency table, and let \(m_{i_1,\ldots,i_p}\) be fitted Poisson means. For a set of variables \(S\), write \(y_S(i_S)\) for the observed marginal total in category combination \(i_S\), obtained by summing over all variables not in \(S\); define \(m_S(i_S)\) in the same way. An IPF update for margin \(S\) is $$ m^{\mathrm{new}}_{i_1,\ldots,i_p} = m^{\mathrm{old}}_{i_1,\ldots,i_p} \frac{y_S(i_S)}{m^{\mathrm{old}}_S(i_S)}. $$ The ratio is constant across the cells contributing to that particular marginal total.

For a hierarchical loglinear model, the selected margins correspond to its generating class: the maximal variable sets in the model. For example, the model \([AB][AC]\) has generating margins \(AB\) and \(AC\), and includes their lower-order terms. It does not include a \(BC\) interaction. The model \([AB][AC][BC]\) has all three two-factor generating margins, but no three-factor interaction.

Assume for the update rule that all cells are allowed and that the starting means and target margins are positive. Then every denominator is positive, and every update is well-defined and positive. Zero margins and structural zeros require care: a multiplier of zero can remove cells from the support, and the positive-table arguments below do not cover that case.

Why the Update Has This Form

The formula is not an arbitrary rescaling. Under independent Poisson sampling, the log-likelihood, apart from terms that do not involve the means, is $$ \ell(m)=\sum_i \{y_i\log(m_i)-m_i\}. $$ Suppose the current means have the form required by a loglinear model, and consider changing the parameter associated with margin \(S\) by adding a separate amount \(\gamma_s\) for each category combination \(s=i_S\). In the cells of stratum \(s\), this multiplies each current mean by \(e^{\gamma_s}\). Holding the other model terms fixed, the part of the log-likelihood depending on \(\gamma_s\) is $$ \ell_s(\gamma_s) = y_s\gamma_s-e^{\gamma_s}q_s+\text{constant}, $$ where \(y_s=y_S(s)\) and \(q_s=m^{\mathrm{old}}_S(s)\).

Result: If \(y_s>0\) and \(q_s>0\), maximizing the likelihood over the multiplier for stratum \(s\) gives \(e^{\gamma_s}=y_s/q_s\). Thus the IPF update is an exact likelihood-maximizing adjustment for that margin, with the other model terms held fixed.

Proof. Differentiating the displayed log-likelihood contribution gives $$ \frac{d\ell_s}{d\gamma_s}=y_s-e^{\gamma_s}q_s. $$ Setting this derivative to zero yields \(e^{\gamma_s}=y_s/q_s\). The second derivative is \(-e^{\gamma_s}q_s<0\), so this stationary point is the unique maximum. Applying this calculation separately to every stratum \(s\) gives the stated cellwise multiplier. \(\square\)

The update matches the selected margin immediately: summing the new means over the cells in stratum \(s\) gives $$ m^{\mathrm{new}}_S(s) = \frac{y_S(s)}{m^{\mathrm{old}}_S(s)} m^{\mathrm{old}}_S(s) = y_S(s). $$ This identity explains the name “proportional fitting”: each stratum is scaled in proportion to its observed-to-fitted margin ratio.

Worked Example: Two-Way Independence

Worked Example: Two-Way Independence

Consider the observed table

B = 1B = 2Row total
A = 1102030
A = 2304070
Column total4060100

The independence model \([A][B]\) requires the row and column margins. Start with fitted means equal to 1 in all four cells. Their row margins are 2 and 2, so the row update multiplies the first row by \(30/2=15\) and the second by \(70/2=35\). The intermediate fitted table is

B = 1B = 2Row total
A = 1151530
A = 2353570
Column total5050100

The column totals are now 50 and 50, not the observed 40 and 60. The column update therefore uses factors \(40/50=0.8\) and \(60/50=1.2\). Multiplying each cell in the corresponding column gives fitted counts 12, 18, 28, and 42. The resulting row totals remain 30 and 70, and the column totals are 40 and 60. Both required margins are matched.

This one-pass result is the familiar independence fit: each fitted count is its row total times its column total, divided by the grand total. For example, the fitted count in the first cell is \(30(40)/100=12\). In this example the column update preserves the row totals because both rows had the same column proportions before that update.

Worked Example: The Model \([AB][AC]\)

Worked Example: Updating AB and AC Margins

Consider a three-way table with binary variables \(A,B,C\). Suppose the target \(AB\) margins are \(4,6\) for \(A=1\) and \(6,8\) for \(A=2\), in the order \(B=1,B=2\). Suppose the target \(AC\) margins are \(6,4\) for \(A=1\) and \(5,9\) for \(A=2\), in the order \(C=1,C=2\). Both margins have grand total 24. Fit the model \([AB][AC]\), starting with a fitted count of 1 in every cell.

First, update \(AB\). Each starting \(AB\) margin is 2, since it sums over the two levels of \(C\). The four multipliers are \(4/2=2\), \(6/2=3\), \(6/2=3\), and \(8/2=4\). Each multiplier is applied to both cells in its \(AB\) stratum. In each \(C\) slice, the resulting fitted table, with rows \(A=1,2\) and columns \(B=1,2\), is

$$ \begin{pmatrix}2&3\\3&4\end{pmatrix}. $$

At this stage the \(AB\) margins match their targets. The current \(AC\) margins are 5 and 5 for \(A=1\), and 7 and 7 for \(A=2\). These are obtained by summing across \(B\) separately at each level of \(C\).

Next, update \(AC\). For \(A=1\), the factors for \(C=1,2\) are \(6/5\) and \(4/5\). For \(A=2\), they are \(5/7\) and \(9/7\). Apply each factor to both \(B\) cells in its \(AC\) stratum. The resulting slices are

$$ C=1:\begin{pmatrix}12/5&18/5\\15/7&20/7\end{pmatrix}, \qquad C=2:\begin{pmatrix}8/5&12/5\\27/7&36/7\end{pmatrix}. $$

For \(C=1\), the row totals are \(12/5+18/5=6\) and \(15/7+20/7=5\). For \(C=2\), they are \(8/5+12/5=4\) and \(27/7+36/7=9\). Thus the \(AC\) margins match. The \(AB\) margins still match as well: for \(A=1\), the \(B\)-specific totals across \(C\) are \(12/5+8/5=4\) and \(18/5+12/5=6\); for \(A=2\), they are \(15/7+27/7=6\) and \(20/7+36/7=8\).

Both margins are matched after this sweep. Each update multiplies the starting means by a factor depending only on \(AB\) or only on \(AC\), so the resulting log means have the form required by \([AB][AC]\). In this example no further sweep is needed.

Worked Example: A Three-Way Model with All Pairwise Margins

Worked Example: Fitting \([AB][AC][BC]\)

Here are observed counts for a binary three-way table. Rows are \(A=1,2\), columns are \(B=1,2\), and the two matrices correspond to \(C=1\) and \(C=2\):

$$ C=1:\begin{pmatrix}8&2\\1&5\end{pmatrix}, \qquad C=2:\begin{pmatrix}4&6\\7&3\end{pmatrix}. $$

The target \(AB\) margins are \(\begin{pmatrix}12&8\\8&8\end{pmatrix}\). The \(AC\) margins, listed by \(C=1,2\) for each level of \(A\), are \(10,10\) for \(A=1\) and \(6,10\) for \(A=2\). The \(BC\) margins, listed by \(C=1,2\) for each level of \(B\), are \(9,11\) for \(B=1\) and \(7,9\) for \(B=2\). Each set has grand total 36.

Start with all eight fitted counts equal to 1, and update \(AB\). Each current \(AB\) margin is 2, so the multipliers are \(12/2=6\), \(8/2=4\), \(8/2=4\), and \(8/2=4\). Both fitted slices become $$ \begin{pmatrix}6&4\\4&4\end{pmatrix}. $$

Now update \(AC\). For \(A=1\), the current margins are 10 and 10, already equal to their targets, so both factors are 1. For \(A=2\), the current margins are 8 and 8, so the factors are \(6/8=3/4\) for \(C=1\) and \(10/8=5/4\) for \(C=2\). The updated slices are $$ C=1:\begin{pmatrix}6&4\\3&3\end{pmatrix}, \qquad C=2:\begin{pmatrix}6&4\\5&5\end{pmatrix}. $$ The \(AB\) margins remain \(\begin{pmatrix}12&8\\8&8\end{pmatrix}\): for the second row, for instance, the \(B=1\) total is \(3+5=8\), and the \(B=2\) total is also \(3+5=8\).

Finally, check \(BC\). For \(C=1\), the fitted \(B=1\) and \(B=2\) totals are \(6+3=9\) and \(4+3=7\). For \(C=2\), they are \(6+5=11\) and \(4+5=9\). These equal the target \(BC\) margins, so all three required margins match. The fitted table is in the model \([AB][AC][BC]\), since the updates used factors depending only on the specified generating margins.

Likelihood Behavior and Practical Use

For positive observed margins and positive current fitted margins, an IPF update cannot reduce the Poisson log-likelihood. To see this, consider a single stratum with observed total \(y>0\) and current fitted total \(q>0\). Its multiplier is \(r=y/q\). The change in log-likelihood from scaling the cell means in that stratum by \(r\) is $$ y\log(r)-(r-1)q = y\log(y/q)-y+q. $$ Writing \(x=y/q\), this equals \(q(x\log x-x+1)\), which is nonnegative for \(x>0\): the function \(g(x)=x\log x-x+1\) has derivative \(\log x\), has its minimum at \(x=1\), and \(g(1)=0\). Summing these nonnegative changes over the strata proves that the update does not reduce the likelihood. Equality holds when every current stratum total already matches its target. \(\square\)

In practice, cycle through the generating margins and stop when every required margin is sufficiently close to its observed counterpart. One possible check is to compute, for every target margin, the largest absolute difference between fitted and observed marginal totals. The tolerance should be chosen relative to the scale of the counts and the accuracy needed. A matched margin is not, by itself, evidence that the fit is complete: check all generating margins after a full cycle.

The examples also show why a statement such as “the last margin update always leaves earlier margins unchanged” is false in general. In the \([AB][AC]\) example, the numbers happened to preserve the \(AB\) margins after the \(AC\) update. That preservation was verified from the resulting cell values; it should not be assumed in a different table. In general, continue cycling and recheck.

Common Mistakes

  • Using the wrong denominator. Divide the observed margin by the current fitted margin in the same stratum. In the \([AB][AC]\) example, the current \(AC\) margins for \(A=2\) were 7 and 7, so the update factors were \(5/7\) and \(9/7\), not ratios with denominator 5.
  • Applying a margin factor to only one cell. The factor for a margin stratum multiplies every cell that contributes to that marginal total.
  • Assuming a single pass always suffices. A later margin adjustment can disturb an earlier one. The required check is whether all generating margins match after the updates.
  • Fitting margins that are not in the model. For \([AB][AC]\), the required margins are \(AB\) and \(AC\), not \(BC\). The generating class determines which margins IPF targets.
Key takeaway: IPF adjusts every cell in a selected marginal stratum by the ratio of its observed to current fitted margin. The update exactly matches that margin and maximizes the likelihood over its multiplier block. Cycle through the model’s generating margins, then verify that all required margins match.

Check Your Understanding

Use the update rule and the model’s generating margins to answer the following.

  1. A current fitted \(2\times2\) table has row totals 12 and 18. The observed row totals are 10 and 20. What factors are used in a row-margin update?
  2. In a three-way table, explain why the \(AB\) update multiplies cells across both levels of \(C\) by the same factor within each \(AB\) stratum.
  3. For the model \([AB][AC]\), which margins must the fitted table match? Is \(BC\) a generating margin?
  4. Why can a later IPF update disturb a margin that an earlier update matched?
  5. State the conditions under which the positive-margin update derived here is well-defined.
Clinical Biostats