# How to set contrasts for contrast in post-hoc comparison of linear mixed effect model in R?

I have 3 age groups (Y/HO/LO) and 2 conditions (Related/Unrelated). I am running a linear regression model on RT (reaction time) with Group and Condition as fixed effects, Subject and Item as random effect:

mdl_RT <- lmer(Target_RT ~ Group * Condition + (1|Subject) + (1|ItemID_1), data = SP_RT)
summary(mdl_RT)
Anova(mdl_RT, type = "III")


It shows that there is a significant interaction between Group and Condition, so I did a post-hoc comparison:

posthoc_rt <- emmeans(mdl_RT, pairwise ~ Group:Condition)
comp <- pairs(posthoc_rt, simple = "each", adjust = "bonferroni")
comp$emmeans  It is good that it gave me the simple contrasts for Group and for Condition, something like this: $simple contrasts for Condition
Group = HO:
contrast            estimate   SE  df z.ratio p.value
Related - Unrelated    -32.3 3.99 Inf  -8.080  <.0001

Group = LO:
contrast            estimate   SE  df z.ratio p.value
Related - Unrelated    -27.2 4.10 Inf  -6.647  <.0001

Group = Y:
contrast            estimate   SE  df z.ratio p.value
Related - Unrelated    -15.5 4.01 Inf  -3.854  0.0001


Also, it gave me ALL contrasts for contrast:

\$simple contrasts for contrast
contrast                                                     estimate    SE  df z.ratio p.value
(HO Related - LO Related) - (HO Related - Y Related)           -73.95 25.46 Inf  -2.904  0.3865
(HO Related - LO Related) - (HO Related - HO Unrelated)         -2.91 25.25 Inf  -0.115  1.0000
(HO Related - LO Related) - (HO Related - LO Unrelated)         27.25  4.10 Inf   6.647  <.0001
(HO Related - LO Related) - (HO Related - Y Unrelated)         -58.48 25.46 Inf  -2.296  1.0000
(HO Related - LO Related) - (LO Related - Y Related)          -109.12 43.98 Inf  -2.481  1.0000


Could you help me with how to set the contrasts for contrast which are of my interest? Now it is comparing too many contrasts. I am only interested in 3 contrasts of contrast:

(HO Related - HO Unrelated) - (LO Related - LO Related); (HO Related - HO Unrelated) - (Y Related - Y Related); (LO Related - LO Unrelated) - (Y Related - Y Related);

Thank you very much!

You can first create a emmGrid by:

this_emm = emmeans(mdl_RT, ~ Group:Condition)

Then, when you type this_emm, you might see a dataframe of 6 rows (since you have 3 groups x 2 condition). After that, you can create vectors that represents the mean of a particular combination of your factors:

HO_Related = c(1, 0, 0, 0, 0, 0)
LO_Related = c(0, 1, 0, 0, 0, 0)
Y_Related = c(0, 0, 1, 0, 0, 0)
HO_Unrelated = c(0, 0, 0, 1, 0, 0)
LO_Unrelated = c(0, 0, 0, 0, 1, 0)
Y_Unrelated = c(0, 0, 0, 0, 0, 1)


Then, you can create your custom contrasts by manipulating these vectors, using the contrast function from emmeans:

contrast(this_emm, method=list(
"CONTRAST1" = (HO_Related - HO_Unrelated) - (LO_Related - LO_Unrelated),
"CONTRAST2" = (HO_Related - HO_Unrelated) - (Y_Related - Y_Unrelated)),