Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interact_plot with 3-way interaction error: contrasts can be applied only to factors with 2 or more levels #61

Open
marcondesfel opened this issue Aug 31, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@marcondesfel
Copy link

I am trying to use interact_plot function from the interactions package to visualize a 3-way interaction with survey data (svyglm object).

When I plot the following:

m <- svyglm(a ~ b * c * d,  design)
interact_plot(m, pred = c, modx = d, mod2 = b)

Model m works and I can get an interaction plot. When I try adding a covariate (factor variable e below) to my model m:

n <- svyglm(a ~ b * c * d + e,  design)
interact_plot(n, pred = c, modx = d, mod2 = b)

I cannot get the interact plot to run. I keep getting the error message: “contrasts can be applied only to factors with 2 or more levels”.

I have already checked for missing values in model n and I made sure that I have enough levels in the factor variable e, but still keep getting the error.

Any help in finding a solution is much appreciated.

Edit: Reproducible example

library(survey)
library(interactions)
data(api) #loading built-in dataset api

Building model with interaction

# survey objects 
dclus2<-svydesign(id=~dnum+snum, weights=~pw, data=apiclus2)
rclus2<-as.svrepdesign(dclus2)

# svyglm object
l <- svyglm(snum ~ stype * sch.wide * api99, family= gaussian, rclus2)

# interaction plot 
plot1 <- interact_plot(l, pred = api99 , modx = stype, mod2 = sch.wide)

plot1 above works. Now what I need is to add factor covariates to model l, like comp.imp. When I do that, I get the following error when I try interact_plot:

l2 <- svyglm(snum ~ stype * sch.wide * api99 + comp.imp, family= gaussian, rclus2)
interact_plot(l2, pred = api99 , modx = stype, mod2 = sch.wide)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

When I substitute the factor comp.imp for a numerical api00 variable

l3 <- svyglm(snum ~ stype * sch.wide * api99 + api00, family= gaussian, rclus2)
interact_plot(l3, pred = api99 , modx = stype, mod2 = sch.wide)

It also works and I can get the interaction plot to work.

@marcondesfel marcondesfel added the bug Something isn't working label Aug 31, 2022
@mrmvergeer
Copy link

mrmvergeer commented Aug 22, 2023

I have the same issue. First I thought it was related to this issue: scaled variables. But the error persisted. I checked the data: No NAs, the variables in the moderation are scaled metric measurements, not factor variables. The lmer-model itself runs fine. Any solutions available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants