rlsmeans

How to narrow down to specific contrasts with lsmeans


I would like to use contrast() with an lsmeans object to perform some specific planned comparisons, but I cannot find a method to perform the comparisons I want. I want to compare whether one factor has an effect at either of two factor levels. For example, in the below I would like to compare whether A,C differs to B,C, and whether A,D is different to B,D. I do not want to compare whether A, C is different to B, D or different to A, D, etc.

fac_one <- c(rep("A", 200), rep ("B", 200))
fac_two <- rep(c("C", "D"), 200)

dats <- data.frame(fac_one= c(rep("A", 200), rep ("B", 200)),
                   fac_two= rep(c("C", "D"), 200))

dats$y <- NA

dats$y[dats$fac_one=="A" & dats$fac_two=="C"] <- 
  rnorm(100, mean=0.9, sd=1)
dats$y[dats$fac_one=="B" & dats$fac_two=="C"] <- 
  rnorm(100, mean=0.9, sd=1)

dats$y[dats$fac_one=="A" & dats$fac_two=="D"] <- 
  rnorm(100, mean=0.6, sd=1)
dats$y[dats$fac_one=="B" & dats$fac_two=="D"] <- 
  rnorm(100, mean=1.4, sd=1)

mod <- lm(y ~ fac_one*fac_two, data = dats)
Anova(mod)

lsmns <- lsmeans(mod, ~fac_one*fac_two)
#currently does many contrasts that I do not want to do
contrast(lsmns)

Thanks!


Solution

  • contrast(lsmns, “pairwise”, by = “fac_two”)
    

    It’s in the documentation