1
votes

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.

2
you can't use mi::mi2stata with the data that results from Zelig::mi. Could you export them as csv's and then read in to stata? (I don't have stata so don't know what format it requires) - user20650
a quick example to see if it works. .. library(mice); imp = mice(nhanes, m=2, print=FALSE); completed <- lapply(seq_len(imp$m),function(i) complete(imp,i)); data_out <- data.table::rbindlist(completed, idcol="m"); write.csv(data_out, "stata.csv", row.names=FALSE) . Perhaps this is enough for you to tweak to the correct format that stata requires. perhaps @NickCox can help with this. - user20650
actaully. this seems more inline with what mi2stata does: completed <- lapply(seq_len(imp$m),function(i) complete(imp,i)) ; data_out <- data.table::rbindlist(completed, idcol="_mi") ; data_out <- rbind(nhanes, data_out, fill=TRUE) ; data_out[, "_mi" := replace(_mi, is.na(_mi), 0)] ; data_out[, "_mj" := .I, by="_mi"] ; write.csv(data_out, "stata.csv", row.names=FALSE) , but I dont know if these additional steps are required) - user20650
@ksroogl ; exactly. You can't use mi::mi2stata on that data - you just can't!. A quick look at the function shows it tries to extract using @, ie S4 class, which your completed data is not of this type. Shouldn't be too hard to write your own. Try tweaking the code in the above comments. - user20650
neither. The code in the comments is to prepare the data to go into stata. (i wrote it out as a csv, but you could use foreign::write.dta). So you don't need to use mi2stata. I am not sure of what stata requires, but the above code may need tweaked. - user20650

2 Answers

2
votes

Q4 looks straightforward. The syntax for that command (not function) is documented as

   mi import ice [, options]

and so STATA looks like an attempt to specify a variable list. Where does that come from?

If Q2 failed, was the point of Q3 and Q4?

I hope that some R user can add some comments on Q2. On the face of it, you got an explicit error message, so do you think it's wrong?

2
votes

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