Tutorials › Generalized Linear Models › Interactions Under Effect and Custom Coding

Factors Contrasts and Interactions · Tutorial 151 of 981

Interactions Under Effect and Custom Coding

Build interaction terms from the chosen factor coding and translate their coefficients into changes in planned contrasts across factor levels.

Intermediate 10 min read

What You'll Learn

  • Construct all pairwise products of effect or custom contrast columns.
  • Express a two-factor GLM linear predictor using coding matrices.
  • Interpret interaction coefficients relative to the scaling of the chosen contrasts.
  • Derive how a specified contrast in one factor changes across levels of the other.
  • Distinguish link-scale interaction effects from response-scale comparisons.

Why the Coding Matters for Interactions

An interaction asks whether the effect associated with one factor changes across levels of another. In a generalized linear model, that comparison is made on the linear-predictor scale, unless a different scale is explicitly specified. With effect or custom coding, the numerical interaction coefficients depend on the contrast columns used to represent the factors. Interpreting them therefore requires looking at the coding, not just at the model formula.

The construction is systematic: write the chosen contrast columns for each factor, then include every product of one column from the first factor and one from the second. The resulting coefficients describe how the coded contribution of one factor changes as the other factor's coded values change.

Definition: Suppose factor A has \(r\) levels and factor B has \(s\) levels. Let \(C_A\) be an \(r \times p\) matrix of coding columns for A and \(C_B\) an \(s \times q\) matrix of coding columns for B. Write \(a_i^T\) for row \(i\) of \(C_A\) and \(b_j^T\) for row \(j\) of \(C_B\). A two-factor linear predictor with all pairwise product terms is $$ \eta_{ij}=\beta_0+a_i^T\alpha+b_j^T\gamma+a_i^T D b_j. $$ Here \(\alpha\) and \(\gamma\) are the main-effect coefficient vectors, and \(D\) is a \(p \times q\) matrix of interaction coefficients. The product terms are the entries \(a_{ik}b_{j\ell}\), one for every pair of coding columns \(k,\ell\). For ordinary contrast coding, the columns of each coding matrix sum to zero.

This expression describes a two-factor model on the GLM linear-predictor scale: \(\eta_{ij}=g(\mu_{ij})\), where \(\mu_{ij}=E[Y\mid A=i,B=j]\) and \(g\) is the link. If a coding matrix has multiple columns, an interaction is generally a set of product terms, not a single coefficient. The size and meaning of each coefficient depend on the scale and orientation of the coding columns.

How a Coded Contrast Changes Across Levels

At a fixed level \(j\) of B, the coefficient vector multiplying A's coding row is \(\alpha+D b_j\). Thus the A contribution is \(a_i^T(\alpha+D b_j)\). Moving from level \(j'\) to level \(j\) changes this coefficient vector by \(D(b_j-b_{j'})\). This gives a direct interpretation of the product terms: they describe changes in A's coded contribution as B changes.

Result: Let \(c\) be a contrast across the levels of A, so \(c\) is an \(r\)-vector with \(\mathbf{1}^T c=0\). The corresponding contrast in the linear predictor at B level \(j\) is $$ c^T\eta_{\cdot j}=c^T C_A(\alpha+D b_j). $$ Consequently, the change in that A contrast between B levels \(j'\) and \(j\) is $$ c^T\eta_{\cdot j}-c^T\eta_{\cdot j'} =c^T C_A D(b_j-b_{j'}). $$

Proof. Collect the \(r\) linear predictors at B level \(j\) into the vector \(\eta_{\cdot j}=\beta_0\mathbf{1}+C_A\alpha+\mathbf{1}(b_j^T\gamma)+C_A D b_j\). Multiplying by \(c^T\) removes both terms proportional to \(\mathbf{1}\), because \(c^T\mathbf{1}=0\). The remaining terms are \(c^T C_A\alpha+c^T C_A D b_j\), which gives the first formula. Subtracting the corresponding expression at \(j'\) cancels \(c^T C_A\alpha\), leaving \(c^T C_A D(b_j-b_{j'})\). \(\square\)

The formula is useful even when the named scientific contrast is not exactly one coding column. The factor \(c^T C_A\) translates that contrast into the coordinates used by the model. If it equals a coordinate vector, the contrast directly selects one row of \(D\); otherwise it combines several rows. The same point applies to contrasts across B.

Contrasts of Contrasts and Interaction Coefficients

An interaction can also be described as a contrast across A of a contrast across B. Let \(\eta\) be the \(r \times s\) matrix whose \((i,j)\) entry is \(\eta_{ij}\). If \(c\) and \(d\) are zero-sum contrast vectors for A and B, respectively, then \(c^T\eta d\) is a difference of differences on the linear-predictor scale.

Result: Under the coded model above, $$ c^T\eta d=(c^T C_A)D(C_B^T d). $$ In particular, if \(c^T C_A\) and \(C_B^T d\) each select a single coding coordinate, this contrast of contrasts is the corresponding entry of \(D\). Otherwise it is a weighted combination of interaction coefficients.

Proof. In matrix form the linear predictors are \(\eta=\beta_0\mathbf{1}\mathbf{1}^T+C_A\alpha\mathbf{1}^T+\mathbf{1}\gamma^T C_B^T+C_A D C_B^T\). Multiply on the left by \(c^T\) and on the right by \(d\). The intercept term vanishes because \(c^T\mathbf{1}=0\); the A main-effect term vanishes because \(\mathbf{1}^T d=0\); and the B main-effect term vanishes because \(c^T\mathbf{1}=0\). The remaining term is \(c^T C_A D C_B^T d\), as claimed. \(\square\)

This result gives a precise qualification to the phrase “the interaction coefficient is a difference of differences.” It is true directly only when the contrast vectors have the appropriate scaling relative to the coding columns. With other scalings, the coefficient may be a multiple of the contrast of contrasts, or the contrast may combine several coefficients.

Worked Examples

Worked Example: A Two-by-Two Model With Effect Coding

Let A and B each have two levels, coded \(x=-1,+1\) and \(z=-1,+1\). Consider the linear predictor $$ \eta=1+0.4x-0.2z+0.3xz. $$ Suppose the GLM uses a logit link, so \(\eta\) is the log odds.

At \(z=+1\), the difference in log odds between A's \(x=+1\) and \(x=-1\) levels is \((1.5)-(0.1)=1.4\). The two values follow from \(1+0.4-0.2+0.3=1.5\) and \(1-0.4-0.2-0.3=0.1\).

At \(z=-1\), the corresponding difference is \((1.3)-(1.1)=0.2\), since \(1+0.4+0.2-0.3=1.3\) and \(1-0.4+0.2+0.3=1.1\).

The change in A's log-odds contrast between the two B levels is \(1.4-0.2=1.2\). The product coefficient is \(0.3\), and the effect-coded formula gives \(4(0.3)=1.2\): changing \(z\) from \(-1\) to \(+1\) changes the A contrast by \(2\) units of \(x\) times \(2\) units of \(z\). On the odds-ratio scale, the ratio of A's odds ratios across B levels is \(\exp(1.2)\), approximately \(3.32\). The coefficient \(0.3\) is not itself the full change between B levels.

Worked Example: Planned A Contrasts Under Custom Coding

Let A have three levels with coding columns \(c_1=(-1,1,0)^T\) and \(c_2=(-1,-1,2)^T\). Let B be coded \(z=0\) or \(z=1\), and use the linear predictor $$ \eta_{ij}=2+0.5c_{1i}-0.2c_{2i}+0.3z+0.4c_{1i}z+0.1c_{2i}z. $$ The coding columns for A each sum to zero, and the product coefficients are \(0.4\) and \(0.1\).

At \(z=0\), the three linear predictors for A's levels are \(1.7, 2.7, 1.6\). For example, level 1 gives \(2+0.5(-1)-0.2(-1)=1.7\), and level 3 gives \(2+0-0.2(2)=1.6\). At \(z=1\), they are \(1.5, 3.3, 2.1\): the additional terms are \(0.3+0.4c_{1i}+0.1c_{2i}\) at each level.

Consider the planned A contrast \(c=(-1/2,1/2,0)^T\). Its coding-coordinate values are \(c^T c_1=1\) and \(c^T c_2=0\), so it selects the first A coding column. Its value at \(z=0\) is \((-1.7+2.7)/2=0.5\); at \(z=1\) it is \((-1.5+3.3)/2=0.9\). The change is \(0.9-0.5=0.4\), exactly the coefficient of \(c_1z\).

For the other planned contrast \(d=(-1/6,-1/6,1/3)^T\), \(d^T c_1=0\) and \(d^T c_2=1\). Its values are \(-(1.7+2.7)/6+1.6/3=-0.2\) at \(z=0\) and \(-(1.5+3.3)/6+2.1/3=-0.1\) at \(z=1\). Its change is \(0.1\), the coefficient of \(c_2z\). The main effect \(0.3z\) cancels in both contrasts because their weights sum to zero.

Worked Example: Three B Levels With Custom Contrasts

Let A be binary with effect code \(x=-1,+1\). Let B have three levels, coded by \(b_1=(-1,1,0)^T\) and \(b_2=(-1,-1,2)^T\). Suppose the log-link linear predictor includes the A terms \(\eta_{ij}=\text{B terms}+0.3x+0.2xb_{1j}-0.1xb_{2j}\). The B terms do not affect the A contrast because they are identical for the two A levels.

Define the A contrast at B level \(j\) as half the difference in linear predictors between \(x=+1\) and \(x=-1\). The terms involving \(x\) show that this contrast equals \(0.3+0.2b_{1j}-0.1b_{2j}\). At B level 1, it is \(0.3-0.2+0.1=0.2\); at level 2, it is \(0.3+0.2+0.1=0.6\); at level 3, it is \(0.3+0-0.2=0.1\).

Thus the contrast changes from level 1 to level 2 by \(0.6-0.2=0.4=2(0.2)\), because \(b_1\) changes by 2 and \(b_2\) does not change. From level 1 to level 3, it changes by \(0.1-0.2=-0.1=0.2-3(0.1)\), because the coding changes are \(+1\) in \(b_1\) and \(+3\) in \(b_2\). Under the log link, twice each contrast is the log ratio of the fitted means for A's two levels. The product coefficients describe the chosen B contrast directions, not automatically the pairwise changes between every pair of B levels.

Common Pitfall and Practical Interpretation

A common mistake is to read a product coefficient as the change in the scientific contrast between any two factor levels. For example, seeing \(0.3\) on \(xz\) and reporting that “A's effect changes by \(0.3\) between B levels” is incorrect when both factors are coded \(-1,+1\): the change in A's logit contrast is \(4(0.3)=1.2\). Likewise, with several custom columns, an individual coefficient describes a change per unit of its particular coding column, not necessarily the planned comparison named in the study question.

A reliable interpretation names three things: the contrast in A, the comparison or coding change in B, and the scale on which the calculation is made. In a GLM, the coefficient calculations above concern \(\eta=g(\mu)\). For a logit link they are log-odds contrasts; for a log link they are log-mean contrasts. Transforming to odds or means requires applying the inverse link, and differences on the response scale need not equal the linear-predictor differences.

1
Write the coding values.
Record each factor's contrast columns, including their signs and scaling.
2
Construct the products.
Multiply every A coding column by every B coding column and include the resulting terms in the linear predictor.
3
Form the requested contrast.
Use the contrast weights and coded rows to calculate the change on the linear-predictor scale.
4
Translate through the link.
When the scientific interpretation concerns means, odds, or rates, apply the inverse link or the appropriate exponential transformation.
Key takeaway: Effect and custom coding change the coordinates used to describe interactions, not the fitted cell linear predictors. Interpret product coefficients by combining them with the actual contrast values; a coefficient equals a named change only when the chosen contrast scaling makes it so.

Check Your Understanding

Use the coding columns and link scale specified in each question.

  1. With binary effect codes \(x,z\in\{-1,+1\}\), by what factor does the A logit contrast change between B levels when the coefficient of \(xz\) is \(0.25\)?
  2. In a model with A coding columns \(c_1,c_2\), what does the coefficient of \(c_2z\) represent when B is coded 0 and 1?
  3. Why can a planned A contrast combine multiple rows of the interaction coefficient matrix \(D\)?
  4. For custom B coding, what information is needed to calculate the change in an A contrast between two B levels?
  5. Under a log link, what does a difference in linear-predictor contrasts correspond to after exponentiation?
Clinical Biostats