# Contrast for hypothesis test in R (lmer)

I'm running a lmer mixed effects model with a four-level factor (levels "0","10","100","1000") as the fixed effect.

lmer(free ~ reward.f + (1|S), longdata)


I know that by default, R uses treatment contrasts and the levels 10, 100, and 1000 are compared to level "0". I would instead like each level to be compared to the previous one, to test a monotonic decrease in "free" across levels of "reward.f"

I would do this:

contr.mat <- matrix(c(c(-1,1,0,0),c(0,-1,1,0),c(0,0,-1,1)),4)
colnames(contr.mat) <- c(10,100,1000)
contrasts(longdata$reward.f) <- contr.mat contrasts(longdata$reward.f)
10 100 1000
0    -1   0    0
10    1  -1    0
100   0   1   -1
1000  0   0    1


Is this the correct contrast matrix for these comparisons?

You are looking for sliding differences aka. repeated contrasts, i.e., $0\ vs. 10$, $10\ vs. 100$, and $100\ vs. 1000$. But your contrast matrix is not appropriate for these tests.

     10 100 1000
0    -1   0    0
10    1  -1    0
100   0   1   -1
1000  0   0    1


Actually, these contrasts test (1) the mean of the first level ($0$) against the mean of levels two, three, and four ($10$, $100$, and $1000$), (2) the mean of the first two levels against the mean of the last two levels, and (3) the mean of the first three levels against the mean of the last level.

The correct contrast matrix for sliding differences (given a categorical variable with four levels) is:

        10  100  1000
0    -0.75 -0.5 -0.25
10    0.25 -0.5 -0.25
100   0.25  0.5 -0.25
1000  0.25  0.5  0.75


For more details, have a look at this answer: How to interpret these custom contrasts?

• This must be the right answer, but I find it frustrating that I was so off. In particular, why is the first column in contr.helmert (which also compares level '10' with level '0'as my first comparison) also c(-1,1,0,0)? If there's a reference I could learn from I'd be very grateful! Anyway, many thanks for your answer. – Marianne Nov 28 '12 at 19:30
• Because contr.helmert is a matrix of orthogonal contrasts & the transpose of the generalized inverse is the same bar a rescaling of each column. So the interpretation of the effect estimates is different but the t statistics are the same. This is not the case when the contrasts aren't orthogonal (which I now know from reading Sven's answer - sorry!). Venables & Ripley cover this well in Section 6.2 of 'Modern Applied Statistics with S'. – Scortchi - Reinstate Monica Nov 28 '12 at 20:45

Yes [Actually no, you have to take the transpose of the generalized inverse of this matrix and set that as the contrast matrix, as Sven points out] - in the first contrast (column), '10' is compared with '0'; in the second, '100' with '10'; in the third '1000' with '100'. Note that these are not orthogonal: if it's not strictly monotonicity you're concerned with you may want to consider [reverse] Helmert contrasts, which compare each level with the mean of subsequent [preceding] levels; or polynomial contrasts, which will split the factor effect into linear, quadratic, & cubic effects.

PS The column names you've given are a bit confusing, as they're the same as the level names.

• Many thanks! I had done the analysis with helmert contrasts as well and was just about to post a follow-up, since they yield different results: – Marianne Nov 28 '12 at 12:23
• With helmert contrasts, the t-value of the first comparison (10 vs 0) is -1.17; with my custom contrasts above it is -4.82. Plotting the data, this seems to be a clear type I error in the model using my custom contrasts, which I assume stems from the non-orthogonality. I find it quite drastic that the t value would be so inflated, even though the comparison should be just the same (first column both contrast matrices looks just the same). – Marianne Nov 28 '12 at 12:30
• When the contrast is the same the t-statistic should be the same. Orthogonality just means that the tests for each contrast are independent. Perhaps you could post some code. – Scortchi - Reinstate Monica Nov 28 '12 at 14:08