compare differences between conditions with emmeans

With the following model

subject <- factor(rep(c(1,2,3,4,5,6,7),each=4, times=2))
dep <- c(5,4,9,3,4,4,2,1,10,7,8,7,1,2,1,1,5,10,1,7,3,2,1,4,3,8,7,3,1,1,2,1,15,10,20,11,2,2,1,3,11,12,9,7,2,3,1,2,11,9,8,9,3,4,2,1)

f1 <- factor(rep(c(rep("Female",times=16),rep("Male",times=12)), times=2))
f2 <- factor(rep(c("day1","day2","day3","day4"),times=14))

data <- data.frame(sub=subject, dep=dep, f1=f1, f2=f2)

m <- lmer(dep ~ f1*f2 + (1|sub), data=data)


I calculated contrasts with emmeans

pairs(emmeans(m, ~f2|f1))


Now I'd like to be able to compare females and males for contrast day1 - day2. Basically compare

f1 = Female:
contrast      estimate       SE df t.ratio p.value
day1 - day2  0.8750000 2.135136 43   0.410  0.9765


with

f1 = Male:
contrast      estimate       SE df t.ratio p.value
day1 - day2 -1.5000000 2.465443 43  -0.608  0.9289


Is there a way to do this?

• Can't you just read this right off the summary of fixed effects? It should be on the line f1Male:f2day2 in the output of summary(m). – whuber Nov 6 '18 at 21:45
• Thanks @whuber, that's right, I keep forgetting the summary function. I suppose I haven't quite understood how to read the output from summary adequately. My impression is that with rvl's answer you can easily test any simple effect you like, but not so with the summary(m). For example summary(m) does not provide the estimate of contrast day2-day3 for females vs males (which is given automatically with rvl's code), which suggests that additional manual calculations need to be performed – locus Nov 6 '18 at 23:50
• You can often recode the dummy variables so that the summary displays exactly the tests you want. Because that's not terribly flexible, most statistical software offers a post-hoc procedure to test any linear contrast. – whuber Nov 7 '18 at 14:04

Yes.

contrast(emmeans(m, ~f1*f2), interaction = “consec”)


But @whuber’s comment does apply in a case as simple as this one.

Or you may prefer

pairs(pairs(emmeans(m, ~ f2|f1)), by = NULL)


That is, compute the results you already have, remove the by grouping, and compare the two pairwise differences.

• That's great @rvl, many thanks! The last code gives me an error though Error in as.data.frame.default(x) : cannot coerce class ‘structure("lmerModLmerTest", package = "lmerTest")’ to a data.frame – locus Nov 6 '18 at 23:39
• Oops, left out the emmeans call. I fixed it – Russ Lenth Nov 6 '18 at 23:54
• your code works great if I want to test every single pairwise difference. However, what if I'm interested in only two of the contrasts, say day1 - day2,Female - day1 - day2,Male and day1 - day3,Female - day1 - day3,Male while adjusting just for these two contrasts? – locus Sep 27 '19 at 13:36
• Use something like contrast(pairs(...), "trt.vs.ctrl1", exclude = 4, by = NULL). See help("contrast") and help("contrast-methods") – Russ Lenth Sep 27 '19 at 14:52
• Oops, I think I have it backwards (may I encourage you to use meaningful factor names?). Use something like emm <- emmeans(m, ~f2|f1); pairs(contrast(emm, "ctrl.vs.trt1", exclude = "day4"), by = NULL) – Russ Lenth Sep 27 '19 at 15:09