Examples for estadd

Basic syntax and usage

Results that are included in the e()-returns for the models can be tabulated by estout or esttab. Thus, the procedure for reporting certain additional statistics is to add them to the the e()-returns and then tabulate them using estout or esttab. The estadd command is designed to support this procedure. It may be used to add user-provided scalars and matrices to e() and has also various bulti-in functions to add, say, beta coefficients or descriptive statistics of the regressors and the dependent variable (see the help file for a list of available functions). The basic syntax of estadd is:

estadd subcommand [, options ] [ : namelist ]

estadd can either be applied to the currently active estimates or to stored estimation sets by specifying their names after the colon (namelist). To add, say, the beta coefficients to your models, you may type:

. sysuse auto
(1978 Automobile Data)

. quietly regress price weight mpg

. estadd beta

added matrix:
               e(beta) :  1 x 3

. estimates store model1

. quietly regress price weight mpg foreign

. estadd beta

added matrix:
               e(beta) :  1 x 4

. estimates store model2

. estout model1 model2, cells(beta) drop(_cons)

--------------------------------------
                   model1       model2
                     beta         beta
--------------------------------------
weight           .4602192     .9129516
mpg             -.0971193     .0428663
foreign                        .573081
--------------------------------------

. estimates clear

[do-file]

Alternatively, you may first store all models and then add the beta coefficients in one run:

. sysuse auto
(1978 Automobile Data)

. quietly regress price weight mpg

. estimates store model1

. quietly regress price weight mpg foreign

. estimates store model2

. estadd beta : model1 model2

. estout model1 model2, cells(beta) drop(_cons)

--------------------------------------
                   model1       model2
                     beta         beta
--------------------------------------
weight           .4602192     .9129516
mpg             -.0971193     .0428663
foreign                        .573081
--------------------------------------

. estimates clear

[do-file]

Hint: To add the beta coefficients to all stored models, you may also type

. estadd beta : *

instead of spelling out the names of the sets.

The procedure can be further simplified using eststo to store the models:

. sysuse auto
(1978 Automobile Data)

. eststo: quietly regress price weight mpg
(est1 stored)

. eststo: quietly regress price weight mpg foreign
(est2 stored)

. estadd beta : *

. estout, cells(beta) drop(_cons)

--------------------------------------
                     est1         est2
                     beta         beta
--------------------------------------
weight           .4602192     .9129516
mpg             -.0971193     .0428663
foreign                        .573081
--------------------------------------

. eststo clear

[do-file]

Adding scalars

A common situation is that one wants to compute some additional tests statistics or summary measures and then included them in the regression table. Use estadd scalar to add such scalar results to the estimation sets. Example:

. sysuse auto
(1978 Automobile Data)

. quietly regress price weight mpg

. test weight=mpg

 ( 1)  weight - mpg = 0

       F(  1,    71) =    0.36
            Prob > F =    0.5514

. estadd scalar F_diff = r(F)

added scalar:
             e(F_diff) =  .3582543

. estadd scalar p_diff = r(p)

added scalar:
             e(p_diff) =  .55138216

. estout, stats(F_diff p_diff)

-------------------------
                        .
                        b
-------------------------
weight           1.746559
mpg             -49.51222
_cons            1946.069
-------------------------
F_diff           .3582543
p_diff           .5513822
-------------------------

[do-file]

Note: Similar to many other Stata commands, test returns its results in r(), from where they can be picked up by estadd. Type return list after test to display a list of the returned results.

Adding results by subgroups

Say, you are estimating models by subgroups and want to tabulate the regressors' descriptives for each group. You may proceed as follows:

. sysuse auto
(1978 Automobile Data)

. by foreign: eststo: quietly regress price weight mpg

-------------------------------------------------------------------------------
-> Domestic
(est1 stored)

-------------------------------------------------------------------------------
-> Foreign
(est2 stored)

. estadd summ : *

. estout, cells(mean sd(par)) drop(_cons)

--------------------------------------
                     est1         est2
                  mean/sd      mean/sd
--------------------------------------
weight           3317.115     2315.909
               (695.3637)   (433.0035)
mpg              19.82692     24.77273
               (4.743297)   (6.611187)
--------------------------------------

. eststo clear

[do-file]

Writing one's own subroutines

As mentioned above, estadd comes with various built-in functions to compute additional statistics (see the help file for a list of the available functions). If you want to add other results, you can either hand-code the computation of the results after each model and add them using estadd scalar and estadd matrix, or you can automate the process by writing your own subcommand. Say, you want to report the multiple correlation (i.e. the square root of the R-squared), then you could either code something like

. sysuse auto
(1978 Automobile Data)

. eststo: quietly regress price weight mpg
(est1 stored)

. estadd scalar R = sqrt(e(r2))

added scalar:
                  e(R) =  .54165406

. eststo: quietly regress price weight mpg foreign
(est2 stored)

. estadd scalar R = sqrt(e(r2))

added scalar:
                  e(R) =  .70679515

. estout, stats(r2 R)

--------------------------------------
                     est1         est2
                        b            b
--------------------------------------
weight           1.746559     3.464706
mpg             -49.51222      21.8536
foreign                        3673.06
_cons            1946.069    -5853.696
--------------------------------------
r2               .2933891     .4995594
R                .5416541     .7067952
--------------------------------------

. eststo clear

[do-file]

or you could write your own subroutine as follows (note that the name of the subroutine must start with estadd_):

. capture program drop estadd_R

. program estadd_R, eclass
  1.     ereturn scalar R = sqrt(e(r2))
  2. end

. sysuse auto
(1978 Automobile Data)

. eststo: quietly regress price weight mpg
(est1 stored)

. eststo: quietly regress price weight mpg foreign
(est2 stored)

. estadd R : *

. estout, stats(r2 R)

--------------------------------------
                     est1         est2
                        b            b
--------------------------------------
weight           1.746559     3.464706
mpg             -49.51222      21.8536
foreign                        3673.06
_cons            1946.069    -5853.696
--------------------------------------
r2               .2933891     .4995594
R                .5416541     .7067952
--------------------------------------

. eststo clear

[do-file]

Within the subroutine, use the ereturn command to add the results. Note that the program must be declared eclass (see help program).

As a second example, assume you want to report y-standardized coefficients. You could hand-code the computations for each model:

. sysuse auto
(1978 Automobile Data)

. eststo: quietly regress price weight mpg
(est1 stored)

. matrix bstdy = e(b)

. quietly summarize price if e(sample)

. matrix bstdy = bstdy / r(sd)

. estadd matrix bstdy = bstdy

added matrix:
              e(bstdy) :  1 x 3

. eststo: quietly regress price weight mpg foreign
(est2 stored)

. matrix bstdy = e(b)

. quietly summarize price if e(sample)

. matrix bstdy = bstdy / r(sd)

. estadd matrix bstdy = bstdy

added matrix:
              e(bstdy) :  1 x 4

. estout, cells(b bstdy(par)) 

--------------------------------------
                     est1         est2
                  b/bstdy      b/bstdy
--------------------------------------
weight           1.746559     3.464706
               (.0005922)   (.0011747)
mpg             -49.51222      21.8536
              (-.0167867)   (.0074093)
foreign                        3673.06
                            (1.245318)
_cons            1946.069    -5853.696
                (.659797)  (-1.984643)
--------------------------------------

. eststo clear

[do-file]

Or you could use an appropriate subroutine:

. capture program drop estadd_bstdy

. program estadd_bstdy, eclass
  1.     tempname bstdy
  2.     matrix `bstdy' = e(b)
  3.     quietly summarize `e(depvar)' if e(sample)
  4.     matrix `bstdy' = `bstdy' / r(sd)
  5.     ereturn matrix bstdy = `bstdy'
  6. end

. sysuse auto
(1978 Automobile Data)

. eststo: quietly regress price weight mpg
(est1 stored)

. eststo: quietly regress price weight mpg foreign
(est2 stored)

. estadd bstdy : *

. estout, cells(b bstdy(par))

--------------------------------------
                     est1         est2
                  b/bstdy      b/bstdy
--------------------------------------
weight           1.746559     3.464706
               (.0005922)   (.0011747)
mpg             -49.51222      21.8536
              (-.0167867)   (.0074093)
foreign                        3673.06
                            (1.245318)
_cons            1946.069    -5853.696
                (.659797)  (-1.984643)
--------------------------------------

. eststo clear

[do-file]

The simple subcommand examples given here do not support estadd's replace and prefix() options. If you are interested in writing more advanced subcommands, have a look at the code of the existing subcommands in "estadd.ado". To view the code in Stata 9, type

. viewsource estadd.ado

In Stata 8, type

. findfile estadd.ado

. view "`r(fn)'", asis