Last active
June 29, 2023 21:00
-
-
Save kbjarkefur/9de3b335cf0e8e40b8ff35087410ebe5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/************************************* | |
This gist shows how to use iebaltab to generate statistics and use them | |
in customized tex tables instead of using the iebaltab's default output | |
**************************************/ | |
******************************** | |
* Set up mock data example | |
* In this mock example we test for differences in means in blood-pressure | |
* between men and women in Stata's built-in mock example data bplong.dta | |
* Load built-in example data on blood pressure | |
sysuse bplong.dta, clear | |
* See that sex has categories 0 and 1 | |
* where 0 is male and 1 is female | |
tab sex, nol | |
******************************** | |
* Run iebaltab | |
* Run iebaltab on blood pressure variable using gender as the group variable. | |
* Specified like this, iebaltab generates no output, but it stores all | |
* calculated stats in a return matrix, where each row is a balance variable | |
* and each columns is a statistic | |
iebaltab bp, groupvar(sex) | |
* Save the restuls matrix in a matrix that is not a return value | |
mat iebt_mat = r(iebtab_rmat) | |
* Display the matrix. Row name correspond to the balance variable. | |
* Column name are on format value_group or value_pair. | |
* So mean_0 is the mean for obs with groupvar==0, | |
* and diff_1_0 is the diff between groups with groupvar==0 and groupvar==1. | |
mat list iebt_mat | |
******************************** | |
* Access values from the return matrix | |
* Its possible to access items by row and column index | |
di iebt_mat[1,3] | |
* A more verbose but also more human readable way to access values is | |
* to use rownumb() and colnum() to access items by name. | |
local row = rownumb(iebt_mat,"bp") //Get row for var bp | |
di el(iebt_mat,`row',colnumb(iebt_mat,"mean_0")) | |
* You can store these values in locals | |
* Build a loop around this section if using several variables | |
local bp_m0 = el(iebt_mat,`row',colnumb(iebt_mat,"mean_0")) | |
local bp_m1 = el(iebt_mat,`row',colnumb(iebt_mat,"mean_1")) | |
local bp_d0_1 = el(iebt_mat,`row',colnumb(iebt_mat,"diff_0_1")) | |
local bp_p0_1 = el(iebt_mat,`row',colnumb(iebt_mat,"p_0_1")) | |
di "`bp_m0'" // mean groupvar == 0 | |
di "`bp_m1'" // mean groupvar == 1 | |
di "`bp_d0_1'" // diff between groupvar == 0 and == 1 | |
di "`bp_p0_1'" // P-value for diff being significant | |
******************************** | |
* Manually build a custom latex table | |
* The rest of the code use these locals to manually build a tex table | |
*Create a temporary texfile | |
tempname texhandle | |
tempfile textmpfile | |
* Add results to one tex table row - any formatting needed is not included here | |
* Build a loop around this section if using several variables | |
local texrow_bp "bp & `bp_m0' & `bp_m1' & `bp_d0_1' & `bp_p0_1'" | |
* Write this comment at the top of each tex file | |
file open `texhandle' using "`textmpfile'", text write | |
file write `texhandle' /// | |
"\documentclass{article}" _n /// | |
"\begin{document}" _n /// | |
"\begin{table}" _n /// | |
"\begin{tabular}{lcccc}" _n /// | |
"\\[-1.8ex]\hline \hline \\[-1.8ex]" _n /// | |
"Variable & Male & Female & Difference & P Value \\" _n /// | |
"\hline \\[-1.8ex]" _n /// | |
"`texrow_bp' \\" _n /// | |
"\hline \\[-1.8ex]" _n /// | |
"\end{tabular}" _n /// | |
"\end{table}" _n /// | |
"\end{document}" _n | |
file close `texhandle' | |
* Write temporay tex file to disk and prepare success string | |
local output_tex_file "/write/your/path/and/filename/here.tex" | |
copy "`textmpfile'" "`output_tex_file'" , replace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment