Friday, March 22, 2013

Stata and $\LaTeX$: Descriptive Statistics, Part 2

In a previous post about outputting descriptive statistics from Stata to $\LaTeX$ I didn't expect to follow that post with a sequel but as I gained more experience outputting statistics, I realized I'd need something more flexible and more powerful.  Enter -estpost-.  I didn't realize just how flexible, powerful, and relatively easy -estpost- is to use until I started playing around with it.   The author, Ben Jann, ought to receive a place in the Stata user-programmer hall of fame for this contribution alone.  Anyway, outputting descriptive statistics can be accomplished with either -summarize- or -tabstat- following -estpost-.  I prefer -tabstat-.  Once the statistics are generated, the output is then dumped into a $\LaTeX$ file with the accompanying code via -esttab-.  It's pretty simple really.  Below is my Stata code using the ever-pervasive system dataset, auto.dta.  

// #1
// read in auto data
sysuse auto


// #2
// univariate summary statistics
estpost tabstat price mpg trunk weight length turn displacement gear_ratio, ///
statistics(N min max p50 mean sd) columns(statistics)
*...to latex...
esttab . using stats.tex, replace cells("count min max p50 mean(fmt(a3)) sd(fmt(a3))") ///
title("Univariate Summary Statistics for auto.dta") label nomtitles noobs width(\hsize)


// #3
// stratified by foreign
estpost tabstat price mpg trunk weight length turn displacement gear_ratio, ///
by(foreign) statistics(N min max p50 mean sd) columns(statistics) nototal
*...to latex...
esttab . using stats.tex, append cells("count min max p50 mean(fmt(a3)) sd(fmt(a3))") ///
title("Summary Statistics for auto.dta stratified by foreign") label nomtitles noobs width(\hsize)


The code from -esttab- outputs a $\LaTeX$ file (I called it stats.tex above) which I then open up in my editor (WinEdt), declare the document class, add the necessary packages into my preamble, then run.  The output is pasted below.  The more I use -estpost-, the more I love it. 

No comments:

Post a Comment