Solved – Setting custom three-way interaction contrasts in R

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?

This can be done in the lsmeans package pretty simply:

lsm = lsmeans(fit, ~A*B|C) contrast(lsm, interaction = "pairwise") 

This code generates and tests the contrast with coefficients $(1,-1,-1,1)$ at each level of factor $C$. This contrast is generated by taking the product of coefficients $(1,-1,1,-1)$ (for factor $A$) and $(1,1,-1,-1)$ (for factor $B$).

Similar Posts:

Rate this post

Leave a Comment

Solved – Setting custom three-way interaction contrasts in R

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?

Best Answer

This can be done in the lsmeans package pretty simply:

lsm = lsmeans(fit, ~A*B|C) contrast(lsm, interaction = "pairwise") 

This code generates and tests the contrast with coefficients $(1,-1,-1,1)$ at each level of factor $C$. This contrast is generated by taking the product of coefficients $(1,-1,1,-1)$ (for factor $A$) and $(1,1,-1,-1)$ (for factor $B$).

Similar Posts:

Rate this post

Leave a Comment