Search code examples
rmissing-datahmisc

How to add imputed data (w/ aregImpute) to a data frame?


My question is how do I go about adding the imputed data to the quakes.missing data frame?

I've created a reproducible example below.

library(Hmisc)
library(missForest) #load packages

data("quakes") 
quakes

quakes.missing <- prodNA(quakes, noNA = 0.1) #create missing values

summary(is.na(quakes.missing)) #confirm that data is missing

impute_quakes <- aregImpute(~ lat + long + depth + mag + stations, data = quakes.missing, n.impute = 5)

impute_quakes

Solution

  • Since you have 5 imputations, you'd have 5 full data frames, you can pull them out with a function like this:

    fill_data <- function(impute = impute_quakes, data = quakes.missing, im = 1) {
      cbind.data.frame(impute.transcan(x = impute, 
                                       imputation = im, 
                                       data = data, 
                                       list.out = TRUE, 
                                       pr = FALSE))
     }
    full_dat1 <- fill_data(im = 1)
    full_dat2 <- fill_data(im = 2)
    ...
    

    (also, I'm sure you are aware, but Hmisc also has a great function fit.mult.impute so you don't need to pull out full data frames in order to perform analyses)