Perform an operation on each imputed data set in R MICE

How can I perform an operation (for example, a subset or adding a calculated column) for each imputed data set in an object of the mids class from the R mice package? I would like the result to still be the mids object.

Edit: Example

 library(mice) data(nhanes) # create imputed datasets imput = mice(nhanes) 

Imputed data sets are stored as a list of lists.

 imput$imp 

where there are lines only for observations with imputation for this variable.

The original (incomplete) data set is stored here:

 imput$data 

For example, how to create a new variable, calculated as chl/2 in each of the imputed data sets, which will give a new mids object?

+5
source share
3 answers

Another option is to calculate the variables before imputation and limit the space 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 according to chain equations in R

+3
source

This can be done easily as follows:

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

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

Perform any necessary manipulations:

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

use as.mids() to convert inverse managed data into a mids object:

  midsobj2 <- as.mids(long2) 

Now you can use midsobj2 as needed. Note that include=TRUE (used to include source data with missing values) is required for as.mids() to properly compress long format data. Please note that before v2.25 mice, an error occurred in the function as.mids () (see this post https://stats.stackexchange.com/a/158327/69413 )

EDIT: According to this answer fooobar.com/questions/1205918 / ... (from what is essentially a duplicate question) you can also edit the mids object directly by referring to $ 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 have problems if you want to multiply $ imp or want to use $ call, so I would not recommend this solution as a whole.

+4
source

There is an overload with which can help you here

 with(imput, chl/2) 

documentation is given in ?with.mids

+1
source

Source: https://habr.com/ru/post/1205915/


All Articles