Why contrasts are needed in multi-group experiments
A negative binomial generalized linear model (GLM) can describe expression across several experimental groups while adjusting for library size and covariates. Its fitted coefficients depend on how the design matrix encodes those groups. A coefficient may compare one group with a reference group, but many useful comparisons are not single coefficients: comparing two non-reference groups requires a difference of coefficients, and comparing averages of several groups requires a weighted combination.
A contrast is that linear combination. Constructing it carefully specifies the comparison before calculating its estimate. In particular, the contrast must reflect the scientific comparison on the model's linear-predictor scale; a coefficient should not be interpreted as a group comparison merely because its label looks relevant.
Model and contrast definitions
For gene \(g\) in sample \(i\), let \(Y_{ig}\) be the observed read count and \(s_i\) the sample's positive library-size normalization factor. A standard count model uses a negative binomial distribution with mean \(\mu_{ig}\) and dispersion \(\phi_g\):
Here \(\mathbf{x}_i\) is the design row for sample \(i\), and \(\boldsymbol{\beta}_g\) is the coefficient vector for gene \(g\). The offset \(\log(s_i)\) adjusts the expected count for library size; it is not an additional coefficient to estimate. The mean \(\mu_{ig}/s_i\) is the modelled normalized mean count. The GLM uses the natural logarithm, so a difference in linear predictors is a natural-log ratio of normalized means.
In this tutorial, “log fold change” means the natural-log fold change unless marked as a base-2 log fold change. Thus, an estimated natural-log fold change \(\hat{\theta}\) corresponds to an estimated fold ratio \(\exp(\hat{\theta})\); the base-2 value is \(\hat{\theta}/\log(2)\). These are two scales for the same contrast, not different comparisons.
The contrast is meaningful under the fitted model: the negative binomial GLM and log link must be suitable for the counts, the design must represent the groups and covariates of interest, and the relevant coefficients must be estimable. For the examples below, assume independent biological samples conditional on the model covariates, an appropriate offset, and no group-by-covariate interaction unless one is explicitly included. An additive model gives the same group contrast at each fixed covariate value. With an interaction, the group difference can depend on that value, so a single contrast may not describe all samples.
Pairwise comparisons with reference-group coding
Suppose a factor has groups \(C\), \(A\), and \(B\), with \(C\) as the reference. Under treatment coding and with no other covariates, the group contributions to the log mean are \(\beta_0\) for \(C\), \(\beta_0+\beta_A\) for \(A\), and \(\beta_0+\beta_B\) for \(B\). The intercept is the reference-group log mean, while \(\beta_A\) and \(\beta_B\) are differences from that reference. Consequently, the comparison \(A\) versus \(B\) is not either coefficient alone: it is \(\beta_A-\beta_B\).
The offsets cancel in a comparison at the same library-size scale. More generally, comparing groups at the same covariate values cancels additive covariate terms as well. If the coefficient order is \((\beta_0,\beta_A,\beta_B)\), the contrast vector for \(A\) versus \(B\) is \((0,1,-1)\). Reversing the comparison reverses every sign.
Worked Example: Comparing Two Non-Reference Groups
Consider a hypothetical three-group experiment with \(C\) as the reference. For one gene, suppose the fitted coefficients are \(\hat{\beta}_0=2.10\), \(\hat{\beta}_A=0.40\), and \(\hat{\beta}_B=-0.15\). The coefficient order is intercept, \(A\), \(B\). Calculate the estimated contrast for \(A\) versus \(B\).
The appropriate contrast vector is \(c=(0,1,-1)^T\). Therefore,
The estimated natural-log fold change is \(0.55\). On the base-2 scale it is \(0.55/\log(2)=0.793\) (to three significant figures). Equivalently, the estimated normalized-mean ratio \(A/B\) is \(\exp(0.55)=1.73\). The intercept cancels, as it must for a comparison between these two non-reference groups.
Contrasts of averaged groups
A comparison can also average over multiple groups. The weights must state what “average” means. A common choice is an equally weighted average of group log means, compared with an equally weighted average of another set of group log means. If group log means are \(\eta_1,\ldots,\eta_K\), a contrast is \(\sum_{k=1}^{K}w_k\eta_k\), with weights summing to zero for a difference. Equal weights within each side give each group the same contribution, regardless of its sample size.
For example, with four group log means \(\eta_A,\eta_B,\eta_C,\eta_D\), the equally weighted average of \(A\) and \(B\) compared with the equally weighted average of \(C\) and \(D\) is \(\frac12\eta_A+\frac12\eta_B-\frac12\eta_C-\frac12\eta_D\). If the comparison is instead meant to weight groups according to sample counts, those weights define a different estimand; they should not be substituted implicitly.
Worked Example: Comparing Two Equally Weighted Group Averages
Suppose a hypothetical experiment has four groups, and the fitted adjusted log means for one gene, evaluated at the same covariate values, are \(\hat{\eta}_A=2.0\), \(\hat{\eta}_B=2.8\), \(\hat{\eta}_C=1.4\), and \(\hat{\eta}_D=2.2\). Calculate the equally weighted average of \(A,B\) minus the equally weighted average of \(C,D\).
The group weights are \(1/2,1/2,-1/2,-1/2\), which sum to zero. The estimated contrast is
Thus, the estimated natural-log fold change between the two equally weighted log-mean averages is \(0.60\), or \(0.60/\log(2)=0.866\) on the base-2 scale. Exponentiating gives a ratio of geometric means of \(\exp(0.60)=1.82\). This is not automatically the log ratio of the arithmetic means of the four groups' normalized means.
Building contrasts from a design matrix
The same group comparison can be represented using different coefficient parameterizations. For a design with a separate coefficient for each group, the coefficients directly represent group log means (possibly at a specified covariate value). A comparison is then formed by placing positive weights on the group or groups in the numerator and negative weights on those in the denominator. With reference coding, first express each group log mean as a sum of the applicable coefficients, then subtract the denominator expression from the numerator expression. This procedure avoids relying on coefficient names alone.
For example, state “\(B\) minus \(A\)” or “average of \(A,B\) minus average of \(C,D\).”
Use the actual coefficient coding, and hold any additive covariates at the same specified values.
The resulting coefficients are the contrast weights \(c\), in the same order as the fitted coefficient vector.
Calculate \(c^T\hat{\boldsymbol{\beta}}_g\), and convert to base 2 only if a base-2 log fold change is requested.
This construction follows directly from the log link. If two normalized expected means are \(m_1\) and \(m_2\), then \(\log(m_1)-\log(m_2)=\log(m_1/m_2)\). Because the fitted log means are linear combinations of coefficients, subtracting them gives the coefficient contrast for the log ratio. For weighted averages, linearity gives \(\sum_j w_j\hat{\eta}_j\), so collecting the coefficient terms yields the corresponding contrast vector. This justifies both the pairwise and averaged constructions.
Worked Example: An Adjusted Comparison with a Covariate
In a hypothetical experiment, the design uses an intercept, indicators for groups \(B\) and \(C\) (with \(A\) as reference), and a batch covariate. For one gene, suppose the coefficient estimates in that order are \((2.00,0.30,-0.20,0.50)\). The last coefficient is the batch effect. Calculate the adjusted comparison \(B\) versus \(C\) at the same batch value.
The log means at a fixed batch value \(z\) have the form \(\eta_A=2.00+0.50z\), \(\eta_B=2.00+0.30+0.50z\), and \(\eta_C=2.00-0.20+0.50z\). Subtracting \(C\) from \(B\) cancels the intercept and common batch term. In coefficient order, the contrast vector is \((0,1,-1,0)^T\).
The estimated adjusted natural-log fold change is \(0.50\), or \(0.50/\log(2)=0.721\) on the base-2 scale. It is the same at every batch value only because the model has an additive batch term and no group-by-batch interaction. If such an interaction were included, the group comparison would also involve the interaction coefficient and could depend on batch.
Checks, alternatives, and common failures
A contrast is a compact statement of a comparison, but a calculation can be numerically correct and still answer the wrong question. Before using an estimate, check that the coefficient order matches the contrast vector, the signs match the stated direction, and the weights match the intended group averaging. For an average-versus-average comparison, the positive and negative weights should each total one before subtraction. A contrast that is not estimable cannot be recovered by choosing different weights: for example, a group perfectly confounded with batch may not have a separately identifiable group effect.
A rank-deficient design, an empty group, or groups with no covariate overlap can make the requested comparison non-estimable or dependent on modelling assumptions. Check that the design contains the relevant group information and that its fitted coefficients support the requested contrast. If an interaction is present, write the group-specific predictors including the interaction before forming the contrast. If coefficients are reported under a different reference group, reconstruct the group predictors rather than assuming the coefficient names still encode the same comparison.
There are several ways to represent a comparison. A direct coefficient is convenient when the desired comparison is exactly the coefficient under the chosen coding. A linear contrast is more general and works without refitting merely to change the reference group. Releveling a factor can make a particular pairwise comparison appear as one coefficient, but the underlying fitted group means and resulting comparison are unchanged when the model is otherwise the same. An omnibus comparison of several groups answers a different question from a specified pairwise or averaged contrast; it does not supply the particular weighted log fold change calculated here.
The main limitation is that a contrast inherits the assumptions and scope of its GLM. It compares modelled normalized means, not raw counts, and its value depends on the specified groups, covariate conditions, and averaging weights. A contrast alone does not establish uncertainty or statistical significance. Those require inferential procedures beyond calculating the point estimate. In particular, do not turn an estimated log fold change into a claim about a causal effect without a design and assumptions that support causal interpretation.
- Wrong reasoning: “The \(B\) coefficient is the comparison of \(B\) with \(A\), because \(B\) is named in the coefficient.”
Correction: Under treatment coding, \(B\)'s coefficient compares \(B\) with the reference group, not with every other group. Subtract the relevant group predictors; for \(B\) versus \(A\), use \(\beta_B-\beta_A\) when \(A\) is not the reference. - Wrong reasoning: “The average of groups \(A\) and \(B\) means weighting each by its sample size.”
Correction: Equal weighting gives each group weight \(1/2\), independent of sample size. Sample-size weighting is a different comparison and must be explicitly chosen. - Wrong reasoning: “A coefficient contrast is always the same for every covariate value.”
Correction: This holds for an additive model without group-by-covariate interactions. With interactions, include their coefficients and specify the covariate value for the contrast.
Check Your Understanding
For each question, identify the contrast and calculate the estimate. Solutions follow so that the weighting and signs can be checked.
- With \(A\) as the reference, suppose \(\hat{\beta}_B=0.25\) and \(\hat{\beta}_C=-0.10\). What is the estimated natural-log fold change for \(B\) versus \(C\)?
- Four group log means are estimated as \(\hat{\eta}_A=1.5,\hat{\eta}_B=2.1,\hat{\eta}_C=1.0,\hat{\eta}_D=1.8\). Calculate the equally weighted average of \(A,B\) minus the equally weighted average of \(C,D\).
- In a model with intercept, \(B\) indicator, and batch coefficient, the estimates are \(1.7,0.4,0.6\). Give the contrast vector for \(B\) versus the reference group at the same batch value, and calculate its estimate.
- In a treatment-coded model, a student reports \(\hat{\beta}_B=0.3\) as the estimate for \(B\) versus \(C\), where \(A\) is the reference and \(\hat{\beta}_C=-0.2\). Correct the comparison and calculate it.
Solutions. (1) The contrast is \(\hat{\beta}_B-\hat{\beta}_C=0.25-(-0.10)=0.35\). (2) The weights are \((1/2,1/2,-1/2,-1/2)\), giving \(0.75+1.05-0.50-0.90=0.40\). (3) In coefficient order the vector is \((0,1,0)\), so the estimate is \(0(1.7)+1(0.4)+0(0.6)=0.40\); the batch term cancels in the same-value comparison. (4) The correct contrast is \(\hat{\beta}_B-\hat{\beta}_C=0.3-(-0.2)=0.50\), not \(\hat{\beta}_B\) alone.