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.
mi::mi2statawith the data that results fromZelig::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) - user20650library(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. - user20650completed <- 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@, 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