I have a
lmer model with three-way interaction and I want to set up a specific contrast testing for the significance of two-way interaction on each level of the third variable. I can do it by hand with a simple model, but I was hoping that there might be a more efficient way of doing this.
Here's an example:
library(lsmeans) library(lme4) #setting up data.frame mydata<-data.frame(expand.grid(subjid=1:10, A=c('0','1'), B=c('X','Y'), C=c('A','B','C','D'))) mydata$dv<-rnorm(nrow(mydata)) # and here is the model fit<-lmer(dv~A*B*C+(1|subjid), mydata)
The interaction I want to test can be written symbolically as
(X.0-Y.0)-(X.1-Y.1)|C, that is, a test for significance of the differences by B between levels of A at each level of C.
I can do it by including an interaction term instead of A and B in the model and setting up the contrasts by hand:
mydata$BA<-interaction(mydata$B,mydata$A) fit<-lmer(dv~BA*C+(1|subjid), mydata) lsmf<-lsmeans(fit, c('BA','C')) c_list <- list(c1 = c(0.5, -0.5, -0.5, 0.5, rep(0,12)), c2 = c(rep(0,4),0.5, -0.5, -0.5, 0.5, rep(0,8) ), c3 = c(rep(0,8),0.5, -0.5, -0.5, 0.5, rep(0,4) ), c4 = c(rep(0,12),0.5, -0.5, -0.5, 0.5 )) summary(contrast(lsmf, c_list), adjust = "holm")
But this is a very clumsy way, especially if there are more than four levels of C or there are other factors in the model. Moreover, with manual coding of BA the model becomes slightly different I think. So is there a better way for setting up such contrasts?