mi2stata: Exports completed data in Stata (.dta) or comma-separated...

Description Usage Arguments Details Value Author(s) See Also Examples


This function exports completed data from an object of mi-class in which m completed data.frames are appended to the end of the raw data. Two additional variables are added which indicate the row number and distinguish the data.frames. The outputed file is either Stata (.dta) or comma-separated (.csv) format, and can be easily registered in Stata as multiply imputed data.


mi2stata(imputations, m, file, missing.ind=FALSE, ...)



Object of mi-class


The number of completed datasets to append onto the raw data


The filename, either a full path or relative to the working directory, where the file will be saved. Filenames must end in either '.dta' or '.csv'. Files with names ending in '.dta' will be saved as a Stata data file, and files with names ending in '.csv' will be saved as a comma-separated file.


If TRUE, includes a binary variable for each variable with NA values, indicating the observations which were originally missing. Defaults to FALSE.


Further arguments passed to write.dta for Stata files, or to write.table for .csv files.


The function calls complete to construct m completed data.frames, and uses rbind to append them to the bottom of the raw data that still contains all of the missing values. Two new variables are added: _mi, which contains the observation numbers; and _mj, which indexes the data.frames.

To save a Stata .dta file, end the filename with '.dta'. To save a comma-separated file, end the filename with .csv'. Stata files are loaded into Stata using Stata's use command, and comma-separated files can be loaded by typing insheet using filename, comma names clear. Once the file is loaded into Stata, the data must be registered as multiply imputed before any subsequent analyses can be performed. In Stata version 11 or later, type mi import mice to register the data. The _mi and _mj variables will be replaced by variables named _mi_id and _mi_m respectively. In Stata version 10 or earlier, install the MIM package by typing findit mim and installing package st0139_1. Then the prefix mim: must be added to any command using the multiply imputed data.

Any observations which are unpossible (legitimately skipped, and are not imputed, see missing_variable) will remain missing in the complete data, but will not be indicated as missing by these variables. If there are any unpossible values, missing indicators are included automatically.




Ben Goodrich and Jonathan Kropko, for this version, based on earlier versions written by Yu-Sung Su, Masanao Yajima, Maria Grazia Pittau, Jennifer Hill, and Andrew Gelman.

See Also

complete, mi, write.dta, write.table


fn <- paste(tempfile(), "dta", sep = ".")
if(!exists("imputations", env = .GlobalEnv)) {
  imputations <- mi:::imputations # cached from example("mi-package")
mi2stata(imputations, m=5, file=fn , missing.ind=TRUE)

Example output

Loading required package: Matrix
Loading required package: stats4
mi (Version 1.0, packaged: 2015-04-16 14:03:10 UTC; goodrich)
mi  Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Trustees of Columbia University
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under the General Public License version 2 or later.
Execute RShowDoc('COPYING') for details.
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.

mi documentation built on May 1, 2019, 10:13 p.m.