Skip to content

Instantly share code, notes, and snippets.

@josherrickson
Last active April 8, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josherrickson/5cbf9b55daf72c599bdb20f2c6d34359 to your computer and use it in GitHub Desktop.
Save josherrickson/5cbf9b55daf72c599bdb20f2c6d34359 to your computer and use it in GitHub Desktop.
Stata code to include additional statistics to multiple imputation
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"
@josherrickson
Copy link
Author

josherrickson commented Apr 8, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment