Ricardo Yomez
Ricardo Yomez

Reputation: 11

How to do specific, custom contrasts in EMMEANs with multiple nested factor levels but without subsetting data

Here is my data frame (my real DF has way more data points):

    rearing.temp<-c("15", "15", "15", "15", "19", "19", "19", "19")
    source<-c("field", "field", "woods", "woods", "field", "field", "woods", "woods")
    runway.temp<-c("40","20","40","20","40","20","40","20")
    velocity<-c("2.3", "2.1", "1.9", "1.9", "2.3", "2.2", "2.3", "2.0")
    snail<-data.frame(rearing.temp, source, runway.temp, velocity)

Here is my model:

mod <- lmer(velocity ~ runway.temp*source*rearing.temp + (1|family) + (1|collection site) + (1|individual.plus.family.id.combined), data=snail)

When I do an emmeans contrast:

emmeans(mod, pairwise~runway.temp*source*rearing.temp)

I get 28 different comparisons, but I am only interested in looking at the difference between the velocity of field snails reared at 15° tested at the 40° runway temperature compared to woods snails reared at 15° tested at the 40° runway temperature. I just want to do one comparison between the snails that are both reared at the same temp, are tested at the same temp, but are sourced from different habitats. How can I do this?

Thank you,

Ricardo

Upvotes: 0

Views: 4342

Answers (1)

Russ Lenth
Russ Lenth

Reputation: 6760

The contrast function provides for specifying a named list of desired contrast coefficients. In your case, apparently you have 8 means (2x2x2 array), and they are arranged in order with the first index alternating fastest and the last factor the slowest - (1,1,1), (2,1,1), (1,2,1), ..., (2,2,2). So do something like this:

emm <- emmeans(mod, ~runway.temp*source*rearing.temp)
custom <- list(`111vs211` = c(1,0,0,0,-1,0,0,0),
               `211vs121` = c(0,1,-1,0,0,0,0,0))
contrast(emm, custom)

There is abundant documentation and examples. See for example ? contrast and vignette("contrasts", "emmeans")

Upvotes: 1

Related Questions