Exercise: design matrices for covariates
Goal
By the end of this exercise you should be able to:
- write a model formula for a research question and say what each term does,
- build the design matrix \(X\) by hand for the common cases: one factor; a factor plus a number (age, BMI); two factors; a factor with three levels (batch, time point); an interaction column,
- compute \(X\boldsymbol\beta\) by hand to get each sample’s or group’s predicted mean, and go back from group means to \(\boldsymbol\beta\),
- say what each coefficient means (difference from the reference; slope per unit),
- decide whether \(\boldsymbol\beta\) has a unique solution, find the dependent columns, and say what to change.
The By hand section is the core of this exercise; the R section only confirms your answers. What “adjusting” for a covariate buys you, and Simpson’s paradox, are in Confounding & Simpson’s paradox. limma is in Many genes at once: limma, DESeq2 in Bulk RNA-seq with DESeq2.
Background
A model formula is a recipe for a design matrix. The model is
\[ \mathbf{y} = X\boldsymbol{\beta} + \boldsymbol{\varepsilon}, \qquad \hat y_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \dots \]
\(X\) has one row per sample and one column per coefficient. With one predictor this is the line \(y = a + bx\) from Fitting equations, with \(\beta_0 = a\) and \(\beta_1 = b\).
Building the columns of \(X\):
- Intercept — a column of all 1’s (present by default). Its coefficient \(\beta_0\) is the predicted outcome when every number is 0 and every factor is at its reference level.
- Continuous predictor (age, BMI, hours) — the raw numbers, one column. Coefficient = change in \(\hat y\) per unit.
- Categorical predictor (a factor with \(k\) levels) — \(k-1\) dummy (0/1) columns under the default treatment contrasts. The omitted level is the reference, folded into the intercept; each dummy’s coefficient = that level minus the reference.
- Interaction
a:b— the product of the columns foraandb, entry by entry.a * bis short fora + b + a:b.
Predictions. \(X\boldsymbol\beta\) is a matrix times a vector: row \(i\) of \(X\) dotted with \(\boldsymbol\beta\) gives \(\hat y_i\) (see Exercise: matrix × vector). Samples with identical rows get the same prediction, so with only factors in the model \(X\boldsymbol\beta\) lists one predicted mean per group.
Reference levels. R orders factor levels alphabetically, as text, so the alphabetically first level is the reference (and "24h" sorts before "6h"). Change it with relevel or factor(x, levels = ...).
Existence of solutions. Fitting means solving \(\mathbf y \approx X\boldsymbol\beta\) by least squares. As in Linear systems in R, there is one best \(\boldsymbol\beta\) exactly when the columns of \(X\) are linearly independent (no column is a sum or multiple of others; qr(X)$rank equals the number of columns \(p\)). That needs \(n \ge p\). If the columns are dependent, infinitely many \(\boldsymbol\beta\) give the same fitted values. Typical causes: a dummy for every level plus an intercept (dummy-variable trap), a batch that coincides with the treatment, an interaction with an empty cell, more columns than samples.
By hand
B1. (formulas) Write model formulas (in y ~ ... form) for each question: (a) does expression depend on treatment alone? (b) does expression depend on treatment, with age also in the model? (c) does expression depend on treatment and sex, allowing the treatment effect to differ between the sexes?
B2. (treatment + age) You have 4 mice:
| sample | treatment | age (weeks) |
|---|---|---|
| 1 | ctrl | 10 |
| 2 | treat | 40 |
| 3 | ctrl | 20 |
| 4 | treat | 30 |
Write out the design matrix \(X\) for expr ~ treatment + age by hand (treatment coding, ctrl as reference). Label the columns.
B3. (\(X\boldsymbol\beta\)) For the model in B2 the fitted coefficients are \(\beta_0 = 5\), \(\beta_1 = 3\) (treatmenttreat), \(\beta_2 = 0.1\) (age). (a) Compute \(X\boldsymbol\beta\), the predicted expression of all four mice. (b) What is the predicted difference between a treated and a control mouse of the same age? (c) What does \(\beta_0 = 5\) correspond to, and why is it not very meaningful here?
B4. (sex + BMI) Four patients:
| patient | sex | BMI |
|---|---|---|
| 1 | F | 20 |
| 2 | M | 25 |
| 3 | F | 30 |
| 4 | M | 35 |
- Write \(X\) for
expr ~ sex + bmi(F is the reference). - With \(\boldsymbol\beta = (2,\ 1,\ 0.2)\) for (
(Intercept),sexM,bmi), compute the predicted expression of each patient. - In words, what do
sexM\(= 1\) andbmi\(= 0.2\) mean? By how much does the prediction differ between two women with BMI 20 and BMI 30? - You centre BMI at 25 (
I(bmi - 25)). What is the new intercept, and what does it mean? Does thebmicoefficient change?
B5. (a factor with three levels) Samples were sequenced in three batches, A, B, C. For expr ~ batch: (a) how many dummy columns does \(X\) have (besides the intercept), and what are they called? (b) which level is the reference under the default coding? (c) the intercept is 2, batchB is 4 and batchC is \(-1\). What is the predicted mean in each batch? (d) what is the difference C − B? (e) in another gene the batch means are A: 5, B: 8, C: 4. Find \(\boldsymbol\beta\).
B6. (two factors: batch + treatment) A design with two sequencing batches (A, B) and two conditions (ctrl, treat), with every combination present. (a) Write the four distinct rows of \(X\) for expr ~ batch + treatment, one per group, with columns ((Intercept), batchB, treatmenttreat). (b) With \(\boldsymbol\beta = (8,\ -2,\ 3)\), compute the predicted mean of each of the four groups. (c) What is the treatment effect in batch A? In batch B? (d) In a different gene the observed group means are A/ctrl 8, A/treat 11, B/ctrl 6, B/treat 10. Can expr ~ batch + treatment reproduce all four means exactly? Why or why not?
B7. (time points) Expression was measured at 0 h, 6 h and 24 h after stimulation, two samples per time point. (a) factor(time) gives the levels "0h" "24h" "6h". Why this order? Write the line that puts them in the right order. (b) With the order fixed, write \(X\) for expr ~ time (six rows). (c) With \(\boldsymbol\beta = (4,\ 6,\ 3)\) for ((Intercept), time6h, time24h), what is the predicted mean at each time point? (d) If you instead use time as a number (expr ~ hours, hours = 0, 6, 24), how many columns does \(X\) have? Can that model reproduce the means from (c)? Explain in one sentence.
B8. (interaction column) Four groups: F/ctrl, F/treat, M/ctrl, M/treat. (a) Write the four rows of \(X\) for expr ~ treatment * sex, with columns ((Intercept), treatmenttreat, sexM, treatmenttreat:sexM). Show how the last column is computed. (b) With \(\boldsymbol\beta = (4,\ 1,\ 2,\ 3)\), compute the four group means. What is the treat − ctrl difference in females? In males? (c) In another gene the group means are F/ctrl 6, F/treat 9, M/ctrl 7, M/treat 12. Find all four coefficients.
B9. (counting columns) A study uses expr ~ sex + batch + age + treatment, where batch has levels A, B, C. (a) How many columns \(p\) does \(X\) have? List them in order. (b) Write the row of \(X\) for a treated female, aged 50, sequenced in batch B. (c) What is the smallest number of samples for which \(\boldsymbol\beta\) can possibly be unique?
B10. (reference level) Your treatment factor has levels drug and placebo. You fit expr ~ treatment and get a coefficient named treatmentplacebo. (a) which level did R choose as the reference, and why? (b) the coefficient treatmentplacebo is \(-4\). Interpret it. Is the drug raising or lowering expression relative to placebo? (c) how would you make placebo the reference so the coefficient reads as the drug effect?
B11. (unique solution? — the dummy-variable trap) A student builds, for four samples F, M, F, M, a design matrix with three columns: an intercept, sexF (1 for females) and sexM (1 for males). (a) Write \(X\). Which combination of columns is linearly dependent? (b) The mean in females is 6 and in males is 9. Show that \(\boldsymbol\beta = (6,\ 0,\ 3)\) and \(\boldsymbol\beta = (9,\ -3,\ 0)\) give the same fitted values. Give a third \(\boldsymbol\beta\) that does too. (c) Is \(\boldsymbol\beta\) unique? What should the student change?
B12. (unique solution? — batch and treatment) Two possible layouts for an RNA-seq experiment with four samples:
| sample | Design 1 | Design 2 |
|---|---|---|
| 1 | ctrl, batch A | ctrl, batch A |
| 2 | ctrl, batch A | treat, batch A |
| 3 | treat, batch B | ctrl, batch B |
| 4 | treat, batch B | treat, batch B |
- Write \(X\) for
expr ~ batch + treatmentfor both designs. - For which design is \(\boldsymbol\beta\) unique? Point to the dependent columns in the other one.
- In Design 1 the observed means are 5 (samples 1–2) and 8 (samples 3–4). Give two different \(\boldsymbol\beta\) that fit equally well. What can you conclude about the drug?
- What would you tell the person planning Design 1?
B13. (unique solution? — quick-fire) For each design, say whether \(\boldsymbol\beta\) is unique. If not, name the cause and what to change. (a) expr ~ treatment * sex with six samples: F/ctrl, F/ctrl, F/treat, F/treat, M/ctrl, M/ctrl. (b) expr ~ treatment + sex + batch + age + bmi (batch A/B/C) with 5 patients. (c) expr ~ treatment + age with six mice, three per group, all of different ages. (d) expr ~ time with time a factor with levels 0h, 6h, 24h, but samples were only collected at 0 h and 6 h.
In R
Use R to confirm the by-hand answers. model.matrix shows the \(X\) a formula builds and %*% multiplies it by \(\boldsymbol\beta\).
See the design matrix a formula builds
treatment <- factor(c("ctrl","treat","ctrl","treat"))
age <- c(10, 40, 20, 30)
X <- model.matrix(~ treatment + age)
X
# (Intercept) treatmenttreat age
# 1 1 0 10
# 2 1 1 40
# 3 1 0 20
# 4 1 1 30
X %*% c(5, 3, 0.1) # predicted expression, one per mouse
# [,1]
# 1 6
# 2 12
# 3 7
# 4 11Factors with more levels, and level order
batch <- factor(c("A","B","C"))
model.matrix(~ batch)
# (Intercept) batchB batchC
# 1 1 0 0 # A = reference (all dummies 0)
# 2 1 1 0
# 3 1 0 1
time <- c("0h","0h","6h","6h","24h","24h")
levels(factor(time)) # "0h" "24h" "6h" -- alphabetical, as text
time <- factor(time, levels = c("0h","6h","24h"))
colnames(model.matrix(~ time)) # "(Intercept)" "time6h" "time24h"An interaction is a product column
treatment <- factor(c("ctrl","treat","ctrl","treat"))
sex <- factor(c("F","F","M","M"))
model.matrix(~ treatment * sex)
# (Intercept) treatmenttreat sexM treatmenttreat:sexM
# 1 1 0 0 0
# 2 1 1 0 0
# 3 1 0 1 0
# 4 1 1 1 1Checking for a unique solution
batch <- factor(c("A","A","B","B"))
treatment <- factor(c("ctrl","ctrl","treat","treat")) # Design 1 of B12
X <- model.matrix(~ batch + treatment)
ncol(X) # 3 columns
qr(X)$rank # 2 -> columns are dependent, beta is not unique
y <- c(5, 5, 8, 8)
coef(lm(y ~ batch + treatment))
# (Intercept) batchB treatmenttreat
# 5 3 NAqr(X)$rank smaller than ncol(X) means dependent columns. lm then reports NA for a coefficient it could not separate.
Tasks
R1. Reproduce \(X\) from B2 with model.matrix(~ treatment + age) and the predictions from B3 with X %*% c(5, 3, 0.1).
R2. For B4, build sex and bmi, run model.matrix(~ sex + bmi), and check your predictions with %*%. Then refit with I(bmi - 25) and look at the new column.
R3. For B5 and B7, check the dummy columns and the reference level with model.matrix(), and check (c) of each with %*%.
R4. For B8, check the product column with model.matrix(~ treatment * sex) and your group means with %*% c(4, 1, 2, 3).
R5. For B10, build treatment <- factor(c("drug","placebo","drug","placebo")), check levels(), then relevel() to make placebo the reference and re-check levels().
R6. For B11–B13, build each \(X\) (for B11 by hand with cbind(1, sexF, sexM); the others with model.matrix()), compare qr(X)$rank with ncol(X), and confirm your verdicts.
Hints
+ means “also include this term as a predictor” (not arithmetic). y ~ a + b fits an additive model; y ~ a * b adds the interaction a:b on top of the two main effects. There is always an intercept unless you write y ~ 0 + .... See formula.
Start with a column of 1’s (the intercept). Numbers go in as their raw values. For a factor under the default treatment contrasts, add one 0/1 column per non-reference level: 1 when the sample has that level, 0 otherwise. For an interaction, multiply the two dummy columns row by row. Columns come in the order of the formula. Check with model.matrix.
Take one row of \(X\), multiply each entry by the matching entry of \(\boldsymbol\beta\), and add up. A treated mouse aged 40 in B3 gives \(5\cdot 1 + 3\cdot 1 + 0.1\cdot 40\). The same rule as in Exercise: matrix × vector.
The reference group has all dummies 0, so its mean is \(\beta_0\). A group that differs from the reference in one dummy has mean $_0 + $ that coefficient. With an interaction, the group with both dummies 1 has mean \(\beta_0 + \beta_1 + \beta_2 + \beta_3\); solve for \(\beta_3\) last.
factor() sorts the distinct values as text, character by character: "2" comes before "6", so "24h" comes before "6h"; "d" comes before "p", so drug comes before placebo. Give the order with factor(x, levels = ...) or pick the reference with relevel.
Look for a column that equals another column, is a sum of other columns, or is all zeros. Count columns \(p\) and samples \(n\): if \(p > n\) the columns must be dependent. If you find a dependence, you can move some amount between coefficients without changing \(X\boldsymbol\beta\) — that is Case 3 (infinitely many solutions) of Linear systems in R. In R, compare qr(X)$rank with ncol(X).
Solutions
B1. (a) expr ~ treatment (b) expr ~ treatment + age (c) expr ~ treatment * sex (equivalently expr ~ treatment + sex + treatment:sex).
B2. Intercept column of 1’s; a single treatment dummy (treatmenttreat = 1 for treated, 0 for control); and the raw age column.
\[ X = \begin{pmatrix} 1 & 0 & 10 \\ 1 & 1 & 40 \\ 1 & 0 & 20 \\ 1 & 1 & 30 \end{pmatrix} \quad \begin{array}{l} \text{col 1: (Intercept)} \\ \text{col 2: treatmenttreat} \\ \text{col 3: age} \end{array} \]
B3. (a) \(X\boldsymbol\beta = (5 + 0 + 1,\ 5 + 3 + 4,\ 5 + 0 + 2,\ 5 + 3 + 3) = (6,\ 12,\ 7,\ 11)\). (b) \(\beta_1 = 3\): at any fixed age the treated prediction is 3 higher (the age term is the same for both and cancels). (c) \(\beta_0 = 5\) is the predicted expression for a control mouse of age 0. That is outside the data (a newborn, or an extrapolation), so it is not scientifically meaningful. Centring age would move the intercept to a typical age.
B4. (a) \[
X = \begin{pmatrix}
1 & 0 & 20 \\
1 & 1 & 25 \\
1 & 0 & 30 \\
1 & 1 & 35
\end{pmatrix}
\quad (\text{(Intercept)},\ \text{sexM},\ \text{bmi})
\] (b) \(2 + 0 + 4 = 6\); \(\ 2 + 1 + 5 = 8\); \(\ 2 + 0 + 6 = 8\); \(\ 2 + 1 + 7 = 10\). (c) sexM \(= 1\): men are predicted 1 unit higher than women with the same BMI. bmi \(= 0.2\): each extra BMI unit adds 0.2, in both sexes. BMI 20 vs 30 is 10 units, so the difference is \(0.2 \times 10 = 2\) (patients 1 and 3: 6 vs 8). (d) New intercept \(= 2 + 0.2 \times 25 = 7\): the predicted expression of a woman with BMI 25, a realistic person. The bmi coefficient stays \(0.2\) (and sexM stays 1).
B5. (a) \(3 - 1 = 2\) dummy columns: batchB and batchC. (b) A — it is alphabetically first, so all its dummies are 0. (c) A: \(2\); B: \(2 + 4 = 6\); C: \(2 - 1 = 1\). (d) C − B \(= \beta_\text{C} - \beta_\text{B} = -1 - 4 = -5\) (check: \(1 - 6 = -5\)). (e) \(\beta_0 = 5\) (mean of A), batchB \(= 8 - 5 = 3\), batchC \(= 4 - 5 = -1\). So \(\boldsymbol\beta = (5,\ 3,\ -1)\).
B6. (a) and (b):
| group | (Intercept) | batchB | treatmenttreat | \(\hat y\) |
|---|---|---|---|---|
| A, ctrl | 1 | 0 | 0 | \(8\) |
| A, treat | 1 | 0 | 1 | \(8 + 3 = 11\) |
| B, ctrl | 1 | 1 | 0 | \(8 - 2 = 6\) |
| B, treat | 1 | 1 | 1 | \(8 - 2 + 3 = 9\) |
- \(3\) in both batches (\(11 - 8 = 9 - 6 = 3\)). The additive model forces the same treatment effect in every batch.
- No. The model has 3 coefficients for 4 groups and assumes the same treat − ctrl difference in both batches, but here it is \(11 - 8 = 3\) in batch A and \(10 - 6 = 4\) in batch B. Reproducing all four means needs a fourth column, the interaction
batchB:treatmenttreat(its coefficient would be \(4 - 3 = 1\)).
B7. (a) R sorts the levels as text: "2" comes before "6", so "24h" comes before "6h". Fix: time <- factor(time, levels = c("0h", "6h", "24h")). (b) \[
X = \begin{pmatrix}
1 & 0 & 0\\
1 & 0 & 0\\
1 & 1 & 0\\
1 & 1 & 0\\
1 & 0 & 1\\
1 & 0 & 1
\end{pmatrix}
\quad (\text{(Intercept)},\ \text{time6h},\ \text{time24h})
\] (c) 0 h: \(4\); 6 h: \(4 + 6 = 10\); 24 h: \(4 + 3 = 7\). (d) Two columns: (Intercept) and hours. It cannot: a straight line changes by the same amount per hour, but the means go up from 0 h to 6 h (+6) and down from 6 h to 24 h (−3).
B8. (a) The interaction column is treatmenttreat × sexM, row by row:
| group | (Intercept) | treatmenttreat | sexM | treatmenttreat:sexM |
|---|---|---|---|---|
| F, ctrl | 1 | 0 | 0 | \(0 \times 0 = 0\) |
| F, treat | 1 | 1 | 0 | \(1 \times 0 = 0\) |
| M, ctrl | 1 | 0 | 1 | \(0 \times 1 = 0\) |
| M, treat | 1 | 1 | 1 | \(1 \times 1 = 1\) |
- F/ctrl \(4\); F/treat \(4 + 1 = 5\); M/ctrl \(4 + 2 = 6\); M/treat \(4 + 1 + 2 + 3 = 10\). Treat − ctrl: females \(5 - 4 = 1\); males \(10 - 6 = 4\) (that is \(\beta_1 + \beta_3 = 1 + 3\)).
- \(\beta_0 = 6\) (F/ctrl). \(\beta_1 = 9 - 6 = 3\) (F/treat − F/ctrl). \(\beta_2 = 7 - 6 = 1\) (M/ctrl − F/ctrl). M/treat: \(6 + 3 + 1 + \beta_3 = 12\), so \(\beta_3 = 2\). \(\boldsymbol\beta = (6,\ 3,\ 1,\ 2)\).
B9. (a) \(p = 6\): (Intercept), sexM, batchB, batchC, age, treatmenttreat. (b) \((1,\ 0,\ 1,\ 0,\ 50,\ 1)\). (c) At least \(n = 6\) samples (and even then only if the columns are independent; to also estimate the noise you need \(n > 6\)).
B10. (a) drug — levels are ordered alphabetically, and drug comes before placebo, so drug is the reference; the non-reference dummy is treatmentplacebo. (b) treatmentplacebo \(= -4\): placebo expression is 4 units lower than drug, so the drug raises expression by 4 relative to placebo. (c) treatment <- relevel(treatment, ref = "placebo") (or factor(treatment, levels = c("placebo","drug"))). Then the coefficient is treatmentdrug \(= +4\).
B11. (a) \[
X = \begin{pmatrix}
1 & 1 & 0\\
1 & 0 & 1\\
1 & 1 & 0\\
1 & 0 & 1
\end{pmatrix}
\quad (\text{(Intercept)},\ \text{sexF},\ \text{sexM})
\] sexF + sexM equals the intercept column: every sample is either F or M. (b) Females: \(\beta_0 + \beta_\text{F}\); males: \(\beta_0 + \beta_\text{M}\). \((6, 0, 3)\): F \(6\), M \(9\). \((9, -3, 0)\): F \(6\), M \(9\). Any \((c,\ 6 - c,\ 9 - c)\) works, e.g. \((0,\ 6,\ 9)\) or \((5,\ 1,\ 4)\). (c) Not unique: infinitely many \(\boldsymbol\beta\) fit equally well (the dummy-variable trap). Drop one dummy (keep only sexM, as R does), or drop the intercept (~ 0 + sex, then the two coefficients are the two group means).
B12. (a) \[
X_1 = \begin{pmatrix}
1 & 0 & 0\\
1 & 0 & 0\\
1 & 1 & 1\\
1 & 1 & 1
\end{pmatrix},
\qquad
X_2 = \begin{pmatrix}
1 & 0 & 0\\
1 & 0 & 1\\
1 & 1 & 0\\
1 & 1 & 1
\end{pmatrix}
\quad (\text{(Intercept)},\ \text{batchB},\ \text{treatmenttreat})
\] (b) Design 2. In Design 1 the columns batchB and treatmenttreat are identical. (c) Every \(\boldsymbol\beta\) with \(\beta_0 = 5\) and \(\beta_\text{batchB} + \beta_\text{treat} = 3\) fits, e.g. \((5,\ 3,\ 0)\) (“all batch”) and \((5,\ 0,\ 3)\) (“all drug”), or \((5,\ 1,\ 2)\). The data cannot tell whether the difference of 3 comes from the drug or from the batch, so nothing can be concluded about the drug. (d) Re-plan it so that every batch contains both conditions (as in Design 2). This must be fixed in the design, before sequencing; no analysis can separate batch and treatment afterwards.
B13. (a) Not unique. There are no treated males, so the treatmenttreat:sexM column is all zeros (an empty cell). Drop the interaction (~ treatment + sex), or collect treated males. (b) Not unique. \(p = 1 + 1 + 1 + 2 + 1 + 1 = 7\) columns but only \(n = 5\) samples, so the columns must be dependent. Use fewer covariates or many more patients (or, with very many predictors, a penalized model). (c) Unique. \(p = 3\), \(n = 6\), and no column is a combination of the others (the ages differ, so age is not a combination of the intercept and the treatment dummy). Careful: if all controls were 10 weeks and all treated mice 20 weeks, then age \(= 10 \times\) (Intercept) \(+ 10 \times\) treatmenttreat and \(\boldsymbol\beta\) would not be unique. (d) Not unique (as model.matrix builds it): the time24h column is all zeros because no sample is at 24 h. Drop the unused level (droplevels(time)). lm actually does this for you, but model.matrix() and some tools keep the empty column.
R1–R6. These confirm the hand answers.
treatment <- factor(c("ctrl","treat","ctrl","treat"))
age <- c(10, 40, 20, 30)
X <- model.matrix(~ treatment + age) # R1: matches B2
X %*% c(5, 3, 0.1) # 6 12 7 11 (B3a)
sex <- factor(c("F","M","F","M")); bmi <- c(20, 25, 30, 35)
model.matrix(~ sex + bmi) %*% c(2, 1, 0.2) # R2: 6 8 8 10 (B4b)
model.matrix(~ sex + I(bmi - 25)) # bmi column becomes -5 0 5 10
batch <- factor(c("A","B","C"))
model.matrix(~ batch) %*% c(2, 4, -1) # R3: 2 6 1 (B5c)
time <- factor(c("0h","0h","6h","6h","24h","24h"), levels = c("0h","6h","24h"))
model.matrix(~ time) %*% c(4, 6, 3) # 4 4 10 10 7 7 (B7c)
treatment <- factor(c("ctrl","treat","ctrl","treat"))
sex <- factor(c("F","F","M","M"))
model.matrix(~ treatment * sex) %*% c(4, 1, 2, 3) # R4: 4 5 6 10 (B8b)
treatment <- factor(c("drug","placebo","drug","placebo"))
levels(treatment) # R5: "drug" "placebo"
levels(relevel(treatment, ref = "placebo")) # "placebo" "drug"
sexF <- c(1, 0, 1, 0); sexM <- c(0, 1, 0, 1)
qr(cbind(1, sexF, sexM))$rank # R6: 2 < 3 columns (B11)
d1 <- model.matrix(~ factor(c("A","A","B","B")) + factor(c("ctrl","ctrl","treat","treat")))
d2 <- model.matrix(~ factor(c("A","A","B","B")) + factor(c("ctrl","treat","ctrl","treat")))
qr(d1)$rank; qr(d2)$rank # 2 (Design 1), 3 (Design 2)
tm <- factor(c("0h","0h","6h","6h"), levels = c("0h","6h","24h"))
qr(model.matrix(~ tm))$rank # 2 < 3 columns (B13d)Where this leads
- Confounding & Simpson’s paradox: what it means that a coefficient is read “holding the other predictors constant”, why adding a covariate can change — even reverse — an effect, and how to read interaction coefficients.
- Many genes at once: limma: the
model.matrix()you built here is exactly what limma takes asdesignand fits to every gene. For the designs that failed in B11–B13, limma reports “coefficients not estimable” andNA, just likelm. - Penalized linear models: when you have more columns than samples (\(p > n\)), or columns that are almost dependent, ridge and lasso add a penalty that makes the answer unique again.