user10338745
user10338745

Reputation:

How to restrict imputation of values during mice

I am using the mice package to impute data, and I have read about post processing to restrict imputed values however everything I have read is about restricting values to a certain range. One of the variables I am imputing is total volume of blood transfused. However not everyone has had a blood transfusion in my data set. I have this (transfused/not transfused) as a binary variable in my data. I need to make it so people with no blood transfused have values of NA. Below is what Ive done but it is not working

post["ml"]<-"imputedneonates[[j]][p$data$transfused[!r[,j]]==0,i]"<-NA

Upvotes: 1

Views: 651

Answers (1)

Wietze314
Wietze314

Reputation: 6020

It would make more sense to set the volume parameter to 0 for those that are not transfused. Here is an example:


require(mice)

set.seed(314)
transfusion <- data.frame(transfused = sample(c(1,0,NA), 100, prob = c(.8,.15,.05), replace = T),
                          age = rnorm(100,6,2)) %>%
  mutate(volume = if_else(transfused == 1,rnorm(nrow(.),250,50),as.numeric(NA))) %>%
  mutate(volume = if_else(sample(c(TRUE,FALSE),nrow(.),prob = c(0.1,0.9), replace = T),as.numeric(NA),volume))


ini <- mice(transfusion, maxit = 0)
post <- ini$post

post["volume"] <- "imp[[j]][data$transfused[!r[, j]] == 0, i] <- as.numeric(0)"
imp <- mice(transfusion, post=post, print=FALSE)

complete(imp)

Another suggestion would be to not impute transfused at all, but rather only impute the volume, and then calculate transfused after imputation or with passive imputation. I don't have much info on your data and the missing data, so this option might not be applicable.

Upvotes: 1

Related Questions