amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
447 stars 108 forks source link

futuremice error with blocks - possibly due to how ibind deals with blocked imputations #625

Closed fredho-42 closed 8 months ago

fredho-42 commented 8 months ago

Hi all,

i encountered an error when using futuremice() with blocks:

library(mice, warn.conflicts = FALSE)

x <- futuremice(
  nhanes, m=5, n.core=5, maxiter=2, parallelseed=1, 
  blocks=list(c('age'), c('bmi', 'hyp', 'chl')), 
  predictorMatrix=matrix(rep(1, 8), nr=2)
  )

Error in `colnames<-`(`*tmp*`, value = 1:imp$m) :    attempt to set 'colnames' on an object with less than two dimensions

Digging a little deeper i found the imp object that were combined using ibind() function did not contain valid imputed values for variables in blocks. An example:

One mids object from a future:

Browse[2]> imps[[1]]$imp
$age
[1] 1
<0 rows> (or 0-length row.names)

$bmi
      1
1  30.1
3  29.6
4  22.7
6  22.5
10 27.5
11 25.5
12 28.7
16 30.1
21 20.4

Combined mids object:

Browse[2]> imp$imp
$age
[1] 1 1 1 1 1
<0 rows> (or 0-length row.names)

$bmi
NULL

$hyp
NULL

$chl
NULL

Looking at ibind(), it seems the problem is all the separately imputed data had a visitSequence using block names, e.g. "age" "B1". The visitSequence has a lower length (2 in this example) than the actual number of variables (4 in this example) being imputed so the for loop in Line 68 couldn't combine all imputed variables.

Replacing

Line 65 with visitSequence <- colnames(x$predictorMatrix), Line 75 with chainMean <- chainVar <- initialize.chain(colnames(x$predictorMatrix), iteration, m), and Line 91 with visitSequence = x$visitSequence,

seems to get my codes work.

Edit: it seems that the latest version on Github has already fixed this issue except when some included variables were not in the block. i've made a comment on the commit.

Fred