Search code examples
rmissing-dataimputationr-mice

Input data must have class mids


I'm working on a school project where I need to impute missing data and after the imputation with mice I'm trying to produce completed data sets with the complete-function.

When I run them one by one everything works fine, but I'd like to use a for-loop in case I want to have more than just m = 5 imputations. Now, when trying to run the for-loop, I always get the error

Error in complete(imputation[1]) : Input data must have class 'mids'.

However when I look up the class it is mids, what's going wrong here?

This is my code:

imputation <- mice(data = data, m = 5, method = "norm", maxit = 1, seed = 500) 
m <- 5
for(i in 1:m){
  completeData[m] <- complete(imputation[m])
  print(summary(completeData[m]))
}

Could someone maybe help me out here?


Solution

  • We are getting error because the class is not mids:

    imputation[1]
    # $call
    # mice(data = walking, m = 5, maxit = 0, seed = 500)
    
    class(imputation[1])
    # [1] "list"
    

    From the manual for ?complete:

    Usage

    complete(x, action = 1, include = FALSE)

    library(mice)
    
    # dummy data imputation
    data(walking)
    imputation <- mice(walking, max = 0, m = 5, seed = 500)
    
    # using for loop
    m <- 5
    for(i in 1:m){
      completeData <- complete(imputation, m)
      print(summary(completeData))
    }
    
    # I prefer to use lapply
    lapply(seq(imputation$m), function(i) summary(complete(imputation, i)))