rimportexportstatar-mice

Importing MICE object to Stata for analysis


I am trying to use imputed data created with MICE in Stata.

My understanding of the steps are:

1) converting the mids object to mi in R

 m=20
 completed=lapply(1:20,function(i)complete(imp,i))
 completed.mi=do.call(Zelig::mi,completed)

2) preparing mice object for exporting in R

(a) mi2stata

 STATA=mi::mi2stata(completed.mi, m=20, file="C:\\Users\\STATA.csv", 
 missing.ind = FALSE)
 Note: after loading the data into Stata, version 11 or later, type 'mi 
 import ice' to register the data as being multiply imputed. 
 For Stata 10 and earlier, install MIM by typing 'findit mim' and include 
 'mim:' as a prefix for any command using the MI data.
 Error in lapply(X = X, FUN = FUN, ...) : 
 trying to get slot "data" from an object (class "mi") that is not an S4 
 object 

(b) Following the suggestion from below to write a csv without mi2stata:

   data_out <- data.table::rbindlist(completed, idcol="m")
   write.csv(data_out, "C:\\deleted\\STATA2.csv", row.names=FALSE)

3) importing the CSV file of the original, nonimputed data into Stata

**appears to have worked fine. all variables from CSV file appears on the right-hand side

4) use mi import ice command in Stata

(a) error re: mi2stata (I had actually imported the non-imputed file)

. mi import ice STATA
varlist not allowed
r(101);

(b) error in reading CSV version of imputed data

 mi import ice[stata2]
 weights not allowed
 r(101);

I have encountered errors with 2, 4, and possibly 1 (as error for 2 refers back to conversion of mice object to mi class data). I would really appreciate a user friendly step by step guidance. Although mi2stata might not work directly work for mice objects, I am still interested in learning a solution for this.


Solution

  • Collecting the comments above: you can't use mi::mi2stata with either the data that results from Zelig::mi or from mice::complete. But if you look at the code for mi::mi2stata, it just seems to stack the raw data, and each imputed dataset. It then adds indices to mark each dataset, and each observation.

    library(mice)
    # don't really need data.table but makes adding the indices easier
    library(data.table) 
    
    # Function to export mice imputed datasets
    mice2stata <- function(imp, path="stata", type="dta"){ 
    
              completed <- lapply(seq_len(imp$m),function(i) complete(imp,i))     
              data_out <- rbindlist(completed, idcol="_mj")     
              data_out <- rbind(imp$data, data_out, fill=TRUE)     
              data_out[, `_mj` := replace(`_mj`, is.na(`_mj`), 0L)]     
              data_out[, `_mi` := rowid(`_mj`)]     
              if(type=="dta") {
    
                  foreign::write.dta(data_out, file=paste(path, type, sep="."))             
    
              } else {
    
                  write.csv(data_out, file=paste(path, type, sep="."), na="", row.names=FALSE)
              }
    
        }
    

    An example

    imp <- mice(nhanes, m=2, print=FALSE) 
    mice2stata(imp, type="dta")
    

    Then in Stata use

    use path\to\stata.dta 
    mi import ice