I have an lme4 mixed model with multiple variables. I am interested in the orthogonal contrast for one of the variables in the model which has three groups (say A, B, C). I want the compare A to the average of B and C. How can I do this using emmeans in R?
Here's one approach that I imagine would work:
mtcars$cyl <-as.factor(mtcars$cyl)
fit <- lm(mpg ~ wt + cyl, mtcars)
library(emmeans)
fit
emm_fit <- emmeans(fit, specs = "cyl")
contrast(emm_fit, list(cyl = c(1, -.5, -.5)))
This is the equivalent of x_1 - .5x_2-.5x_3
This results in the following:
contrast estimate SE df t.ratio p.value
cyl 5.16 1.37 28 3.781 0.0008
This fits a model, uses emmeans to develop the marginals, then in the contrast
function we look at the difference between 4 cylinders from the average of 6 and 8. For more details see https://cran.r-project.org/web/packages/emmeans/vignettes/comparisons.html#contrasts