Planned Comparisons as Weighted Differences
A factor with several levels can support many possible comparisons. A study might ask whether level D differs from level A, whether level B differs from the average of A and C, or whether the average response for two active treatments differs from that for two control treatments. A planned comparison turns the question into weights on the factor levels. Those weights determine a linear combination of fitted factor coefficients.
In a generalized linear model (GLM), the direct linear comparison is ordinarily made among the level-specific linear predictors. Its interpretation depends on the link: with an identity link it is a difference in means; with a logit link it is a log odds ratio; with a log link it is a log ratio of means. In particular, a contrast on the linear-predictor scale is not generally a difference between response-scale means.
To compare the average linear predictor in a set \(A\) with the average in a set \(B\), assign weight \(1/|A|\) to each level in \(A\) and weight \(-1/|B|\) to each level in \(B\). If the sets overlap, add the weights for any shared level. The resulting weights sum to \(1-1=0\), so they define a contrast. More generally, a planned comparison of two weighted averages uses weights summing to one within each average, with the second average subtracted from the first.
From Comparison Weights to Effect-Coded Coefficients
Use the following effect-coding convention for a factor with \(K\) levels. Level \(K\) is the omitted column pattern: the first \(K-1\) levels have a \(1\) in their respective factor column and zeros in the other factor columns, while level \(K\) has \(-1\) in every factor column. Write the intercept as \(\beta_0\) and the \(K-1\) factor coefficients as \(\beta_1,\ldots,\beta_{K-1}\). Then the level-specific linear predictors are
The level deviations from \(\beta_0\) sum to zero, so \(\beta_0\) is the unweighted average of the \(K\) level-specific linear predictors in this factor-only parameterization. This convention makes it possible to translate any planned comparison directly into the fitted factor coefficients.
Proof. Substitute the expressions for the level predictors into the definition of the contrast:
Because \(c\) is a contrast, \(\sum_{j=1}^K c_j=0\). The intercept term is therefore zero, leaving the stated expression. \(\square\)
The coefficient multiplying \(\beta_j\) is not always just the weight \(c_j\). It is \(c_j-c_K\), because the omitted level's linear predictor contains the negative sum of all the displayed factor coefficients. If the planned comparison is specified in terms of the levels, first write its full \(K\)-entry weight vector; then apply this formula.
Worked Example: One Level Versus the Average of Two Others
Suppose a Gaussian GLM with identity link has four factor levels A, B, C, and D, coded as above. The fitted coefficients are \(\hat\beta_0=10\), \(\hat\beta_A=-2\), \(\hat\beta_B=1\), and \(\hat\beta_C=3\), with D as the omitted level. Compare D with the average of A and C.
The implied effect for D is \(\hat\beta_D=-(-2+1+3)=-2\). The four fitted linear predictors, which are also the fitted means under the identity link, are therefore
Put weight \(-1/2\) on A, \(-1/2\) on C, and \(1\) on D. In the order A, B, C, D, the contrast vector is \(c=(-1/2,0,-1/2,1)\), whose entries sum to zero. The fitted contrast is
The coefficient formula gives the same answer. Here \(c_D=1\), so the coefficients on \(\hat\beta_A,\hat\beta_B,\hat\beta_C\) are \(-1.5,-1,-1.5\), respectively:
Because the link is the identity, this is a fitted-mean difference: the fitted mean for D is 2.5 units below the average fitted mean for A and C.
Interpreting the Contrast Under Different Links
Let \(\mu_j\) denote the mean response at level \(j\), and let \(g\) be the GLM link function, so \(\eta_j=g(\mu_j)\). A contrast \(c^T\eta\) compares the transformed means. The link determines what that transformed comparison represents. The following derivations apply directly to a factor-only model; in models with additional terms, the level-specific predictors are understood at the same fixed values of those other predictors.
For the log-link statement, use \(\eta_j=\log(\mu_j)\) and the logarithm rule \(\sum_j c_j\log(\mu_j)=\log(\prod_j\mu_j^{c_j})\), for positive means \(\mu_j\). For example, equal weights within \(A\) produce \(\frac{1}{|A|}\sum_{j\in A}\log(\mu_j)=\log((\prod_{j\in A}\mu_j)^{1/|A|})\), the log of the geometric mean in \(A\). Subtracting the corresponding expression for \(B\) gives the log ratio of those geometric means. \(\square\)
For a logit link, \(\eta_j=\log(\mu_j/(1-\mu_j))\), where \(\mu_j\) is a probability strictly between zero and one. A difference \(\eta_r-\eta_s\) is the log odds ratio comparing levels \(r\) and \(s\). More complex contrast weights give weighted combinations of log odds, not generally differences of probabilities.
Worked Example: A Planned Comparison in a Logistic GLM
Consider a four-level factor in a logistic GLM, with fitted coefficients \(\hat\beta_0=-0.4\), \(\hat\beta_A=0.6\), \(\hat\beta_B=-0.2\), and \(\hat\beta_C=0.3\); D is the omitted level. Plan to compare B with the average of A and C on the linear-predictor scale.
The level predictors are \(\hat\eta_A=0.2\), \(\hat\eta_B=-0.6\), \(\hat\eta_C=-0.1\), and \(\hat\eta_D=-1.1\). The planned weights in the order A, B, C, D are \((-1/2,1,-1/2,0)\), so
Using effect-coded coefficients gives \((-1/2)\hat\beta_A+\hat\beta_B+(-1/2)\hat\beta_C=-0.3-0.2-0.15=-0.65\), as required. The inverse-logit maps each predictor to a probability; the odds corresponding to predictor \(\eta\) are \(\exp(\eta)\). Thus \(\exp(-0.65)\approx0.522\) is the odds for B divided by the geometric mean of the odds for A and C. It is not a difference in probabilities.
For comparison, the fitted probabilities are approximately \(0.5498\) for A, \(0.3543\) for B, and \(0.4750\) for C. The probability for B minus the average probability for A and C is \(0.3543-(0.5498+0.4750)/2\approx-0.1581\), not \(-0.65\). These are different quantities on different scales.
Uncertainty for a Fitted Planned Comparison
A planned comparison is a linear function of the fitted coefficients, so its uncertainty must account for the covariance among those coefficients. Let \(\hat\beta=(\hat\beta_1,\ldots,\hat\beta_{K-1})^T\), and let \(a_j=c_j-c_K\) for \(j=1,\ldots,K-1\). Then \(\hat C=a^T\hat\beta\). If the estimated coefficient covariance matrix is \(V\), the estimated variance of this linear combination is \(a^T V a\); its standard error is the square root.
This follows by expanding the variance of a linear combination. Provided the coefficients have finite second moments, write \(V_{rs}=\operatorname{Cov}(\hat\beta_r,\hat\beta_s)\). Then
In practice, \(V\) is the covariance matrix estimated for the fitted GLM coefficients. The resulting standard error is used with the appropriate inferential procedure; a normal or Wald approximation is an approximation, not an automatic exact distributional claim.
Worked Example: Calculating a Contrast Standard Error
Suppose a planned comparison has already been expressed as \(\hat C=\hat\beta_1-\hat\beta_3\), with estimated coefficient variances \(\widehat{\operatorname{Var}}(\hat\beta_1)=0.09\) and \(\widehat{\operatorname{Var}}(\hat\beta_3)=0.16\), and estimated covariance \(\widehat{\operatorname{Cov}}(\hat\beta_1,\hat\beta_3)=0.03\). Its estimated variance is
Ignoring the covariance would give \(\sqrt{0.09+0.16}=0.5\), which is different. The sign and size of the covariance matter because the uncertainty is for a difference of estimates, not for either estimate by itself.
Common Mistakes and a Practical Construction
- Using weights that do not sum to zero. A comparison of two averages uses weights summing to one for the first average and minus one for the second. The resulting contrast sums to zero.
- Reading a contrast as a probability difference under a logit link. A contrast of linear predictors is on the log-odds scale. Apply the inverse link to obtain fitted probabilities, then calculate any desired probability-scale comparison separately.
- Using only the displayed coefficient weights. With the effect-coding convention here, the coefficient for level \(j<K\) is \(c_j-c_K\), not necessarily \(c_j\). The omitted level contributes through all the displayed coefficients.
- Computing a standard error from separate standard errors alone. A linear combination's variance includes covariance terms. Use the full estimated covariance matrix.
A reliable construction is to write down the levels in a fixed order, translate the scientific question into signed weights, verify that their sum is zero, and only then translate those weights into coefficient multipliers. Finally, state the scale of the result: mean, log odds, log mean, or another link-scale quantity. This last step prevents a mathematically correct contrast from receiving the wrong scientific interpretation.
State which level or average is subtracted from which other level or average.
Give the first quantity positive weights and the subtracted quantity negative weights; combine weights if a level occurs in both.
Verify that the weights sum to zero, then use \(c_j-c_K\) as the multiplier of each effect-coded factor coefficient.
Use the GLM link to describe the fitted contrast, and use the coefficient covariance matrix if a standard error is needed.
Check Your Understanding
For each question, write the planned weights before attempting any coefficient calculation.
- For levels A, B, C, and D, write a contrast vector comparing the average of A and B with the average of C and D.
- Under the effect-coding convention in this tutorial, what coefficient multiplies \(\beta_2\) in the contrast if the level weights are \(c_1,\ldots,c_K\)?
- In a log-link GLM, how would you interpret a contrast that compares the average log mean for two levels with the average log mean for three other levels?
- Why is a logit-scale contrast of \(-0.5\) not, by itself, a probability difference of \(-0.5\)?
- If two coefficient estimates have positive covariance, how does that covariance enter the variance of their difference?