Tutorials › Causal Inference › Categorical Effect Modifiers and Contrast Coding

Effect Modification and Subgroup Effects · Tutorial 862 of 972

Categorical Effect Modifiers and Contrast Coding

Build and interpret treatment-by-category models, then translate their coefficients into treatment effects for each category under multiple contrast codings.

Advanced 12 min read

What You'll Learn

  • Define a category-specific causal treatment effect and the assumptions needed to identify it.
  • Write a treatment-by-category model using a full-rank set of contrasts.
  • Recover subgroup effects from reference-cell coefficients.
  • Interpret sum-to-zero and sequential contrast coefficients without treating them as subgroup effects.
  • Explain why fitted category-specific effects do not depend on the chosen contrast basis.

Why Contrast Coding Matters

When a treatment effect may differ across categories of a variable, the model needs to represent that variation. A treatment-by-category interaction does this, but its individual coefficients depend on how the categories are coded. A coefficient can be a treatment effect in one coding scheme and a difference between treatment effects in another. The central task is therefore not to read a coefficient in isolation, but to recover and interpret the treatment effect for each category.

Let \(A\) be a binary treatment, with \(A=1\) denoting treatment and \(A=0\) control; let \(S\) be a categorical effect modifier with \(K\) categories; and let \(Y\) be the outcome. For category \(s\), the causal treatment effect on the mean-difference scale is \(\tau_s=E[Y(1)-Y(0)\mid S=s]\), where \(Y(1)\) and \(Y(0)\) are the potential outcomes under treatment and control. This tutorial focuses on representing and recovering these category-specific effects in a linear mean model.

Definition: A treatment-by-category interaction allows the treatment contrast in the model to vary across the levels of \(S\). Contrast coding represents the categories with numerical columns so that category-specific means and treatment effects can be expressed as model coefficients. The coding is a parameterization; by itself, it does not change the causal estimand.

Model and Causal Interpretation

Use \(K-1\) contrast columns, collected in a vector \(c(S)\), to represent the \(K\) categories. A linear conditional-mean model with treatment interactions is

$$ E[Y\mid A,S] = \beta_0+\beta_A A+\gamma^\mathsf{T}c(S) +A\delta^\mathsf{T}c(S). $$

The terms \(\gamma^\mathsf{T}c(S)\) describe differences in the control-group mean across categories under this parameterization. The treatment part is \(\beta_A A+A\delta^\mathsf{T}c(S)\). Consequently, the model-implied treatment contrast in category \(s\) is

$$ \tau_s^{\text{model}} = E[Y\mid A=1,S=s]-E[Y\mid A=0,S=s] = \beta_A+\delta^\mathsf{T}c(s). $$

This is a model contrast. To interpret it as the causal effect \(\tau_s\), identification assumptions are required. Consistency means the observed outcome equals the potential outcome corresponding to the treatment actually received. Conditional exchangeability means \(Y(a)\) is independent of \(A\) given \(S\), for each treatment value \(a\). Positivity requires a positive probability of receiving either treatment within every category whose effect is to be estimated. Under these assumptions, the observed conditional mean contrast identifies the causal mean contrast within category \(s\). In a randomized trial, random assignment within categories supports conditional exchangeability; randomization does not remove the need for treatment representation in each category.

For a categorical \(S\), a saturated treatment-by-category mean model allows a separate mean for every treatment-by-category cell. It imposes no additional shape restriction across the categories. With complete data in all cells, it reproduces their means. In other settings, a less saturated model may be used, but then its functional form and any additional adjustment assumptions matter.

Reference-Cell Coding

Choose one category as the reference, say category \(R\). For each of the other \(K-1\) categories, create an indicator that equals 1 in that category and 0 otherwise. The reference category has all indicators equal to 0. In the interaction model, \(\beta_A\) is the treatment contrast in the reference category. For a non-reference category \(s\), its treatment effect is \(\beta_A+\delta_s\), where \(\delta_s\) is that category’s treatment-by-indicator coefficient. Thus an interaction coefficient is a difference from the reference category’s treatment effect, not usually the category’s treatment effect itself.

Worked Example: Recovering Effects With a Reference Category

Consider a hypothetical randomized trial with three categories of a baseline clinical characteristic: A, B, and C. Assignment is randomized within each category, and each treatment-by-category cell contains participants. Suppose the observed mean outcome, measured in points, is as follows.

CategoryControl meanTreatment meanCell mean contrast
A10122
B14195
C87-1

Assume consistency and positivity in all six cells. Randomization within category supports exchangeability. Fit the saturated linear mean model, using A as the reference and indicators \(I_B\) and \(I_C\):

$$ E[Y\mid A,I_B,I_C] = \beta_0+\beta_A A+\gamma_B I_B+\gamma_C I_C +\delta_B(AI_B)+\delta_C(AI_C). $$

For category A, \(I_B=I_C=0\). Its control mean is \(\beta_0=10\), and its treatment mean is \(\beta_0+\beta_A=12\). Therefore,

$$ \widehat{\beta}_A=12-10=2\text{ points}. $$

For category B, the control mean gives \(\widehat{\gamma}_B=14-10=4\). Its treatment contrast is \(\widehat{\beta}_A+\widehat{\delta}_B=19-14=5\), so

$$ \widehat{\delta}_B=5-2=3\text{ points}, \qquad \widehat{\tau}_B=2+3=5\text{ points}. $$

For category C, \(\widehat{\gamma}_C=8-10=-2\). Its contrast is \(\widehat{\beta}_A+\widehat{\delta}_C=7-8=-1\), giving

$$ \widehat{\delta}_C=-1-2=-3\text{ points}, \qquad \widehat{\tau}_C=2+(-3)=-1\text{ point}. $$

The model therefore recovers estimated treatment effects of 2 points in A, 5 points in B, and -1 point in C. These are estimated causal effects under the stated identification assumptions; the interaction coefficients, 3 and -3 points, are differences relative to the effect in A.

Alternative Contrasts and the Same Category Effects

Reference-cell coding is convenient when a particular category is a natural comparison. It is not the only valid coding. For three categories, one common alternative is sum-to-zero, or effect, coding. Assign contrast vectors \((1,0)\) to A, \((0,1)\) to B, and \((-1,-1)\) to C. The contrast values sum to zero across categories. In this coding, the treatment coefficient \(\beta_A\) is the unweighted average of the three category-specific treatment effects. The interaction coefficients describe deviations from that average, subject to the sum-to-zero constraint.

Worked Example: Sum-to-Zero Coding

Use the same hypothetical cell means as in the preceding example, but now use the sum-to-zero contrast vectors. The category-specific treatment effects calculated directly from the cells are 2, 5, and -1 points. Their unweighted average is

$$ \widehat{\beta}_A = \frac{2+5+(-1)}{3} = 2\text{ points}. $$

The interaction deviation for A is \(2-2=0\); for B it is \(5-2=3\); and for C it is \(-1-2=-3\). With the specified coding, these are represented by interaction coefficients \(\widehat{\delta}_1=0\) and \(\widehat{\delta}_2=3\). The deviation in C is \(-\widehat{\delta}_1-\widehat{\delta}_2=-3\), as required by the sum-to-zero constraint.

The intercept is the unweighted average of the control means:

$$ \widehat{\beta}_0 = \frac{10+14+8}{3} = 10.667\text{ points}. $$

The control-mean deviations are \(-0.667\) points for A, \(3.333\) points for B, and \(-2.667\) points for C. These sum to zero and are represented by \(\widehat{\gamma}_1=-0.667\) and \(\widehat{\gamma}_2=3.333\). For example, the fitted treatment mean in C is

$$ 10.667-2.667+2-3=7\text{ points}. $$

The subgroup effects remain 2, 5, and -1 points. The coefficient \(\beta_A=2\) is an equally weighted average across the three categories because of this coding. It is not automatically a sample-size-weighted average or an average for any particular target population.

Other full-rank contrast bases may make coefficients represent successive differences or other comparisons. For example, use vectors \((0,0)\) for A, \((1,0)\) for B, and \((1,1)\) for C. In this parameterization, the first interaction coefficient is the difference between the treatment effects in B and A; the second is the difference between the effects in C and B. It is important to inspect the coding values before assigning a verbal interpretation to a coefficient.

Worked Example: Sequential Contrasts

Again use the hypothetical means in the table. Under the sequential contrast vectors just defined, the treatment contrast in A is \(\widehat{\beta}_A=2\) points. In B, the model contrast is \(\widehat{\beta}_A+\widehat{\delta}_1=5\), so

$$ \widehat{\delta}_1=5-2=3\text{ points}. $$

In C, the contrast is \(\widehat{\beta}_A+\widehat{\delta}_1+\widehat{\delta}_2=-1\). Substituting the known values gives

$$ \widehat{\delta}_2=-1-2-3=-6\text{ points}. $$

Recovering the effects category by category gives A: \(2\) points; B: \(2+3=5\) points; and C: \(2+3-6=-1\) point. Here \(\widehat{\delta}_2=-6\) is the change from B to C in the treatment effect, not the treatment effect in C.

Why the Coding Changes Coefficients but Not Fitted Effects

The invariance can be seen by writing the vector of category-specific treatment contrasts. Let \(\mathbf{1}\) be the \(K\)-vector of ones and let \(C\) be the \(K\)-by-\((K-1)\) matrix whose rows are the category contrast vectors \(c(s)^\mathsf{T}\). A valid coding has the property that the columns of \([\mathbf{1}\ C]\) form a full-rank basis for the \(K\) category-specific values. The model’s vector of treatment contrasts is

$$ \boldsymbol{\tau}^{\text{model}} = \beta_A\mathbf{1}+C\boldsymbol{\delta}. $$

Because \([\mathbf{1}\ C]\) is full rank, this expression can represent any vector of \(K\) category-specific treatment contrasts, and its coefficients are determined by the chosen basis. Replacing \(C\) with another valid contrast matrix changes the coordinates \(\beta_A\) and \(\boldsymbol{\delta}\), but it still represents the same fitted contrast vector when the model is re-expressed correctly. The category-level predictions, and therefore the recovered treatment effects, are unchanged.

This argument also clarifies why omitting interaction terms is consequential. Without them, the treatment contrast is constrained to be the same in every category: \(\tau_s^{\text{model}}=\beta_A\). A coding change cannot remove that restriction. To allow category-specific effects, the model must include enough treatment-by-contrast terms to span their differences.

Practical Interpretation and a Common Mistake

For interpretation, first write the category-specific contrast from the fitted model, then substitute the category’s coding values. Report the resulting treatment contrast in the outcome’s units and name the category. State the causal assumptions that support a causal reading. A coefficient table alone may not make clear which category’s treatment effect a coefficient represents.

  • Wrong reasoning: “The interaction coefficient for category B is 3, so the treatment effect in B is 3 points.”
  • Correction: Under reference-cell coding with A as the reference, that coefficient is the difference between the B and A treatment effects. If the treatment coefficient is 2 points in A, the estimated effect in B is \(2+3=5\) points.
  • Wrong reasoning: “The treatment coefficient is always the effect for the reference group.”
  • Correction: That interpretation holds under reference-cell coding. Under sum-to-zero coding, it is the unweighted average across categories; other contrasts give it other meanings.

The scale also matters. In a linear mean model, these formulas give differences in conditional mean outcome. In a model with a different link, such as logistic regression, the coefficients are on that model’s link scale; translating them to a risk difference or another scale requires a separate calculation. Contrast coding does not make link-scale coefficients interchangeable with causal effects on other scales.

Key takeaway: Coding determines how the model’s coefficients describe category differences. Recover each category’s treatment contrast from the full treatment portion of the model, and interpret it causally only under the assumptions that identify that contrast.

Check Your Understanding

Answer each question by referring to the coding and the treatment contrast, not by interpreting a coefficient in isolation.

  1. With reference-cell coding, the estimated treatment coefficient is 1.5 points and the interaction coefficient for category B is 2 points. What is the estimated treatment effect in B, and what does the interaction coefficient represent?
  2. Under three-category sum-to-zero coding, why is the treatment coefficient the unweighted average of the three category-specific effects?
  3. What full-rank condition on the contrast coding allows a model to represent any vector of category-specific treatment effects?
  4. With the sequential contrasts from this tutorial, what does the second interaction coefficient compare?
  5. Name the assumptions needed to interpret an observed within-category mean contrast as a causal treatment effect.
Clinical Biostats