Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mi2stata

Exports completed data in Stata (.dta) or comma-separated (.csv) format


Description

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.

Usage

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

Arguments

imputations

Object of mi-class

m

The number of completed datasets to append onto the raw data

file

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.

missing.ind

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.

Details

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.

Value

NULL

Author(s)

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

Examples

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)

mi

Missing Data Imputation and Model Checking

v1.0
GPL (>= 2)
Authors
Andrew Gelman [ctb], Jennifer Hill [ctb], Yu-Sung Su [aut], Masanao Yajima [ctb], Maria Pittau [ctb], Ben Goodrich [cre, aut], Yajuan Si [ctb], Jon Kropko [aut]
Initial release
2015-04-16

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.