I am using lmer in R to check the effect of condition (cond) on some result. Here are some made up data, where s is the subject identifier and a, b and c are conditions.
library("tidyr")
library("dplyr")
set.seed(123)
temp <- data.frame(s = paste0("S", 1:30),
a = rnorm(30, -2, 1),
b = rnorm(30, -3, 1),
c = rnorm(30, -4, 1))
I would like to compare
- level
ato the mean of levelsbandcand - level
bto levelc.
My question is, how do I set the contrasts to do this in such a way that the intercept reflects the mean of the three conditions and the two computed estimates directly reflect differences as defined in 1. and 2.?
I tried with
c1 <- cbind(c(-0.5, 0.25, 0.25), c(0, -0.5, 0.5))
gather(temp, cond, result, a, b, c) %>%
lmer(result ~ cond + (1|s), data = ., contrasts = list(cond = c1))
where cond2 seems to be OK, but cond1 is not.
Following How to interpret these custom contrasts?, I tried to use the generalized inverse instead, but these estimates don't make sense either.
c2 <- t(ginv(c1))
gather(temp, cond, result, a, b, c) %>%
lmer(result ~ cond + (1|s), data = ., contrasts = list(cond = c2))
I tried Helmert contrasts too, but the means still don't match up.
gather(temp, cond, result, a, b, c) %>%
mutate(cond = factor(cond, levels = c("c", "b", "a"))) %>%
lmer(result ~ cond + (1|s), data = ., contrasts = list(cond = contr.helmert))
What is the correct way to do this?