amices / mice

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

different variable imputed values using same predictor variables #455

Closed MonicaFialho closed 2 years ago

MonicaFialho commented 2 years ago

I would expect the imputed values of x to be the same if the same preditor variables were used, despite other variables being imputed or not, but it's not the case, as reproduced here:

library(data.table)
library(robustlmm)
library(mice)
library(miceadds)
library(magrittr)
library(dplyr)
library(tidyr)

set.seed(1)

# Data ------------------------------------

dt1 <- data.table(id = rep(1:10, each=3), 
                  group = rep(1:2, each=15),
                  time = rep(1:3, 10),
                  sex = rep(sample(c("F","M"),10,replace=T), each=3),
                  x = rnorm(30),
                  y = rnorm(30),
                  z = rnorm(30))

setDT(dt1)[id %in% sample(1:10,4) & time == 2, `:=` (x = NA, y = NA)][
           id %in% sample(1:10,4) & time == 3, `:=` (x = NA, y = NA)]

dt2 <- dt1 %>% group_by(id) %>% fill(y) %>% ungroup %>% as.data.table

# MI 1 ------------------------------------
pm1 <- make.predictorMatrix(dt1)
pm1['x',c('y','z')] <- 0
pm1[c('x','y'), 'id'] <- -2
imp1 <- mice(dt1, pred = pm1, meth = "2l.pmm", seed = 1, m = 2, print = F, maxit = 20)
# boundary (singular) fit: see ?isSingular - don't know how to interpret this (don't occur with my real data)
View(complete(imp1, 'long'))

# MI 2 ------------------------------------
pm2 <- make.predictorMatrix(dt2)
pm2['x',c('y','z')] <- 0
pm2['x', 'id'] <- -2
imp2 <- mice(dt2, pred = pm2, meth = "2l.pmm", seed = 1, m = 2, print = F, maxit = 20, remove.constant = F)
# imp2$loggedEvents report sex as constant (don't know why) so I include remove.constant=F to keep that variable (don't occur with my real data)                      
View(complete(imp2, 'long'))

In imp1:

In ìmp2:

Given so, why are the results different for the imputed data on x? Is it the expected behavior?

Thank you!

PS: I've posted this same question in StackOverflow (before I remember posting it here). Should I delete that post to avoid crossed posts or simply add there the link to here?

dmurdoch commented 2 years ago

I think the "2l.pmm" method isn't in mice, it's in miceadds, so you might want to post a third time :-).

MonicaFialho commented 2 years ago

I'm so lost... thank you @dmurdoch for pointing that out ;) I've opened a new issue: #24