Tuesday, October 02, 2018

Stata: output and reuse the estimates of histogram

Stata: Output and reuse the estimates of a histogram
Sometimes we would like to export the estimates of a histogram and make a histogram plot using other software such as Excel.  The undocumented command - twoway__ histogram_gen - can help.
  • Stata tip 20: Generating histogram bin variables, for example (modified):
    • sysuse auto,clear
    • histogram weight, percent bin(20)
    • twoway__histogram_gen weight, bin(20) percent gen (hvalue xvalue, replace)
    • twoway bar hvalue xvalue, barwidth(.15) bstyle(histogram)
    • list hvalue xvalue if !missing(hvalue), sum(hvalue) clean
    • or, you can copy columns from the "Data Editor" window and paste them into Excel sheet.
  • or use - serset -, the data in the .gph file are stored in the serset format
    • sysuse auto,clear
    • histogram weight, percent width(100)
    • preserve
    • serset use,clear
    • then, you can copy the first and third columns (such as __000001, __000003) from the "Data Editor" window and paste them into an Excel sheet.
    • restore
  • - grexport - a user-written ado file, an alternative to -graph export-
    • sysuse auto,clear
    • histogram weight, percent width(100)
    • grexport, list //list results in the result window

No comments: