-
Notifications
You must be signed in to change notification settings - Fork 1
gFormulaImpute with custom method fails on mice 3.18 #4
Description
Firstly, I wanted to thank you for all your fantastic work on g-methods and multiple imputation!
Following the recent mice package dependency update from 3.17.0 to 3.18.0, gFormulaImpute() is no longer compatible with mice when a user supplies a custom method.
Reproducing the issue
The following code executes correctly with mice 3.17.0, but throws an error with mice 3.18.0:
# Packages --------------------------------------------------------------------------------------------------------
library(mice)
library(gFormulaMI)
# Reproduce the bug -----------------------------------------------------------------------------------------------
set.seed(7626)
imp1 <- gFormulaImpute(
data = simDataFullyObs,
M = 1,
trtVars = c("a0", "a1", "a2"),
trtRegimes = list(c(0,0,0), c(1,1,1))
)
meth <- imp1$method
pred <- imp1$predictorMatrix
# This will throw an error even for mice v. 3.17.0 because the method vector includes 'regime', which is not accepted by the function.
try(
gFormulaImpute(
data = simDataFullyObs,
M = 1,
trtVars = c("a0", "a1", "a2"),
trtRegimes = list(c(0,0,0), c(1,1,1)),
method = meth,
predictorMatrix = pred
)
)
# This was a workaround with mice 3.17.0, which no longer works with mice 3.18.0.
# Remove 'regime' from method
meth2 <- meth[!grepl("regime", names(meth))]
# This will throw an error for mice v. 3.18.0 but not earlier versions
try(
gFormulaImpute(
data = simDataFullyObs,
M = 1,
trtVars = c("a0", "a1", "a2"),
trtRegimes = list(c(0,0,0), c(1,1,1)),
method = meth2,
predictorMatrix = pred
)
)Output with mice 3.18.0:
[1] "Input data is a regular data frame."
Error : All elements of 'method' must be named and match blocks.
A potential fix
I believe I may have found the source of this issue. From mice 3.18.0 onward, mice() insists that its internal blocks vector has the same names and order as the column names in the data frame.gFormulaImpute() creates a new column called regime and adds an empty element to the user-supplied method vector intended for 'regime', but this element is not named.
Below is a potential solution that also allows gFormulaImpute() to accept a method vector both with or without the regime element. I am not sure this covers every scenario, so I have opened this as an Issue and not a Pull request. The fix is for lines 192-195 and 242-245 of the original gFormulaImpute() function, so this change needs to be applied at two places. This change should also keep the function compatible with mice 3.17.
- #add on an empty imputation method for the new variable regime
- method <- c(method,"")
+ if (!"regime" %in% names(method)) {
+ #add on an empty imputation method for the new variable regime
+ method <- c(method, regime = "")
+ }Let me know if this works and if you'd like me to open a pull request.