How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?

How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA? | SPSS FAQ

After running a repeated measures ANOVA model, we are often interested in performing post hoc contrasts between various cell means or effects to estimate specific effects or differences in effects. To specify a contrast, some of the coefficients will be weighted with positive weights (e.g. 1) and others with negative weights (e.g. -1).

In SPSS, the repeated measures ANOVA model is a multivariate model (multiple outcomes).

To demonstrate, we will run a repeated measures ANOVA model with one between-subjects factor with 2 levels, A, and one within-subjects factor with 4 levels, which we will call time. The levels of the within-subjects factor, time, varies across the 4 measured outcomes.

Below are the data:

say1y2y3y4
113477
216588
313479
413368
5212510
6223610
722459
8223611

The variables are:

To perform a repeated measures ANOVA in SPSS, we will use this syntax:

GLM y1 y2 y3 y4 BY a
/WSFACTOR=time 4
/WSDESIGN=time
/DESIGN=a
/PRINT=parameter.

Let’s look at the output table

From the Parameter Estimates table, we can see that we SPSS parameterizes this model like so:

$$y_{1i} = beta_{01} + beta_{11}(a_i=1) + beta_{21}(a_i=2) + epsilon_{1i}$$
$$y_{2i} = beta_{02} + beta_{12}(a_i=1) + beta_{22}(a_i=2) + epsilon_{2i}$$
$$y_{3i} = beta_{03} + beta_{13}(a_i=1) + beta_{23}(a_i=2) + epsilon_{3i}$$
$$y_{4i} = beta_{04} + beta_{14}(a_i=1) + beta_{24}(a_i=2) + epsilon_{4i}$$

or for short:

$$y_{ti} = beta_{01} + beta_{1t}(a_i=1) + beta_{2t}(a_i=2) + epsilon_{1t}$$

Each of the (hat{beta}_{2t}) are constrained to be 0 because of collinearity.

We can represent the model in matrix form as:

$$mathbf{Y = XB + E}$$

where (mathbf{Y}) is (ntimes t), (mathbf{X}) is (ntimes p), (mathbf{B}) is (ptimes t), and (mathbf{E}) is (ntimes t), and here

We will be using the matrix (mathbf{B}) for our custom contrasts. For our model above, (mathbf{B}) looks like this:

begin{bmatrix}hat{beta}_{01} & hat{beta}_{02} & hat{beta}_{03} & hat{beta}_{04}\hat{beta}_{11} & hat{beta}_{12} & hat{beta}_{13} & hat{beta}_{14}\hat{beta}_{21} & hat{beta}_{22} & hat{beta}_{23} & hat{beta}_{24}end{bmatrix}

Now we are ready to use LMATRIX and MMATRIX to perform our contrasts.

The matrix equation that guides the coding of our contrasts is this:

$$mathbf{L’BM = K}$$

where for a single contrast,

Each of (mathbf{L}), (mathbf{M}), and (mathbf{K}) can be expanded to have (c) columns to represent (c) simultaneous contrasts.

Between-subjects contrast

Let’s start with the a between subjects contrast. It is not terribly interesting since the regression table already has all of these contrasts, but it is still instructive as to how to use LMATRIX and MMATRIX.

Imagine we wanted to test whether the mean of $y$ for A=1 is different from A=2 when t=1 (the first time point).

Our predicted value for (y_1) given A=1 is:

$$hat{y}_{1|A=1} = hat{beta}_{01} + hat{beta}_{11}$$

The predicted value for (y_1) given A=2 is:

$$hat{y}_{1|A=2} = hat{beta}_{01} + hat{beta}_{21}$$

The effect of A for t=1 is just the difference between these 2 predicted values:

begin{align}
hat{y}_{1|A=2} – hat{y}_{1|A=1} & = (hat{beta}_{01} + hat{beta}_{21}) – (hat{beta}_{01} + hat{beta}_{11}) \
& = hat{beta}_{21} – hat{beta}_{11}
end{align}

Intuitively, then, this contrast should be between the second and third parameter for the first equation. If we set up the (mathbf{L}) matrix to reflect this contrast, knowing that it is used for between-subjects contrast, it would look like this:

$$mathbf{L} = begin{bmatrix}0\-1\1end{bmatrix}$$

Then multiplying (mathbf{L’B}) we get this:

begin{align}
mathbf{L’B} & = begin{bmatrix}0 & -1 & 1 end{bmatrix}timesbegin{bmatrix}hat{beta}_{01} & hat{beta}_{02} & hat{beta}_{03} & hat{beta}_{04}\hat{beta}_{11} & hat{beta}_{12} & hat{beta}_{13} & hat{beta}_{14}\hat{beta}_{21} & hat{beta}_{22} & hat{beta}_{23} & hat{beta}_{24}end{bmatrix} \
& = begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21}) & (-hat{beta}_{12} + hat{beta}_{22}) & (-hat{beta}_{13} + hat{beta}_{23}) & (-hat{beta}_{14} + hat{beta}_{24})end{bmatrix}
end{align}

We see the quantity we want ((hat{beta}_{21} – hat{beta}_{11})) as the first element of (mathbf{L’B}). We can use the matrix (mathbf{M}) to select this first element like so:

$$mathbf{M} = begin{bmatrix}1\0\0\0end{bmatrix}$$

The result of (mathbf{L’BM}) is thus:

begin{align}
mathbf{L’BM} & = begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21}) & (-hat{beta}_{12} + hat{beta}_{22}) & (-hat{beta}_{13} + hat{beta}_{23}) & (-hat{beta}_{14} + hat{beta}_{24})end{bmatrix} times begin{bmatrix}1\0\0\0end{bmatrix}\
& = begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21})end{bmatrix}
end{align}

Now we have the quantity we want isolated, we can test it against zero with the (mathbf{K}) matrix, which looks like this:

$$mathbf{K} = begin{bmatrix}0end{bmatrix}$$

So then the test we have is this:

begin{align}
mathbf{L’BM} & = mathbf{K} \
begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21})end{bmatrix} & = 0
end{align}

Since all of the (hat{beta}_{2t}) are constrained to be (0), this is essentially a test of (-hat{beta}_{11}) against zero.

Here is the SPSS code:

GLM y1 y2 y3 y4 BY a
/WSFACTOR=time 4
/WSDESIGN=time
/DESIGN=a
/PRINT=parameter
/LMATRIX = intercept 0 a -1 1
/MMATRIX = y1 1 y2 0 y3 0 y4 0.

And the resulting output:

We can see that the “Contrast Estiamte” is indeed equal to (-hat{beta}_{11}).

Within-subjects contrasts

Now let’s look at the effect of the within-subjects factor, time.

We’ll start simple, looking at the mean of the outcome at time=1 vs the mean of the outcome at time=4, given that a=2.

We are testing this hypothesis then:

$$H_0: mu_{4|A=2} – mu_{1|A=2} = 0$$

Out predicted value for $mu_{4|a=2}$ and $mu_{1|A=2}$ are:
$$hat{y}_{4|A=2} = hat{beta}_{04} + hat{beta}_{24}$$
$$hat{y}_{1|A=2} = hat{beta}_{01} + hat{beta}_{21}$$

To estimate the contrast then, we want:

$$hat{y}_{4|A=2} – hat{y}_{1|A=2} = hat{beta}_{04} + hat{beta}_{24} – (hat{beta}_{01} + hat{beta}_{21})$$

Regarding the $hat{beta}$ matrix, we see that we want to select and sum the first and third elements of the first and fourth columns. To extract those elements with the $L$ matrix, we specify $L$ as:

$$mathbf{L} = begin{bmatrix}1\0\1end{bmatrix}$$

Multiplying $L’B$:

begin{align}
mathbf{L’B} & = begin{bmatrix}1 & 0 & 1 end{bmatrix}timesbegin{bmatrix}hat{beta}_{01} & hat{beta}_{02} & hat{beta}_{03} & hat{beta}_{04}\hat{beta}_{11} & hat{beta}_{12} & hat{beta}_{13} & hat{beta}_{14}\hat{beta}_{21} & hat{beta}_{22} & hat{beta}_{23} & hat{beta}_{24}end{bmatrix} \
& = begin{bmatrix}(hat{beta}_{01} + hat{beta}_{21}) & (hat{beta}_{02} + hat{beta}_{22}) & (hat{beta}_{03} + hat{beta}_{23}) & (hat{beta}_{04} + hat{beta}_{24})end{bmatrix}
end{align}

In the resulting $L’B$ matrix (1X4 vector), we see the two quantities of our contrast, the first and fourth elements. We want to subtract the first element from the fourth, which we specify with this $M$ matrix:

$$mathbf{M} = begin{bmatrix}-1\0\0\1end{bmatrix}$$

Multiplying $L’B$ by $M$:

begin{align}
mathbf{L’BM} & = begin{bmatrix}(hat{beta}_{01} + hat{beta}_{21}) & (hat{beta}_{02} + hat{beta}_{22}) & (hat{beta}_{03} + hat{beta}_{23}) & (hat{beta}_{04} + hat{beta}_{24})end{bmatrix} times begin{bmatrix}-1\0\0\1end{bmatrix} \
& = begin{bmatrix}-(hat{beta}_{01} + hat{beta}_{21}) + (hat{beta}_{04} + hat{beta}_{24})end{bmatrix} \
& = (hat{beta}_{04} + hat{beta}_{24}) – (hat{beta}_{01} + hat{beta}_{21})
end{align}

That is the contrast that we want.

Let’s perform this contrast in SPSS. Again we specify the weights for LMATRIX and MMATRIX, and we also output the estimated cell means with EMMEANS to confirm our contrast.

GLM y1 y2 y3 y4 BY a
/WSFACTOR=time 4
/WSDESIGN=time
/DESIGN=a
/PRINT=parameter
/LMATRIX = intercept 1 a 0 1
/EMMEANS tables(a*time)
/MMATRIX = y1 -1 y2 0 y3 0 y4 1.

The contrast estimate is 8.25. Let’s confirm this with the EMMEANS table:

The estimated cell means are $hat{y}_{4|A=2}=10$ and $hat{y}_{1|A=2}=1.75$, yielding the difference estimate: $hat{y}_{4|A=2}-hat{y}_{1|A=2}=8.25$

Simple effect contrasts: differences of differences

The GLM command will automatically interact the between-subjects factors with the within-subjects factors in a repeated-measures design, allowing the effect of the between-subjects factor to be different across levels of the within-subjects factor and vice-versa. We can see the test of the interaction in the output table “Tests of Within-Subjects Effects”. Here is the table from the model above:

We see that the test of the interaction is significant with p

Let’s do a contrast of the simple effect of A at time=2 vs the simple effect of A at time=4. This tests whether the effect of A is different at the two time points. We’ll first formulate the contrast in terms of the regression coefficients.

The simple effect of A at time=2 is this difference:
begin{align}
hat{y}_{2|A=2} – hat{y}_{2|A=1} & = (hat{beta}_{02} + hat{beta}_{22}) – (hat{beta}_{02} + hat{beta}_{12}) \
& = hat{beta}_{22} – hat{beta}_{12}
end{align}

The simple effect of A at time=4 is this difference:
begin{align}
hat{y}_{4|A=2} – hat{y}_{4|A=1} & = (hat{beta}_{42} + hat{beta}_{42}) – (hat{beta}_{42} + hat{beta}_{42}) \
& = hat{beta}_{24} – hat{beta}_{14}
end{align}

The contrast of these simple effects is another difference: $$(hat{y}_{4|A=2} – hat{y}_{4|A=1}) – (hat{y}_{2|A=2} – hat{y}_{2|A=1}) = (hat{beta}_{24} – hat{beta}_{14}) – (hat{beta}_{22} – hat{beta}_{12}) $$

This contrast, then, is a contrast of two between-subjects contrasts. Because the individual elements are between-subjects contrasts, we will need to specify contrast coding in LMATRIX (i.e. use a matrix with 1 and -1 elements). But then, those contrasts are themselves contrasted, which can be accomplished with contrast coding MMATRIX.

The individual simple effects of A for time=2 and time=4 can be specified with matrix $L$ that subtracts the second element from the third:

$$mathbf{L} = begin{bmatrix}0\-1\1end{bmatrix}$$

Multiplying $L’B$ (we have seen this before):

begin{align}
mathbf{L’B} & = begin{bmatrix}0 & -1 & 1 end{bmatrix}timesbegin{bmatrix}hat{beta}_{01} & hat{beta}_{02} & hat{beta}_{03} & hat{beta}_{04}\hat{beta}_{11} & hat{beta}_{12} & hat{beta}_{13} & hat{beta}_{14}\hat{beta}_{21} & hat{beta}_{22} & hat{beta}_{23} & hat{beta}_{24}end{bmatrix} \
& = begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21}) & (-hat{beta}_{12} + hat{beta}_{22}) & (-hat{beta}_{13} + hat{beta}_{23}) & (-hat{beta}_{14} + hat{beta}_{24})end{bmatrix}
end{align}

Now we want to subtract the second element of $L’B$ from the fourth. We can specify this contrast in $M$:

$$mathbf{M} = begin{bmatrix}0\-1\0\1end{bmatrix}$$

Multiplying $L’B$ by $M$:

begin{align}
mathbf{L’BM} & = begin{bmatrix}(-hat{beta}_{11} + hat{beta}_{21}) & (-hat{beta}_{12} + hat{beta}_{22}) & (-hat{beta}_{13} + hat{beta}_{23}) & (-hat{beta}_{14} + hat{beta}_{24})end{bmatrix} times begin{bmatrix}0\-1\0\1end{bmatrix}\
& = begin{bmatrix}-(-hat{beta}_{12} + hat{beta}_{22}) + (-hat{beta}_{14} + hat{beta}_{24})end{bmatrix}\
& = (-hat{beta}_{14} + hat{beta}_{24}) – (-hat{beta}_{12} + hat{beta}_{22})
end{align}

That is the contrast (of contrasts) that we want.

Let’s try it in SPSS:

GLM y1 y2 y3 y4 BY a
/WSFACTOR=time 4
/WSDESIGN=time
/DESIGN=a
/PRINT=parameter
/LMATRIX = intercept 0 a -1 1
/EMMEANS tables(a*time)
/MMATRIX = y1 0 y2 -1 y3 0 y4 1.

Our contrast estimate:

The contrast estimate is 3.5. Let’s confirm with the EMMEANS table.

From the table above:
begin{align}
(hat{y}_{4|A=2} – hat{y}_{4|A=1}) – (hat{y}_{2|A=2} – hat{y}_{2|A=1}) & = (10 – 8) – (5.5 – 7)\
& = 3.5
end{align}

Confirmed!

Cite this article

stats writer (2024). How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-lmatrix-and-mmatrix-to-perform-custom-contrasts-in-a-repeated-measures-anova/

stats writer. "How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-lmatrix-and-mmatrix-to-perform-custom-contrasts-in-a-repeated-measures-anova/.

stats writer. "How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-lmatrix-and-mmatrix-to-perform-custom-contrasts-in-a-repeated-measures-anova/.

stats writer (2024) 'How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-lmatrix-and-mmatrix-to-perform-custom-contrasts-in-a-repeated-measures-anova/.

[1] stats writer, "How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use LMATRIX and MMATRIX to perform custom contrasts in a repeated measures ANOVA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top