Showing posts with label estout. Show all posts
Showing posts with label estout. Show all posts

Tuesday, November 13, 2012

Creating Professional Tables Using Estpost

Original Code

* This command should install the package estout.
ssc install estout

/*
estpost is one of several commands included in the pacakge estout.

In my previous post I delt with the primary use of estout, to create post estimation tables fit for publication.

I pretty much read through the high quality documentation listed on the estout site and made my own examples.

I will probably to the same with estpost.

I strongly recommend reading through the documentation found on the website http://repec.org/bocode/e/estout/estpost.html.

See (http://www.econometricsbysimulation.com/2012/11/professional-post-estimation-tables.html)

In this post I will deal with the estpost command which takes the results of several common summary statistics commands and converts them to formats that will be used by esttab.


If you end up using this command to create your tables please cite the author Jann, Ben.

It is obvious a lot of work went into creating this package with probably very little reward:

Citing estout

Thanks for citing estout in your work. For example, include a note such as
"Tables produced by estout (Jann 2005, 2007)."
and add
Jann, Ben (2005): Making regression tables from stored estimates. The Stata Journal 5(3): 288-308.
Jann, Ben (2007): Making regression tables simplified. The Stata Journal 7(2): 227-244.
to the bibliography.

estpost is compatible with the following commands:

From: http://repec.org/bocode/e/estout/hlp_estpost.html#commands

    command              description
    ----------------------------------------------------------------
    summarize            post summary statistics
    tabstat              post summary statistics
    ttest                post two-group mean-comparison tests
    prtest               post two-group tests of proportions
    tabulate             post one-way or two-way frequency table
    svy: tabulate        post frequency table for survey data
    correlate            post correlations
    ci                   post confidence intervals for means,
                             proportions, or counts
    stci                 post confidence intervals for means
                             and percentiles of survival time
    margins              post results from margins (Stata 11)
    ----------------------------------------------------------------

*/

* Let us start with some made up data!

clear
set obs 10

* Let's imagine that we are interested in 10 different products
gen prod_num = _n

* Each product has a base price
gen prod_price = rbeta(2,5)*10

* In six different markets
expand 6

sort prod_num

* I subtract the 1 because _n starts at 1 but mod (modular function) starts at 0
gen mercado = mod(_n-1, 6)

* Each mercado adds a fixed value to each product based on local demand
gen m_price = rbeta(2,5)*5 if prod_num == 1

bysort mercado: egen mercado_price = sum(m_price)

* Get rid of the reference price
drop m_price

* There is 104 weeks of observations for each product and mercado
expand 104

sort prod_num mercado

gen week = mod(_n-1,104)
* Each week there is a shared shock to all of the prices

gen week_p = rnormal()*.5 if mercado==0 & prod_num==1
bysort week_p: egen week_price=sum(week_p)
drop week_p

* Finally there is a product, market, and week specific shock that is indepentent of other shocks.
gen u = rnormal()*.5

* Let's generate some other random characteristics.
gen prod_stock = rnormal()

* Seasonality
gen seasonality = rnormal()

* Now let's calculate the price

gen price = prod_price + mercado_price + week_price + prod_stock + seasonality + u

* Finally in order to make things interesting let's say that our data set is incomplete because of random factors which occure 10% of the time.

gen missing = rbinomial(1,.1)
drop if missing==1
drop missing

* And to drop our unobservables
drop u week_price mercado_price prod_price

***********************************************************************************************************
*
*   Now that we have created our data, let's do some descriptive statistics that we will create tables from
*

* First the basic summarize command
estpost summarize price seasonality prod_stock

* This in effect tells us what statistics can be pulled from the summarize command.

* We can get more stats (such as medians) by using the detail option
estpost summarize price seasonality prod_stock, detail

* We can now create a table of estimates
esttab ., cells("mean sd count p1 p50 p99") noobs compress

* To save the table directly to a rtf (word compatible format)
esttab . using tables.rtf, replace cells("mean sd count p1 p50 p99") noobs compress

* Or excel
esttab . using tables.csv, replace cells("mean sd count p1 p50 p99") noobs compress

* Note the . after esttab is important.  I don't know why, but it does not work without it.

* Now imagine we would like to assemble a table that has the mean price seasonality and prod_stock by mercado
estpost tabstat price seasonality prod_stock, statistics(mean sd) columns(statistics) listwise by(mercado)

* Everything looks like it is working properly up to this point but for some reason I can't get the next part to work.
esttab, main(mean) aux(sd) nostar unstack noobs nonote nomtitle nonumber

* The table only has one column when it should have 6 for the six different markets.

estpost tab prod_num
esttab . using tables.rtf , append cells("b(label(freq)) pct(fmt(2)) cumpct(fmt(2))")

* There is also a correlate function that will post information about the correlation between the first variable listed after corr and the other variables.
estpost corr price week seasonality mercado
esttab . using tables.rtf , append  cell("rho p count")

* Unfortunately the alternative option, to generate the matrix of correlations that we would expect is not working either.

* This is the sad fate of these user written programs (such as Ian Watson's tabout), Stata becomes updated and they do not.

* I would find it very annoying to have to update code constantly so that a general public that I do not know can continue to use my code for free.

* However, perhaps if people are nice and send the author some emails requesting an update he might be encouraged to come back to his code knowing it is being used.

* His contact information listed on the package tutorial is Ben Jann, ETH Zurich, jann@soz.gess.ethz.ch.

Monday, November 12, 2012

Professional Post Estimation Tables Using Estout

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)