jacob-long / jacob-long/interactions

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

Open
#61 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
R
Stars
135
Forks
19
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the reproducible R example using survey and interactions, comparing l, l2, and l3. Trace why interact_plot fails when comp.imp is included as a factor, and consider the issue complete when the l2 interaction plot runs without the contrasts error while the existing l and l3 cases still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.