Original Code
* Statistical programs often lack the built in capabilities to create tables for publication.
* In Stata, fortunately, the user community has stepped up and offered a number of solutions that hopefully will get the job done.
* In the next few posts, I will cover several packages that will present solutions to this problem.
* We will start by looking into the package estout.
* This command should install the package estout.
ssc install estout
* I strongly recommend reading the tutorial http://repec.org/bocode/e/estout/esttab.html
* The author of this exceptional Stata package is Ben Jann, ETH Zurich, jann@soz.gess.ethz.ch
* The package estout comes with several different commands.
* You use a combination of these commands in order to produce a final table for output.
* Let's generate some data:
clear
set obs 1000
gen x1 = rnormal()
gen x2 = rnormal()
gen x3 = runiform()
gen e = rnormal()*10
* Let's imagine a somewhat complex relationship
gen y1 = 2*x1 + 3*x2 + 4*x3^x2 + e
gen y2 = x1*x3 + x2 + e*x3
* Now let's estimate the relationship several ways.
* eststo - is short for estimate store:
eststo clear /* This clears the store estimates */
eststo: regress y1 x1
* We need not display the commands
eststo: qui regress y1 x1 x2
* eststo can be used as a prefix our to store estimates after a command
qui regress y1 x2 x3
eststo
qui regress y2 x1 x2 x3
eststo
* To observe the current collection of estimates:
esttab
* By default esttab displays t-stats. We can have it display standard errors instead.
esttab, se
* As well as r2
esttab, se r2
* I am copying the following straight from the online tutorial mentioned previously:
/* The t-statistics can also be replaced by p-values (p), confidence intervals (ci), or any parameter statistics contained in the estimates (see the aux() option). Further summary statistics options are, for example, pr2 for the pseudo R-squared and bic for Schwarz's information criterion. Moreover, there is a generic scalars() option to include any other scalar statistics contained in the stored estimates. For instance, to print p-values and add the overall F-statistic and information on the degrees of freedom, type: */
* It is also possible to display tables with only specified coefficients.
esttab, beta not
* It is also easy to have esttab display labels rather than variable names.
label var x1 "Explanatory Var 1"
label var x2 "Explanatory Var 2"
label var x3 "Wind speed"
label var y1 "Hair style"
label var y2 "Ability to spell synonyms"
esttab, label
* As well as specify names of the table and models as well as notes easily.
esttab, label ///
title(Table 1: Essential Results) ///
nonumbers mtitles("Hair Sytle A" "Hair Sytle B" "Hair Sytle C" "Ability to spell synonyms") ///
addnote("Source: comprehensive database on all things important")
* It might be useful to compress output to take up less screen space
esttab, compress
* You can change the star symbols and significance levels for significance:
esttab, star(" :/" .95 " :]" 0.35 " :D" 0.05 " :P" .0001) p compress
* Finally to the best part!
* esttab can easily output into excel or other database software:
esttab using example.csv
* It also keeps all of our formatting.
esttab using example.csv, replace label ///
title(Table 1: Essential Results) ///
nonumbers mtitles("Hair Sytle A" "Hair Sytle B" "Hair Sytle C" "Ability to spell synonyms") ///
addnote("Source: comprehensive database on all things important")
* It is also possible to export into a format readable directly by word (very cool)
esttab using example.rtf
* It is possible to append to the word document additional tables
esttab using example.rtf, append label compress
* You can even do a little format tweaking directly in Stata through rtf code!
* For example: the following command with make the table title bold and the note part after source italicized.
esttab using example.rtf, append label ///
title({\b Table 1: Essential Results }) ///
nonumbers mtitles("Hair Sytle A" "Hair Sytle B" "Hair Sytle C" "Ability to spell synonyms") ///
addnote("Source: {\i comprehensive database on all things important}")
* There is also some tools to play around with LaTeX for those who use LaTeX.
* It is also possible to view the internal call of the estout command by entering the option noisily
esttab, noisily notype
* This should allow the easy manipulation of additional options.
* Say rather than one line on top you want two lines before the header.
esttab, prehead(`"{hline @width}"' `"{hline @width} "')
* eststo can also be combined with a by prefix.
gen cat = rbinomial(3, .3)
* This will generate four different categories that we would like to run our estimation routine on.
gen cat_name = "Elephant" if cat==0
replace cat_name = "Zebra" if cat==1
replace cat_name = "Blue" if cat==2
replace cat_name = "Opera" if cat == 3
eststo clear
bysort cat_name: eststo: qui reg y1 y2 x1 x2
esttab, label nodepvar nonumber
* A very interesting option that eststo can do is save additional scalars for tabulation later.
eststo clear
qui regress y1 x2 x3
test x2=x3
eststo, addscalars(coef_equal r(p))
qui regress y1 x1 x2 x3
test x1=x2=x3
eststo, addscalars(coef_equal r(p))
esttab, scalars(coef_equal)
You can also use -estadd- (part of the -estout- package) to add scalars to estimates.
ReplyDeleteThanks for this post! The part about the "noisily" option in esttab helped me manipulate the delimiters in estout to do what I wanted.
ReplyDeleteI have a question:
ReplyDeletehere is the example I can come up with:
sysuse auto
poisson price weight mpg
estout, cells(b se) transform(weight
exp(_b[weight]+_b[mpg])-exp(_b[weight])
exp(_b[weight]+_b[mpg])-exp(_b[weight]) mpg
exp(_b[weight]+_b[mpg])-exp(_b[weight]) exp(_b[weight]+_b[mpg]))
Here, I want to calculate the term
"exp(_b[weight]+_b[mpg])-exp(_b[weight]) "; the STATA gives the
following results:
b/se
price
weight -.0123634
-1.000255
mpg -.0123634
.0004759
_cons 8.179377
.0194376
Note that the standard error is -1.000255.
Any thoughts?
Thanks a lot!
Sorry. This post was my first and last exploration of these commands. I can't really give you any advice.
Delete