Last active
April 8, 2021 10:35
-
-
Save josherrickson/5cbf9b55daf72c599bdb20f2c6d34359 to your computer and use it in GitHub Desktop.
Stata code to include additional statistics to multiple imputation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webuse mheart1s20, clear | |
* Run the input model, producing the results as well as returning the AIC/BIC | |
capture program drop modelPlusIC | |
program modelPlusIC, eclass properties(mi) | |
args model | |
quiet `model' | |
quiet estat ic | |
matrix S = r(S) | |
* Append the new AIC/BIC to IC | |
matrix savmat = (savmat \ S[1,5], S[1,6]) | |
end | |
* mi estimate the input model, as well as gathering the results from the IC matrix and displaying the results | |
capture program drop produceResults | |
program produceResults | |
args model | |
preserve | |
* Generate an empty matrix which we'll fill in with the AIC/BIC | |
matrix savmat = (0,0) | |
* Run the MI imputation | |
mi estimate: modelPlusIC "`model'" | |
* Get rid of the first 0,0 that we initialized the matrix with | |
quiet mi desc | |
matrix savmat = savmat[2..`=r(M)+1',1...] | |
* Save the results to the data | |
matrix colnames savmat = "AIC" "BIC" | |
svmat savmat, names(col) | |
* Summarize | |
mean AIC BIC | |
restore | |
end | |
* Pass the model as a string so it gets run properly inside. | |
produceResults "logit attack smokes age bmi" | |
produceResults "logit attack smokes age bmi hsgrad female" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code will produce the regular output of
mi estimate: ....
, as well as summarize the AIC/BIC on all imputed data sets.To generate different statistics, adjust lines 8-11 to produce and save the desired results, then adjust lines 20 and 29 to ensure the saving matrix has the right dimension.