Why Choose a Contrast?
A factor with several levels can be represented by more than one set of coefficients. The choice is not merely a software setting: it determines which comparisons appear directly as individual coefficients. A clinical study might ask how each treatment compares with control, whether each treatment departs from the average level, or whether the average of two active treatments differs from control. These questions call for different contrast parameterizations.
The scientific comparison and the fitted model are related but distinct. When two contrast codings span the same model space, they give the same fitted linear predictors and means; they express those fits using different coefficients. A useful coding makes the coefficients answer the intended question without changing what the model can fit.
The examples below focus on a factor by itself, or on its comparisons at fixed values of other predictors. In a GLM, each level has a linear predictor \(\eta_j=g(\mu_j)\), where \(g\) is the link function and \(\mu_j\) is the corresponding mean. A contrast of the \(\eta_j\)'s is on the linear-predictor scale. If \(g\) is nonlinear, it is generally not the same as a contrast of the means \(\mu_j\).
Reference Contrasts: Comparisons With a Baseline
Reference coding designates one level as the baseline. With control as the reference and treatment levels A and B, the linear predictors can be written as \(\eta_j=\alpha+\beta_j\) for treatment levels, and \(\eta_{\text{control}}=\alpha\). Thus \(\alpha\) is the reference-level linear predictor, while each treatment coefficient is a difference from control on the linear-predictor scale. This is a natural choice when the study's primary comparisons are each treatment versus control.
Worked Example: Treatment Effects Relative to Control
Suppose a logistic regression with a three-level treatment factor has fitted coefficients \(\hat\alpha=-1.2\), \(\hat\beta_A=0.5\), and \(\hat\beta_B=0.9\), with control as the reference. Find the fitted probabilities and interpret the treatment coefficients.
The fitted linear predictors are \(\hat\eta_{\text{control}}=-1.2\), \(\hat\eta_A=-1.2+0.5=-0.7\), and \(\hat\eta_B=-1.2+0.9=-0.3\). For the logit link, the inverse link is \(\operatorname{logit}^{-1}(\eta)=1/(1+\exp(-\eta))\). Therefore the fitted probabilities are \(\operatorname{logit}^{-1}(-1.2)\approx0.231475\), \(\operatorname{logit}^{-1}(-0.7)\approx0.331812\), and \(\operatorname{logit}^{-1}(-0.3)\approx0.425557\).
The coefficient \(0.5\) says that A is \(0.5\) higher than control in log odds; the corresponding odds ratio is \(\exp(0.5)\approx1.649\). It does not say that A's probability is 0.5 higher. The probability difference is \(\operatorname{logit}^{-1}(-0.7)-\operatorname{logit}^{-1}(-1.2)\approx0.331812-0.231475=0.100337\), or about \(0.100\). The probability difference for B versus control is approximately \(0.425557-0.231475=0.194082\), or \(0.194\).
Reference coding is convenient for baseline comparisons, but its coefficients change if the reference level changes. That does not imply the fitted treatment means change: it changes which level is represented by the intercept and which differences are reported directly as coefficients.
Effect Contrasts: Deviations From a Level Average
An effect-coding parameterization can make deviations from the unweighted average of the level-specific linear predictors explicit. For three levels A, B, and C, one sum-to-zero coding uses the rows \((1,0)\), \((0,1)\), and \((-1,-1)\). With this coding, \(\eta_A=\alpha+\gamma_A\), \(\eta_B=\alpha+\gamma_B\), and \(\eta_C=\alpha-\gamma_A-\gamma_B\). The three deviations from \(\alpha\) sum to zero, and \(\alpha=(\eta_A+\eta_B+\eta_C)/3\). The intercept is therefore the average on the linear-predictor scale, not necessarily the average response mean.
Worked Example: Interpreting Sum-to-Zero Effects
Use the three fitted linear predictors from the previous example: \(\hat\eta_{\text{control}}=-1.2\), \(\hat\eta_A=-0.7\), and \(\hat\eta_B=-0.3\). Treat these as levels C, A, and B, respectively. Find the effect-coded intercept and deviations.
The average linear predictor is \(\hat\alpha=(-1.2-0.7-0.3)/3=-2.2/3\approx-0.733333\). The deviations for C, A, and B are, respectively, \(-1.2-(-0.733333)\approx-0.466667\), \(-0.7-(-0.733333)\approx0.033333\), and \(-0.3-(-0.733333)\approx0.433333\). They sum to zero, up to rounding. In the displayed coding, take \(\gamma_A\approx0.033333\) and \(\gamma_B\approx0.433333\). The third level's deviation is then \(-\gamma_A-\gamma_B\approx-0.466667\), as required.
These are deviations on the log-odds scale. The intercept's inverse-logit is approximately \(\operatorname{logit}^{-1}(-0.733333)\approx0.324\); it is not the average of the three fitted probabilities. That average is approximately \((0.231475+0.331812+0.425557)/3=0.329615\). The mismatch occurs because applying a nonlinear inverse link does not commute with averaging.
Effect coding is useful when deviations from a level average are scientifically meaningful. The word “average” needs care: the coding above gives an unweighted average across the factor's levels. It is not automatically an average weighted by sample sizes, and it does not generally describe a population-average response.
Custom Contrasts: Planned Comparisons
A custom contrast directly represents a comparison chosen for scientific reasons. For levels control, A, and B, the weights \((-1,\tfrac12,\tfrac12)\) compare the average linear predictor of A and B with control. The weights \((0,-1,1)\) compare B with A. Both weight vectors sum to zero. Such comparisons can be calculated from fitted level predictors regardless of the coding used to fit the model, provided the quantities are estimable.
Worked Example: Coding for Two Planned Comparisons
For the same fitted linear predictors \((-1.2,-0.7,-0.3)\), calculate the average-active-treatment comparison with control and the B-versus-A comparison. Then give a coding in which the coefficients equal those comparisons.
Let \(w_1=(-1,\tfrac12,\tfrac12)^\mathsf{T}\) and \(w_2=(0,-1,1)^\mathsf{T}\), in the order control, A, B. The planned comparisons are \(w_1^\mathsf{T}\hat\eta=-(-1.2)+\tfrac12(-0.7)+\tfrac12(-0.3)=1.2-0.35-0.15=0.7\) and \(w_2^\mathsf{T}\hat\eta=-(-0.7)+(-0.3)=0.7-0.3=0.4\).
Use the coding matrix whose rows, in that same level order, are \((-2/3,0)\), \((1/3,-1/2)\), and \((1/3,1/2)\), and write \(\eta=\alpha\mathbf{1}+C\gamma\). The columns sum to zero and are linearly independent. Since \(w_1^\mathsf{T}C=(1,0)\), \(w_2^\mathsf{T}C=(0,1)\), and both contrasts annihilate \(\mathbf{1}\), the coefficients \(\gamma_1,\gamma_2\) equal the two planned contrasts. Here \(\hat\alpha\) is the mean linear predictor, \(\hat\gamma_1=0.7\), and \(\hat\gamma_2=0.4\). The resulting level predictors are control: \(-0.733333-(2/3)(0.7)=-1.2\); A: \(-0.733333+(1/3)(0.7)-(1/2)(0.4)=-0.7\); B: \(-0.733333+(1/3)(0.7)+(1/2)(0.4)=-0.3\).
Thus a planned comparison can be made a coefficient by choosing a suitable basis. The numerical coefficients do not change the fitted level predictors; they make the intended scientific quantities directly visible.
Why Equivalent Codings Give the Same Fit
The following result states when a change of contrast basis is only a change of coordinates. Consider a factor-only saturated linear-predictor model with \(L\) levels. Let \(\mathbf{1}\) denote the \(L\)-vector of ones, and let \(C\) be an \(L\)-by-\((L-1)\) matrix whose columns are linearly independent and each sum to zero. Then \(\eta=\alpha\mathbf{1}+C\gamma\) can represent any vector of \(L\) level-specific linear predictors.
Proof. The subspace of vectors whose entries sum to zero has dimension \(L-1\). The columns of \(C\) are \(L-1\) independent vectors in that subspace, so they form a basis for it. The same holds for \(C^*\). Consequently there is an invertible \((L-1)\)-by-\((L-1)\) matrix \(A\) such that \(C^*=CA\). For any \(\alpha,\gamma\), set \(\alpha^*=\alpha\) and \(\gamma^*=A^{-1}\gamma\). Then \[ \alpha^*\mathbf{1}+C^*\gamma^* =\alpha\mathbf{1}+CA A^{-1}\gamma =\alpha\mathbf{1}+C\gamma. \] The reverse mapping follows using \(A^{-1}\), so the sets of representable vectors are identical. Applying the same inverse link to an identical linear predictor gives identical fitted means. \(\square\)
In models with other predictors or interactions, the same principle applies when the entire design is reparameterized through an invertible transformation that preserves its column space. Recoding only one part of an interaction while leaving the rest inconsistent can instead change the model being represented. Also, when the design is rank deficient, not every proposed coefficient or contrast is separately identifiable; a contrast is estimable only if the observed design identifies that linear function.
Justification. Substitute the parameterization into the contrast: \(w^\mathsf{T}\eta=w^\mathsf{T}(\alpha\mathbf{1}+C\gamma)=\alpha w^\mathsf{T}\mathbf{1}+w^\mathsf{T}C\gamma=w^\mathsf{T}C\gamma\). For reference coding, write \(\eta_j=\alpha+\beta_j\) for a nonreference level and \(\eta_{\mathrm{ref}}=\alpha\). Subtraction gives \(\eta_j-\eta_{\mathrm{ref}}=\beta_j\). \(\square\)
Choosing a Coding That Fits the Question
| Scientific target | Useful coding | What a coefficient directly describes |
|---|---|---|
| Each treatment versus a designated control | Reference contrasts | Difference from control on the linear-predictor scale |
| Each level's departure from an unweighted level average | Sum-to-zero effect contrasts | Deviation from the average linear predictor |
| A particular pooled, ordered, or otherwise planned comparison | Custom contrasts | The selected linear combination, if parameterized to make it a coefficient |
With factor-by-covariate interactions, a contrast may depend on the covariate value. For example, a treatment difference in a model with treatment-specific slopes is evaluated at a specified covariate value; it is not a single unconditional difference. Likewise, in a nonlinear-link GLM, a contrast of linear predictors need not equal a difference in response means. State both the scale and the conditions of the comparison when reporting a result.
Common Mistake: Treating Coding as the Scientific Effect
- Wrong: “The coefficient is the treatment effect, so changing its coding changed the effect.” A coefficient is a coordinate-dependent parameter; its meaning depends on the contrast matrix and reference level.
- Correction: State the level-specific fitted quantities and the contrast of interest. If two codings span the same model space, compute that contrast from either representation; the resulting fitted comparison is the same.
- Wrong: “A logit coefficient of \(0.5\) means a probability increase of \(0.5\).” It is a difference in log odds.
- Correction: Convert each linear predictor through the inverse link to obtain fitted probabilities, then subtract those probabilities if a probability difference is the scientific target.
Check Your Understanding
For each question, identify the comparison or coding that best matches the stated target.
- A trial has placebo and three active doses, and the primary analysis compares each dose with placebo. Which coding makes these comparisons appear directly as coefficients?
- For four factor levels, what does a sum-to-zero effect coding make the intercept represent on the linear-predictor scale?
- With levels control, A, and B, write weights for comparing the average of A and B with control. Verify that the weights sum to zero.
- Why can two different contrast codings produce the same fitted probabilities in a logistic regression?
- In a model with a treatment-by-continuous-predictor interaction, why might a treatment contrast need to specify a value of the continuous predictor?