Tutorials › Multivariate Statistics (Graduate) › Contrast Matrices for Within-Subject Effects

Contrasts and Repeated Measures · Tutorial 358 of 975

Contrast Matrices for Within-Subject Effects

Build contrast matrices that express planned comparisons across repeated-measurement occasions, and check whether their rows are valid and independent.

Advanced 11 min read

What You'll Learn

  • Define a within-subject contrast and its contrast matrix
  • Explain why each contrast must have coefficients that sum to zero
  • Distinguish independent contrast rows from redundant rows using matrix rank
  • Construct matrices for specified occasion-to-occasion and average-to-average comparisons
  • Identify when a contrast matrix represents all profile departures from a constant level
  • Diagnose common errors in coefficient ordering and normalization

Why Use Within-Subject Contrasts?

When the same participants are measured on several occasions, each participant contributes a profile of measurements. The scientific question may concern how that profile changes: for example, whether a later occasion differs from baseline, or whether the average at some occasions differs from the average at others. A within-subject contrast expresses such a comparison as a weighted sum of occasion means.

Let there be \(m\) occasions, ordered in a fixed way, and write the population mean profile as \(\mu=(\mu_1,\ldots,\mu_m)^\mathsf{T}\). The order matters: the first entry of every coefficient vector multiplies \(\mu_1\), the second multiplies \(\mu_2\), and so on. A contrast compares components of this profile while removing any common level shared by all occasions.

Definition: A within-subject contrast is a linear combination \(c^\mathsf{T}\mu=\sum_{j=1}^m c_j\mu_j\) whose coefficients satisfy \(\sum_{j=1}^m c_j=0\). A within-subject contrast matrix \(C\) is a \(q\times m\) matrix whose rows are contrast coefficient vectors. Equivalently, \(C\mathbf{1}_m=\mathbf{0}\), where \(\mathbf{1}_m\) is the \(m\)-vector of ones.

The \(q\)-vector \(C\mu\) contains the \(q\) specified comparisons. A matrix can encode one selected comparison or several. Its rows need not be scaled to have integer coefficients: multiplying a contrast by a nonzero constant changes its units, but not the comparison it represents. For instance, \((-1,1)\) gives the difference between two means, while \((-1/2,1/2)\) gives half that difference.

The Zero-Sum Condition

The coefficients must sum to zero because a contrast is intended to measure a difference in profile shape, not the overall level. If every occasion mean is increased by the same amount \(a\), then the comparison should not change. The zero-sum condition guarantees exactly that.

Result: For a coefficient vector \(c\), the linear combination \(c^\mathsf{T}\mu\) is unchanged by every common shift \(\mu\mapsto\mu+a\mathbf{1}_m\) if and only if \(c^\mathsf{T}\mathbf{1}_m=0\).

Proof. Under a common shift, the linear combination becomes

$$ c^\mathsf{T}(\mu+a\mathbf{1}_m) =c^\mathsf{T}\mu+a\,c^\mathsf{T}\mathbf{1}_m. $$

If \(c^\mathsf{T}\mathbf{1}_m=0\), the added term is zero for every \(a\), so the contrast is unchanged. Conversely, suppose it is unchanged for every \(a\). Then \(a\,c^\mathsf{T}\mathbf{1}_m=0\) for every \(a\); choosing \(a=1\) gives \(c^\mathsf{T}\mathbf{1}_m=0\). This proves both directions. \(\square\)

The same reasoning applies row by row to \(C\). Thus \(C\mathbf{1}_m=\mathbf{0}\) says that none of the matrix’s comparisons responds to a shift common to every occasion. This condition does not say that the occasion means are equal; it describes which linear combinations the matrix computes.

Worked Example: Comparing Two Occasions

Suppose the mean responses at baseline and follow-up are \(\mu_1\) and \(\mu_2\). Construct a matrix for the follow-up-minus-baseline comparison.

The desired quantity is \(\mu_2-\mu_1\). In the fixed order (baseline, follow-up), its coefficients are \(-1\) and \(1\), so take \(C=(-1,\ 1)\). The coefficients sum to \(-1+1=0\), and

$$ C\mu=(-1,\ 1) \begin{pmatrix} \mu_1\\ \mu_2 \end{pmatrix} =-\mu_1+\mu_2 =\mu_2-\mu_1. $$

If both means increase by \(a\), the result is \((\mu_2+a)-(\mu_1+a)=\mu_2-\mu_1\). Reversing the coefficient signs would instead compute baseline-minus-follow-up.

Rank and the Number of Independent Comparisons

The rank of \(C\) is the number of linearly independent rows, and therefore the number of independent linear comparisons encoded by its rows. The requirement depends on what the matrix is meant to represent. For a selected collection of \(q\) comparisons, full row rank, \(\operatorname{rank}(C)=q\), means that none of the listed rows is a linear combination of the others. If some rows are redundant, the matrix still satisfies the zero-sum condition, but it does not encode as many independent comparisons as it has rows.

There cannot be more than \(m-1\) independent within-subject contrasts. This is a consequence of the zero-sum constraint, not a requirement that every analysis use all \(m-1\) possible dimensions.

Result: The set of all contrast coefficient vectors for \(m\) occasions has dimension \(m-1\). Consequently, a \(q\times m\) contrast matrix has rank at most \(m-1\). A full set of contrasts that captures every departure of the mean profile from a constant profile has rank \(m-1\).

Proof. A coefficient vector \(c\) is a contrast exactly when \(\sum_{j=1}^m c_j=0\). Its first \(m-1\) entries can be chosen freely, after which the last entry must be \(c_m=-\sum_{j=1}^{m-1}c_j\). There are therefore \(m-1\) free coordinates, so the contrast space has dimension \(m-1\). Every row of \(C\) lies in this space, which gives \(\operatorname{rank}(C)\leq m-1\).

For the final claim, the constant profiles are the vectors \(a\mathbf{1}_m\). If \(C\) has rank \(m-1\), the rank–nullity theorem gives \(\dim(\ker C)=m-(m-1)=1\). Since \(C\mathbf{1}_m=\mathbf{0}\), the kernel contains the one-dimensional span of \(\mathbf{1}_m\), and thus equals that span. Therefore \(C\mu=\mathbf{0}\) holds exactly for constant profiles. This establishes the claim. \(\square\)

For a chosen set of comparisons, full row rank is the relevant independence check. If the aim is to represent all nonconstant profile departures, use \(m-1\) independent rows. These are different design choices: the first may ask only about a few planned comparisons, while the second spans the entire contrast space.

Worked Example: Comparing Follow-Up Occasions With Baseline

There are four occasions: baseline, week 1, week 2, and week 3. Construct a matrix whose rows compare each follow-up mean with baseline, in the direction follow-up minus baseline.

The three requested quantities are \(\mu_2-\mu_1\), \(\mu_3-\mu_1\), and \(\mu_4-\mu_1\). Their rows, in the stated occasion order, give

$$ C= \begin{pmatrix} -1&1&0&0\\ -1&0&1&0\\ -1&0&0&1 \end{pmatrix}, \qquad C\mu= \begin{pmatrix} \mu_2-\mu_1\\ \mu_3-\mu_1\\ \mu_4-\mu_1 \end{pmatrix}. $$

Each row sums to zero. To verify rank three, suppose a linear combination of the rows with coefficients \(a,b,d\) equals the zero row. Looking at columns 2, 3, and 4 gives \(a=0\), \(b=0\), and \(d=0\), respectively. Thus the rows are independent and \(\operatorname{rank}(C)=3=m-1\). This matrix spans all departures from a constant four-occasion profile.

Constructing Contrasts From a Comparison

A reliable construction starts with the comparison in words, translates it into a formula, and then places each coefficient in the column for its occasion. When comparing averages of groups of occasions, give each occasion in a group equal weight within that group. For example, the difference between the average of two later occasions and the average of two earlier occasions uses coefficients \(-1/2\) on the earlier pair and \(1/2\) on the later pair. Each average is weighted equally, and the total coefficient sum is zero.

Worked Example: Comparing Two Occasion Averages and a Pair

For four ordered occasions, construct two comparisons: (1) the average of occasions 3 and 4 minus the average of occasions 1 and 2; (2) occasion 4 minus occasion 3.

The first comparison is \((\mu_3+\mu_4)/2-(\mu_1+\mu_2)/2\). Its coefficients in occasion order are \((-1/2,-1/2,1/2,1/2)\). The second is \(\mu_4-\mu_3\), with coefficients \((0,0,-1,1)\). Therefore,

$$ C= \begin{pmatrix} -\tfrac12&-\tfrac12&\tfrac12&\tfrac12\\ 0&0&-1&1 \end{pmatrix}, \qquad C\mu= \begin{pmatrix} (\mu_3+\mu_4)/2-(\mu_1+\mu_2)/2\\ \mu_4-\mu_3 \end{pmatrix}. $$

The row sums are \(-1/2-1/2+1/2+1/2=0\) and \(0+0-1+1=0\). The rows are independent: if \(a\) times the first row plus \(b\) times the second is zero, column 1 gives \(-a/2=0\), so \(a=0\); column 3 then gives \(-b=0\), so \(b=0\). Hence \(\operatorname{rank}(C)=2\). The matrix encodes two independent comparisons, not all three dimensions of the four-occasion contrast space.

Coefficient scaling should match the intended interpretation. For example, \((-1,-1,1,1)\) also has zero sum and encodes a contrast between the two occasion pairs, but its value is twice the difference of their averages. That is a valid contrast, yet its magnitude has different units. State the target comparison first so that the chosen scale is transparent.

Worked Example: Detecting a Redundant Comparison

For three occasions, consider the rows \(c_1=(-1,1,0)\), \(c_2=(0,-1,1)\), and \(c_3=(-1,0,1)\). Determine the comparisons, check the zero-sum condition, and find the rank.

The rows represent \(\mu_2-\mu_1\), \(\mu_3-\mu_2\), and \(\mu_3-\mu_1\), respectively. Each row sums to zero. But the third comparison is the sum of the first two:

$$ c_1+c_2=(-1,1,0)+(0,-1,1)=(-1,0,1)=c_3. $$

Therefore the three rows are dependent. The first two are independent because if \(a c_1+b c_2=(0,0,0)\), the first coordinate gives \(-a=0\), and the third gives \(b=0\). Thus \(\operatorname{rank}(C)=2\), not 3. The matrix has three rows but only two independent comparisons, which is the maximum possible for three occasions.

Common Mistakes and Practical Checks

A contrast matrix should be checked against the intended scientific comparison, not just against its dimensions. Use the following sequence before interpreting its output:

1
Fix the occasion order.
Write the occasion labels in column order and keep that order while assigning every coefficient.
2
Translate each comparison into a formula.
For example, “later minus earlier” determines the sign before coefficients are entered in the matrix.
3
Check every row sum.
A nonzero sum means the row changes under a common shift and is not a within-subject contrast.
4
Check rank against the goal.
Use full row rank for a list of independent planned comparisons; use rank \(m-1\) when the goal is a complete set spanning all profile departures from a constant level.
  • Using coefficients that do not sum to zero. A weighted sum such as \((1,1,0)\) measures an overall level component as well as any differences; it is not a contrast.
  • Reversing the sign. The rows \((-1,1)\) and \((1,-1)\) are both valid, but they represent opposite directions. Match the signs to the wording of the comparison.
  • Counting rows instead of independent rows. A matrix with \(q\) rows need not have rank \(q\). Check for linear relations, as in the three-occasion example.
  • Confusing a selected contrast set with a complete set. A few planned comparisons can be useful without spanning the full \(m-1\)-dimensional contrast space.

Contrast matrices make the scientific meaning of a repeated-measures comparison explicit in its coefficients. The zero-sum condition removes the common level; rank records how many distinct comparisons the rows actually encode. Together, these checks clarify both what is being compared and whether the matrix contains redundant statements.

Check Your Understanding

Answer each question using the stated occasion order.

  1. For three occasions, write a row that computes occasion 1 minus the average of occasions 2 and 3. Verify its coefficient sum.
  2. Why does a within-subject contrast remain unchanged when the same constant is added to every occasion mean?
  3. What is the maximum possible rank of a within-subject contrast matrix for five occasions, and why?
  4. Two rows are \((-1,1,0)\) and \((0,-1,1)\). What comparisons do they represent, and are they independent?
  5. A matrix has four contrast rows but rank three. How many independent comparisons does it encode, and what does that imply about the rows?
Clinical Biostats