Reputation: 85
I would like to probe an interaction in a moderation analysis between a categorical predictor and a continuous variable in a multiple imputed data set.
I would like to look at the effect of different levels of the continuous moderator (-1SD, Mean, +1SD).
I know that this is possible in SPSS with the PROCESS
macro, but unfortunately, it cannot handle multiple imputed data. The interaction
package can't handle it either. Does anyone have any ideas on how to solve this problem?
I have imputed the data with the mice
package, with m = 50.
My model looks like this:
lm(outcome ~ group * continuous_moderator)
Here is a reproducible example, using the windspeed dataset integrated in mice:
data <- mice::windspeed
data$group <- sample(c(0:1), replace = T, size = 433)
imp_data <- mice(data,
m=50,
maxit=20,
meth='cart',
seed=12345)
model1 <- with(imp_data,
lm(RochePt ~ group * Clones))
Thanks in advance for all your help and tips.
Upvotes: 0
Views: 371
Reputation: 17805
You can do this using [the marginaleffects
package.] (Disclaimer: I am the author.) See this vignette for details:
https://vincentarelbundock.github.io/marginaleffects/articles/multiple_imputation.html
The quick illustration below uses the by
argument which is available in the dev version (should be out on CRAN in the next couple weeks). Install it:
library(remotes)
install_github("vincentarelbundock/marginaleffects")
Then,
library(mice)
library(marginaleffects)
data <- mice::windspeed
# {marginaleffects} reserves the name "group", so I "group_id" instead
data$group_id <- sample(c(0:1), replace = T, size = 433)
fit <- function(x) {
mod <- lm(RochePt ~ group_id * Clones, data = x)
out <- comparisons(mod, variables = "Clones", by = "group_id")
# see vignette linked above for why we need the next line.
out$term <- out$group_id
return(out)
}
imp_data <- mice(data,
m = 5,
maxit = 20,
meth = "cart",
seed = 12345,
printFlag = FALSE)
imp_data <- complete(imp_data, "all")
mod <- lapply(imp_data, fit)
mice::pool(mod)
#> Class: mipo m = 5
#> term m estimate ubar b t dfcom df riv lambda
#> 1 1 5 0.9394599 0.002593162 0 0.002593162 433 430.9702 0 0
#> 2 0 5 1.0443336 0.003134239 0 0.003134239 433 430.9702 0 0
#> fmi
#> 1 0.004608611
#> 2 0.004608611
Edit: Here’s a minimal example using the newdata
argument and the datagrid()
function to assess contrasts conditional on a continuous moderator. You can then modify the fit
function I gave you before to integrate this in your multiple imputation workflow.
Please read the documentation to learn more. It is very detailed and the functions are super flexible: https://vincentarelbundock.github.io/marginaleffects/
library(marginaleffects)
mod <- lm(mpg ~ hp * factor(cyl), data = mtcars)
comparisons(
mod,
variables = "cyl",
newdata = datagrid(hp = c(100, 110, 120)))
#> rowid type term contrast_cyl comparison std.error
#> 1 1 response interaction 6 - 4 -4.792913 2.016159
#> 2 2 response interaction 6 - 4 -3.741286 2.025602
#> 3 3 response interaction 6 - 4 -2.689660 2.253712
#> 4 1 response interaction 8 - 4 -8.049775 2.314779
#> 5 2 response interaction 8 - 4 -7.064458 2.393402
#> 6 3 response interaction 8 - 4 -6.079140 2.563475
#> statistic p.value conf.low conf.high predicted_lo
#> 1 -2.377250 0.0174422739 -8.744511 -0.8413143 24.70544
#> 2 -1.847000 0.0647471270 -7.711393 0.2288198 23.57768
#> 3 -1.193436 0.2326988568 -7.106855 1.7275346 22.44992
#> 4 -3.477557 0.0005060048 -12.586658 -3.5128926 24.70544
#> 5 -2.951639 0.0031609267 -11.755439 -2.3734759 23.57768
#> 6 -2.371445 0.0177186636 -11.103458 -1.0548222 22.44992
#> predicted_hi cyl hp
#> 1 19.91252 8 100
#> 2 19.83639 8 110
#> 3 19.76026 8 120
#> 4 16.65566 8 100
#> 5 16.51322 8 110
#> 6 16.37078 8 120
Upvotes: 1