0

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.

  • 1
    The errors seems like they are caused by your data, not the code. It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Aug 31 '22 at 13:16
  • @MrFlick thanks, I added a reproducible example using the api dataset. – Felippe Marcondes Aug 31 '22 at 14:00

0 Answers0