Erdne Htábrob
Erdne Htábrob

Reputation: 879

interaction contrast with glmer

I am running a model with a similar structure:

model <- glmer(protest ~ factora*factorb*numeric+factora+factorb+numeric+1 + (1 + factor1|level1) + (1|level2), data=data, family=binomial(link='logit'))

where factora and factorb are factor variables, numeric is a numerical variable.

I am curious of the statistical significance of the contrast in the interaction while holding factora constant at 3, between two values of factorb (1-5) across the range of the numerical value.

I have tried the following options with no luck:

library(psycho)
get_contrasts(model, formula="factora:factorb:numeric", adjust="tukey")
View(contrasts$contrasts) 

this works, but unfortunately the results hold numeric constant and only vary factora and factorb. Therefore, it does not answer my question.

I have also tried:

library(multcomp)
test = glht(model,linfct=mcp("factora:factorb:numeric"="Tukey"))

this yields the error of

Error in mcp2matrix(model, linfct = linfct) : 
  Variable(s) ‘factora:factorb:numeric’ have been specified in ‘linfct’ but cannot be found in ‘model’! 

without regard of the way I specify the interaction and despite other functions like get_contrasts finding the interaction specified the same way.

I have also tried:

library(emmeans)
contrast(m.3[[2]], interaction = c("factora", "factorb", "numeric"))

this however does not support glmer.

Any ideas?

Upvotes: 1

Views: 2541

Answers (1)

Russ Lenth
Russ Lenth

Reputation: 6780

There are a couple of issues here that are tripping you up.

One is that we don't really apply contrasts to numeric predictors. Numeric predictors have slopes, not contrasts; and if you have a model where a numeric predictor interacts with a factor, that means that the slope for the numeric predictor is different for each level of the factor. The function emtrends() in the emmeans package can help you estimate those different slopes.

The second is that the interaction argument in emmeans::contrast() needs a specification for the type of contrasts to use, e.g., "pairwise". The factors to apply them to are those in the emmGrid object in the first argument.

So... I think maybe you want to try something like this:

emt <- emtrends(model, ~ factora*factorb, var = "numeric")
emt    # list the estimated slopes

contrast(emt, interaction = "consec")
    # obtain interaction contrasts comparing consecutive levels of the two factors

Upvotes: 2

Related Questions