Search code examples
rdatasetimputationr-micehmisc

How extract several imputed values from several variables using mice or another package in R into a single dataset?


From the multiple imputation output (e.g., object of class mids for mice) I want to extract several imputed values for some of the imputed variables into a single dataset that also includes original data with the missing values.

Here are sample dataset and code:

library("mice")
nhanes
tempData <- mice(nhanes, seed = 23109) 

Using the code below I can extract these values for each variable into separate datasets:

age_imputed<-as.data.frame(tempData$imp$age) 
bmi_imputed<-as.data.frame(tempData$imp$bmi) 
hyp_imputed<-as.data.frame(tempData$imp$hyp) 
chl_imputed<-as.data.frame(tempData$imp$chl) 

But I want to extract several variables to preserve the order of the rows for further analysis.

I would appreciate any help.


Solution

  • Use the complete function from mice package to extract the complete data set including the imputations:

    complete(tempData, action = 1)       
    

    action argument takes the imputation number or if you need it in "all", "long" formats etc. Refer R documentation.