-3

I am using the "mi" package for imputation of missing values. I have run the following code:

'mi' package code

            library(mi)                 
            imp_rd<-mi(rd1) ## rd1 is my data file containing 7 variables.
            summary(imp_rd)
            hist(imp_rd)

Now, I want to save the output of "imp_rd" (which is my imputed data file) as .csv file. Any one who will help me regarding this problem.

2
  • Possible duplicate of Export CSV without col.names Oct 7, 2015 at 11:15
  • You can access the imputed dataset using ?complete - if you have 4 chains use complete(imp_rd, 1:4) to produce a list of 4 compete dataframes, (with missing patterns indicated). Write these out for(i in 1:4) write.csv(complete(imp_rd, i), paste0("imp_,", i, ".csv" ))
    – user20650
    Oct 7, 2015 at 12:02

2 Answers 2

1

if you want to export imputed data-sets generated by the model that mi estimated, a good way to do it is by using the mi2stata command, which allows you to export to either a .dta or a .csv format.

But remember not to think about exporting "one" imputed data set. The whole point of multiple imputation is that you can get a bunch of different imputed data sets that will allow you to account for the uncertainty induced by the missing data that you originally had.

So be sure to specify how many imputed data sets you want to export and the path where you want to save the imputed data. In the following example I chose to generate 10 imputed data sets.

library(mi)
imp_rd<-mi(rd1) 
mi2stata(imp_rd, m=10, "pathtofile/imp_rd.csv")

Hope you find this useful.

0

if your output file is a dataframe you can use:

write.csv(imp_rd, file = "imp_rd.csv", sep = ",")

this should save file in csv in your working directory

thanks

5
  • Thanks for your guidance... but the write.csv(imp_rd,........) not work for the above output... Also I have run the "for" loop but did not get the result in appropriate format... It seems to be work but not according to my requirement.... I just need the imputed results... but the 'for' loop provided the results with many other un-necessary informations. So if possible the please help me to find just the FINAL results... Thanks
    – Zeeshan
    Oct 7, 2015 at 20:38
  • is your 'imp_rd' is a dataframe?
    – Achak
    Oct 8, 2015 at 11:36
  • Dear Achak, "rd1" is dataframe but I did not exactly know that whether the out of "mi" imputed result will be in the format of dataframe or not. As earlier working with other imputation packages like VIM the output is in the format of dataframe. I have got the following error after running the code: The R-Language code is: The R-code is: library(mi) # for "mi" imputation package imp_rd<-mi(rd1) summary(imp_rd) hist(imp_rd) write.table(imp_rd, file="rd123.csv", sep="\t", col.names=F, row.names=F)
    – Zeeshan
    Oct 8, 2015 at 18:22
  • Dear Achak, "rd1" is dataframe but I did not exactly know that whether the output of "mi" imputed values will be in the format of dataframe or not. For instance the output of VIM package is in the format of dataframe. The error after running the code: library(mi) # for "mi" package imp_rd<-mi(rd1) write.table(imp_rd, file="rd123.csv", sep="\t", col.names=F, row.names=F) Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class "structure("mi", package = "mi")" to data.frame
    – Zeeshan
    Oct 8, 2015 at 18:33
  • Zeeshan .... have a look at str(imp_rd) - it is not in the correct format to use write.table directly. You need to use complete to extract the dataframes, as in the above comment. If you do not need the imputation information appended as additional columns just select the original columns in a two-step process. If this iis not what you want you need to give a bit more info.
    – user20650
    Oct 10, 2015 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.