plotting an interaction term in moderated regression using MICE imputation

Viewed 582

I'm using imputed data to test a series of regression models, including some moderation models.

Imputation

imp_data <- mice(data,m=20,maxit=20,meth='cart',seed=12345)

I then convert this to long format so I can recode / sum variables as needed, beore turning back to mids format

impdatlong_mids<-as.mids(impdat_long)

Example model:

model1 <- with(impdatlong_mids,
               lm(Outcome ~ p1_sex + p2 + p3 + p4
                  + p5+ p6+ p7+ p8+ p9+ p10
                  + p11+ p1_sex*p12+ p1_sex*p13 + p14)

in non-imputed data, to create a graphic representation of the significant ineraction, I'd use (e.g.)

interact_plot (model=model1, pred = p1_sex, modx = p12)

This doesn't work with imputed data / mids objects. Has anyone plotted an interaction using imputed data, and able to help or share examples?

Thanks

EDIT: Reproducible example

library(tidyverse)
library(interactions)
library(mice)
# library(reprex) does not work with this
set.seed(42)
options(warn=-1)

#---------------------------------------#
# Data preparations

# loading an editing data
d <- mtcars
d <- d %>% mutate_at(c('cyl','am'),factor)

# create missing data and impute it
mi_d <- d
nr_of_NAs <- 30
for (i in 1:nr_of_NAs) {
  mi_d[sample(nrow(mi_d),1),sample(ncol(mi_d),1)] <- NA
}


mi_d <- mice(mi_d, m=2, maxit=2)
#---------------------------------------#
# regressions

#not imputed
lm_d <- lm(qsec ~ cyl*am + mpg*disp, data=d)

#imputed dataset
lm_mi <- with(mi_d,lm(qsec ~ cyl*am + mpg*disp))
lm_mi_pool <- pool(lm_mi)

#---------------------------------------#
# interaction plots
# not imputed

#continuous
interactions::interact_plot(lm_d, pred=mpg,modx=disp, interval=T,int.width=0.3)


#categorical
interactions::cat_plot(lm_d, pred = cyl, modx = am)

Sample continuous and categorical plots

#---------------------------------------#
# interaction plots
# imputed

#continuous
interactions::interact_plot(lm_mi_pool, pred=mpg,modx=disp, interval=T,int.width=0.3)
# Error in model.frame.default(model) : object is not a matrix

#categorical
interactions::cat_plot(lm_mi_pool, pred = cyl, modx = am)
# Error in model.frame.default(model) : object is not a matrix

The problem seems to be that neither interact_plot, cat_plot or any other available package allows for (at least categorical) interaction plotting with objects of class mipo or pooled regression outputs.

2 Answers

I am using the walking data from the mice package as an example. One way to get the interaction plot (well version of one type of interaction plot) is to use the gtsummary package. Under the hood it will take the model1 use pool() from mice to average over the models and then use a combo of tbl_regression() and plot() to output a plot of the coefficients in the model. The tbl_regression() function is what is calling the pool() function.

library(mice)
library(dplyr)
library(gtsummary)
imp_data <- mice(mice::walking,m=20,maxit=20,meth='cart',seed=12345)

model1 <- with(imp_data,
     lm(age ~ sex*YA))


model1 %>% 
      tbl_regression() %>% 
      plot()

plot from mice model

The package emmeans allows you to extract interaction effects from a mira object. Here is a gentle introduction. After that, the interactions can be plotted with appropriate ggplot. This example is for the categorical variables but could be extended to the continous case - after the emmeans part things get relatively straighforward.

library(ggplot2)
library(ggstance)
library(emmeans)
library(khroma)
library(jtools)


lm_mi <- with(mi_d,lm(qsec ~ gear*carb))

#extracting interaction effects
emcatcat <- emmeans(lm_mi, ~gear*carb)
tidy <- as_tibble(emcatcat)

#plotting
pd <- position_dodge(0.5)

ggplot(tidy, aes(y=gear, x=emmean, colour=carb)) + 
geom_linerangeh(aes(xmin=lower.CL, xmax=upper.CL), position=pd,size = 2) +
geom_point(position=pd,size = 4)+
ggtitle('Interactions') +
labs (x = "aggreageted interaction effect") +
scale_color_bright() +
  theme_nice()

this can be extended to a three-way interaction plot with facet_grid as long as you have a third categorical interaction term.

Categorical interaction plot

Related