harrelfe / Hmisc

Harrell Miscellaneous
Other
204 stars 81 forks source link

`fit.mult.impute(fit.reps=TRUE)` with MICE errors out (`invalid 'length' argument`) #152

Closed krassowski closed 1 year ago

krassowski commented 2 years ago

In R 4.0 and R 4.1 calling fit.mult.impute results in:

Error in vector("list", n.impute) : invalid 'length' argument` if `n.impute

if n.impute is not explicitly specified. Here is a reproducer:

set.seed(1)
x1 <- factor(sample(c('a', 'b', 'c'), 100, TRUE))
x2 <- (x1=='b') + 3 * (x1=='c') + rnorm(100)
y  <- x2 + 1 * (x1=='c') + rnorm(100)
x1[1:20] <- NA
x2[18:23] <- NA
d <- data.frame(x1, x2, y)
f <- mice(d, print=FALSE)
# works
h <- fit.mult.impute(y ~ x1 + x2, lm, f, data=d)
# works
h <- fit.mult.impute(y ~ x1 + x2, lm, f, data=d, fit.reps=TRUE, n.impute=f$m)
# does not work
h <- fit.mult.impute(y ~ x1 + x2, lm, f, data=d, fit.reps=TRUE)

This is because when MICE is passed n.impute is only defined (line 17) after it is used (line 13):

https://github.com/harrelfe/Hmisc/blob/408d3957a313b4033b7e5eea48c4887e6990a614/R/fit.mult.impute.s#L13-L17

The fix could be as simple as moving line 13 after line 17.

harrelfe commented 1 year ago

Thanks for this and for the fix which will be in Hmisc 4.7-1