Tutorials › High-Dimensional Genomic Data Analysis › Worked Example With Three Conditions

Count-Based Differential Expression for RNA-seq · Tutorial 454 of 795

Worked Example With Three Conditions

Build a three-condition model matrix and work through planned contrasts to quantify gene-level differences and their uncertainty.

Intermediate 12 min read

What You'll Learn

  • Encode a three-condition RNA-seq design using a reference condition and indicator columns.
  • Translate a planned pairwise comparison into a contrast vector.
  • Calculate the contrast estimate and its standard error from coefficient estimates and their covariance.
  • Interpret a Wald test and confidence interval on both log and fold-change scales.
  • Recognize how batch confounding, model fit, and multiple testing affect conclusions.

From Three Conditions to a Planned Comparison

With three conditions, a gene can differ in several ways: one condition may differ from both others, or only one pair may differ. A useful analysis begins by stating the scientific comparison of interest, then encoding all three conditions in a model that can estimate it. The model uses the same negative binomial generalized linear model framework as other count-based differential-expression analyses; the emphasis here is on how the design matrix and a planned pairwise contrast work together.

For gene \(g\) in sample \(j\), let \(Y_{gj}\) be the observed count, \(s_j\) the sample’s normalization factor, and \(x_j\) the row of the design matrix. A standard model is

$$ Y_{gj}\sim \operatorname{NB}(\mu_{gj},\alpha_g),\qquad \log(\mu_{gj})=\log(s_j)+x_j^\mathsf{T}\beta_g,\qquad \operatorname{Var}(Y_{gj})=\mu_{gj}+\alpha_g\mu_{gj}^{2}. $$

Here \(\alpha_g\) is the gene-specific dispersion and \(\log(s_j)\) is an offset, not an estimated condition effect. The model assumes that counts are conditionally independent across biological samples given the model, that the negative binomial mean-variance relationship is adequate, and that the design includes the relevant systematic effects. The log link makes coefficients differences on the log-mean scale; exponentiating such a difference gives a mean ratio, or fold change, conditional on the model.

Design Matrix for Three Conditions

Let A be the reference condition, with B and C represented by indicator columns. In a model containing only condition, the design matrix has an intercept and two condition indicators. Its rows depend on each sample’s condition, not on its observed count.

Definition: Under reference coding for three conditions, the row for A is \((1,0,0)\), for B is \((1,1,0)\), and for C is \((1,0,1)\). The gene-specific coefficient vector is \(\beta_g=(\beta_{0g},\beta_{Bg},\beta_{Cg})^\mathsf{T}\). Thus \(\beta_{0g}\) is the log mean for A at offset zero, \(\beta_{Bg}\) is the log mean ratio B-to-A, and \(\beta_{Cg}\) is the log mean ratio C-to-A.

The phrase “at offset zero” describes the model’s normalized mean scale: for a sample with size factor \(s_j\), the expected count is \(s_j\) times the modeled normalized mean. The condition comparisons are unaffected by this common offset because the same factor is accounted for in each sample’s model.

Worked Example: Writing the Three-Condition Design Matrix

Suppose a hypothetical experiment has two biological replicates in each of conditions A, B, and C, with no additional covariates. In the sample order A1, A2, B1, B2, C1, C2, the design matrix is

$$ X= \begin{pmatrix} 1&0&0\\ 1&0&0\\ 1&1&0\\ 1&1&0\\ 1&0&1\\ 1&0&1 \end{pmatrix}, \qquad \beta_g= \begin{pmatrix} \beta_{0g}\\ \beta_{Bg}\\ \beta_{Cg} \end{pmatrix}. $$

The first column is the intercept; the second marks B samples; the third marks C samples. For an A sample the linear predictor apart from its offset is \(\beta_{0g}\). For a B sample it is \(\beta_{0g}+\beta_{Bg}\), and for a C sample it is \(\beta_{0g}+\beta_{Cg}\). Therefore the B-to-C log mean ratio is

$$ (\beta_{0g}+\beta_{Bg})-(\beta_{0g}+\beta_{Cg}) =\beta_{Bg}-\beta_{Cg}. $$

The intercept cancels. This is why a pairwise comparison must be expressed as a contrast of coefficients rather than read as one coefficient alone.

Planned Contrasts and Their Uncertainty

A contrast is a specified linear combination of model coefficients. For the B-to-C comparison under reference coding, define \(c=(0,1,-1)^\mathsf{T}\). The estimated log mean ratio is \(\hat{\theta}_g=c^\mathsf{T}\hat{\beta}_g=\hat{\beta}_{Bg}-\hat{\beta}_{Cg}\). Its standard error depends on the estimated covariance matrix of the coefficients, not just on their separate standard errors.

Key result: If \(\widehat{\operatorname{Cov}}(\hat{\beta}_g)\) is the estimated covariance matrix of the fitted coefficients, then the estimated variance and standard error of a contrast are

\(\widehat{\operatorname{Var}}(\hat{\theta}_g)=c^\mathsf{T}\widehat{\operatorname{Cov}}(\hat{\beta}_g)c,\qquad SE(\hat{\theta}_g)=\sqrt{c^\mathsf{T}\widehat{\operatorname{Cov}}(\hat{\beta}_g)c}.\)

For a planned two-sided Wald test of \(H_0:\theta_g=0\) against \(H_a:\theta_g\ne 0\), the statistic is \(z=\hat{\theta}_g/SE(\hat{\theta}_g)\). A large-sample normal reference distribution gives the approximate p-value. An approximate 95% confidence interval on the log scale is \(\hat{\theta}_g\pm1.96SE(\hat{\theta}_g)\). Exponentiating the estimate and both interval endpoints gives a fold-change estimate and interval. These approximations rely on the fitted coefficient estimates being sufficiently well behaved for the Wald approximation; small samples, weakly informed coefficients, or poor model fit can undermine that approximation.

Worked Example: Testing B Against C for One Gene

Consider a hypothetical gene with six samples, two per condition. The observed counts are A: 80 and 96, B: 120 and 144, and C: 150 and 180. Suppose the model fit, with the appropriate sample offsets and dispersion estimation, reports \(\hat{\beta}_{Bg}=0.4055\) and \(\hat{\beta}_{Cg}=0.6287\). These estimates correspond approximately to B-to-A and C-to-A mean ratios of \(1.50\) and \(1.88\), respectively. For the planned B-to-C contrast, the point estimate is

$$ \hat{\theta}_g=0.4055-0.6287=-0.2232. $$

Suppose the estimated coefficient variances are \(0.02501\) for both \(\hat{\beta}_{Bg}\) and \(\hat{\beta}_{Cg}\), and their estimated covariance is \(0.00750\). The contrast variance includes the covariance term:

$$ \widehat{\operatorname{Var}}(\hat{\theta}_g) =0.02501+0.02501-2(0.00750) =0.03502, \qquad SE(\hat{\theta}_g)=\sqrt{0.03502}=0.1871. $$

The Wald statistic is \(z=-0.2232/0.1871\approx-1.193\), giving an approximate two-sided p-value of \(0.233\). The data do not provide strong evidence against equal B and C means for this gene at a 0.05 significance level. This is not evidence that the means are identical.

Using the unrounded standard error from the contrast variance, the approximate 95% log-scale interval is

$$ -0.2232\pm1.96\sqrt{0.03502} \ \approx\ (-0.5900,\ 0.1436). $$

Exponentiating gives an estimated B-to-C mean ratio of \(\exp(-0.2232)\approx0.8000\), with an approximate 95% interval of \((0.5543,\ 1.1544)\). In context, the fitted mean for B is about 20% lower than for C, but the interval includes 1, so these data are compatible with either a lower or a higher B mean. The interval is an uncertainty range from the model and approximation; it is not a 95% probability statement about the fixed, unknown ratio.

Justifying the Contrast Calculation

The variance formula follows from the variance of a linear combination. Write the estimation error in the contrast as \(c^\mathsf{T}(\hat{\beta}_g-\beta_g)\). If the coefficient estimation error has covariance matrix \(V_g\), then for any fixed vector \(c\),

$$ \operatorname{Var}\{c^\mathsf{T}(\hat{\beta}_g-\beta_g)\} =c^\mathsf{T}\operatorname{Var}(\hat{\beta}_g-\beta_g)c =c^\mathsf{T}V_gc. $$

For \(c=(0,1,-1)^\mathsf{T}\), expanding this product gives \(\operatorname{Var}(\hat{\beta}_{Bg})+\operatorname{Var}(\hat{\beta}_{Cg})-2\operatorname{Cov}(\hat{\beta}_{Bg},\hat{\beta}_{Cg})\). Omitting the covariance would therefore generally give the wrong standard error. The sign matters: the covariance is subtracted twice for a difference and added twice for a sum.

Worked Example: Comparing C With the Geometric Mean of A and B

A different scientific question might ask whether C differs from the geometric mean of A and B on the normalized mean scale. On the log scale this is the contrast \(\theta_g=\beta_{Cg}-\tfrac12\beta_{Bg}\), with vector \(c=(0,-0.5,1)^\mathsf{T}\). Using the same hypothetical fitted estimates and covariance values, the estimate is

$$ \hat{\theta}_g=0.6287-\tfrac12(0.4055)=0.42595. $$

Its estimated variance is

$$ \widehat{\operatorname{Var}}(\hat{\theta}_g) =0.25(0.02501)+0.02501-0.00750 =0.0237625, \qquad SE=\sqrt{0.0237625}=0.1541. $$

Exponentiating the estimate gives \(\exp(0.42595)\approx1.531\). Thus the fitted normalized mean in C is about 1.53 times the geometric mean of the A and B normalized means. The approximate 95% log interval is \(0.42595\pm1.96(0.1541)\), or about \((0.1239,0.7280)\); exponentiation gives a ratio interval of approximately \((1.132,2.071)\). This comparison answers a different question from B versus C, illustrating why the contrast should be chosen before interpreting the output.

Adding Covariates, Diagnostics, and Alternatives

The same contrast logic applies when the design includes covariates. For example, if each condition is represented in both of two batches, a model can include an additional batch indicator. In sample order A-batch1, A-batch2, B-batch1, B-batch2, C-batch1, C-batch2, one suitable design matrix is

$$ X= \begin{pmatrix} 1&0&0&0\\ 1&0&0&1\\ 1&1&0&0\\ 1&1&0&1\\ 1&0&1&0\\ 1&0&1&1 \end{pmatrix}, $$

where the fourth column marks batch 2. In this additive model the adjusted B-to-C contrast is still \((0,1,-1,0)^\mathsf{T}\): the batch coefficient cancels. This requires the design to distinguish condition from batch. If, for example, all A samples occur in batch 1 and all B and C samples in batch 2, some condition and batch effects may be inseparable. A rank-deficient design cannot uniquely estimate all those coefficients; additional software output cannot repair a confounded experiment.

Check that the matrix encodes the actual sample labels and that each condition has biological replication. Inspect residual diagnostics and the dispersion-mean relationship for indications that the count model is inadequate, and investigate extreme observations rather than assuming they are harmless. A single influential sample can change a gene’s estimate or uncertainty substantially. Also verify that the contrast direction matches the question: reversing B-to-C to C-to-B reverses the log estimate and reciprocates the fold change, while leaving a two-sided test unchanged.

A planned pairwise contrast is not the same as an omnibus test of whether any of the three conditions differ. The pairwise test addresses one specified comparison; an omnibus test assesses a broader condition effect, typically using a joint test of the two condition coefficients. Separate pairwise tests can address several comparisons, but the number of tests and the multiplicity strategy must be considered. In RNA-seq, thousands of genes are typically tested, so gene-level p-values should not be treated as if only one hypothesis had been examined; adjusted p-values address that across-gene multiplicity. A contrast also cannot compensate for an inappropriate model, too few biological replicates, or poor experimental balance.

Common Pitfalls

  • Reading a coefficient as the desired comparison. With A as reference, \(\beta_{Bg}\) compares B with A, not B with C. Subtract \(\beta_{Cg}\) to obtain B versus C.
  • Ignoring coefficient covariance. The variance of a difference contains \(-2\operatorname{Cov}(\hat{\beta}_{Bg},\hat{\beta}_{Cg})\). Adding the two variances alone is generally incorrect.
  • Calling a log coefficient a fold change. A coefficient of \(-0.2232\) is a log mean ratio. Exponentiating it gives the ratio, about \(0.8000\).
  • Interpreting a non-significant result as equality. A p-value above the chosen significance level does not establish equal expression; assess the estimate and its uncertainty in context.
  • Using an unidentifiable design. If condition and batch are completely confounded, the model cannot separate their effects. Balance and replication are design requirements, not post-analysis options.
Key takeaway: Specify the three-condition design, state the planned contrast in coefficient terms, and calculate its uncertainty from the full coefficient covariance matrix. Interpret the exponentiated contrast as a conditional mean ratio, while checking model fit, design identifiability, and multiplicity.

Check Your Understanding

Answer each question, then compare with the short solutions below.

  1. With A as reference, what contrast gives the log mean ratio C-to-B?
  2. If \(\hat{\beta}_{Bg}=0.30\) and \(\hat{\beta}_{Cg}=0.10\), calculate the B-to-C log ratio and fold change.
  3. Why is the covariance needed when calculating the standard error of \(\hat{\beta}_{Bg}-\hat{\beta}_{Cg}\)?
  4. What happens to the interpretation of a condition effect if condition and batch are completely confounded?

Solutions. (1) Use \(c=(0,-1,1)^\mathsf{T}\), giving \(\hat{\beta}_{Cg}-\hat{\beta}_{Bg}\). (2) The B-to-C log ratio is \(0.30-0.10=0.20\); the fold change is \(\exp(0.20)\approx1.221\), so the fitted B mean is about 22.1% higher than C. (3) The variance of the difference is \(\operatorname{Var}(\hat{\beta}_{Bg})+\operatorname{Var}(\hat{\beta}_{Cg})-2\operatorname{Cov}(\hat{\beta}_{Bg},\hat{\beta}_{Cg})\). (4) The model cannot uniquely attribute the observed difference to condition rather than batch; the effects are not separately identifiable from those data.

Clinical Biostats