Perform operation on each imputed dataset in R's MICE

馋奶兔 提交于 2019-11-29 07:11:07

Another option is to calculate the variables before the imputation and place restrictions on them.

library(mice)

# Create the additional variable - this will have missing
nhanes$extra <- nhanes$chl / 2

# Change the method of imputation for extra, so that it always equals chl/2
# change the predictor matrix so only chl predicts extra
ini <- mice(nhanes, max = 0, print = FALSE)

meth <- ini$meth
meth["extra"] <- "~I(chl/2)"

pred <- ini$pred  # extra isnt used to predict
pred[ "extra", "chl"] <- 1

# Imputations
imput <- mice(nhanes, seed=1, pred = pred, meth = meth, print = FALSE)

There are examples in mice: Multivariate Imputation by Chained Equations in R

wjchulme

This can be done easily as follows -

Use complete() to convert a mids object to a long-format data.frame:

 long1 <- complete(midsobj1, action='long', include=TRUE)

Perform whatever manipulations needed:

 long1$new.var <- long1$chl/2
 long2 <- subset(long1, age >= 5)

use as.mids() to convert back manipulated data to mids object:

 midsobj2 <- as.mids(long2)

Now you can use midsobj2 as required. Note that the include=TRUE (used to include the original data with missing values) is needed for as.mids() to compress the long-formatted data properly. Note that prior to mice v2.25 there was a bug in the as.mids() function (see this post https://stats.stackexchange.com/a/158327/69413)

EDIT: According to this answer https://stackoverflow.com/a/34859264/4269699 (from what is essentially a duplicate question) you can also edit the mids object directly by accessing $data and $imp. So for example

 midsobj2<-midsobj1
 midsobj2$data$new.var <- midsobj2$data$chl/2
 midsobj2$imp$new.var <- midsobj2$imp$chl/2

You will run into trouble though if you want to subset $imp or if you want to use $call, so I wouldn't recommend this solution in general.

There is an overload of with that can help you here

with(imput, chl/2)

the documentation is given at ?with.mids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!