Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Designs, linear models and contrasts

The design

A Design joins the sample table, the fixed effects as an R formula, optional random effects in lme4’s notation and named contrasts. The formula follows the notation of Wilkinson & Rogers (1973) as R reads it Chambers & Hastie, 1992: ~ condition + batch adds terms, a:b is an interaction, a * b is a + b + a:b, and ~ 0 + group has no intercept. model_matrix codes it as R’s model.matrix does: a numeric column is a covariate, a text or boolean column is a factor coded by treatment contrasts, indicators of every level but the first.

Columns are named as R names them: the intercept (Intercept) (INTERCEPT), a level after its factor (conditionstim), interactions joined by :, and names that are not syntactic in R between backticks (quote_name). parse_formula expands a formula into its terms (FormulaTerms), parse_random_effects reads random effects (RandomEffect), and model_matrix returns the matrix with its column names (ModelMatrix).

The first level of a factor is the reference, and every coefficient of that factor is a difference from it. Without levels, the levels of a text column are sorted by code point, which is R’s order only in the C locale; pass levels={"condition": ["ctrl", "stim"]} whenever the reference matters, which is almost always.

One model for every gene

Every gene gg of a samples x genes matrix of log-expression values is fitted with the same design matrix XX (limma’s lmFit; Smyth (2004)):

E(yg)=Xag,var⁡(yg)=σg2 Wg−1/2R Wg−1/2,E(y_g) = X a_g, \qquad \operatorname{var}(y_g) = \sigma_g^2\, W_g^{-1/2} R\, W_g^{-1/2},

where WgW_g holds the gene’s precision weights (from voom, or sample weights) and RR is the identity, or the correlation matrix of a known correlation between the samples of a block Smyth et al., 2005. fit_linear_model returns a LinearModelFit with, for every gene, the coefficients a^g\hat a_g, the residual variance sg2s_g^2 on dgd_g residual degrees of freedom, and the unscaled standard errors ugku_{gk}, so that the standard error of coefficient kk is ugksgu_{gk} s_g. A gene with missing values, or with weights of its own, is fitted to its observed samples by a QR decomposition of its own Golub & Van Loan, 2013; missing values and zero weights lower its dgd_g.

Contrasts

A contrast is a combination cc of the coefficients, such as the difference between two groups. make_contrasts reads named expressions such as "groupB - groupA" or "(groupB + groupC)/2 - groupA" written in the design’s column names, as limma’s makeContrasts does Ritchie et al., 2015, into Contrasts, a mapping of contrast names to weights; Contrasts.select takes some of them by name, and Design.with_contrasts returns the design with other contrasts. contrasts_fit re-expresses each gene’s fit: the estimate is c′a^gc' \hat a_g and its unscaled variance c′Vgcc' V_g c, with VgV_g the gene’s unscaled covariance matrix. Contrasts are matched to the coefficients by name, never by position.

With weights that differ between genes, limma’s contrasts.fit approximates the standard errors of contrasts of correlated coefficients from the covariance of the unweighted design. CytePool computes them exactly by default; method="limma" reproduces the approximation.

Designs that cannot answer the question

A contrast is estimable when it is a combination of the rows of XX, that is, when it is orthogonal to the null space of XX Searle, 1971. In a design of full rank every contrast is. Rank deficiency arises when a column is a combination of others, decided as R’s qr() decides it: for example when two factors group the samples alike, or, within one cell type, when every sample of a donor was left out. limma_voom then fits the column space of the group’s design and tests only the contrasts its samples can estimate, reporting the others.

Design.check reports such problems before anything is fitted, as a DesignReport of DesignIssue entries (DesignReport.kinds lists their kinds), each with what to do: aliased columns, no residual degrees of freedom, confounded factors, empty levels, levels with one sample, samples fitted exactly (leverage 1, the diagonal of the hat matrix; Hoaglin & Welsch (1978)), numeric codes that look like batches, labels that look like missing values, contrasts that cannot be estimated, and random effects whose variances cannot be estimated.

References
  1. Wilkinson, G. N., & Rogers, C. E. (1973). Symbolic description of factorial models for analysis of variance. Journal of the Royal Statistical Society: Series C (Applied Statistics), 22(3), 392–399. 10.2307/2346786
  2. Chambers, J. M., & Hastie, T. J. (1992). Statistical models. In J. M. Chambers & T. J. Hastie (Eds.), Statistical Models in S. Wadsworth & Brooks/Cole.
  3. Smyth, G. K. (2004). Linear models and empirical Bayes methods for assessing differential expression in microarray experiments. Statistical Applications in Genetics and Molecular Biology, 3(1), Article 3. 10.2202/1544-6115.1027
  4. Smyth, G. K., Michaud, J., & Scott, H. S. (2005). Use of within-array replicate spots for assessing differential expression in microarray experiments. Bioinformatics, 21(9), 2067–2075. 10.1093/bioinformatics/bti270
  5. Golub, G. H., & Van Loan, C. F. (2013). Matrix Computations (4th ed.). Johns Hopkins University Press.
  6. Ritchie, M. E., Phipson, B., Wu, D., Hu, Y., Law, C. W., Shi, W., & Smyth, G. K. (2015). limma powers differential expression analyses for RNA-sequencing and microarray studies. Nucleic Acids Research, 43(7), e47. 10.1093/nar/gkv007
  7. Searle, S. R. (1971). Linear Models. Wiley.
  8. Hoaglin, D. C., & Welsch, R. E. (1978). The hat matrix in regression and ANOVA. The American Statistician, 32(1), 17–22. 10.1080/00031305.1978.10479237