Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: esttab or estout table format


From   Ben Jann <[email protected]>
To   [email protected]
Subject   Re: st: esttab or estout table format
Date   Thu, 29 Jan 2009 11:42:03 +0100

Carl asks:
> Is there any way to place coefficients from different models in separate rows of a table with s.e. underneath, instead of placing coefficients from different models in separate columns?

Unfortunately, there is no option in -esttab- or -estout- to flip
models and coefficients, but here are two solutions for the problem.

Solution 1: -esttab- and -estout- return a matrix r(coefs) that
contains the tabulated results. You can run -esttab- or -estout- and
then run it again in matrix mode to transpose and tabulate r(coefs).
This approach is simple but the possibilities for formatting the table
are somewhat limited. Example:

---begin log---
. sysuse auto
(1978 Automobile Data)

. eststo model1: quietly reg price weight

. eststo model2: quietly reg price weight mpg

. esttab, se nostar

--------------------------------------
                      (1)          (2)
                    price        price
--------------------------------------
weight              2.044        1.747
                  (0.377)      (0.641)

mpg                             -49.51
                               (86.16)

_cons              -6.707       1946.1
                 (1174.4)     (3597.0)
--------------------------------------
N                      74           74
--------------------------------------
Standard errors in parentheses

. mat list r(coefs)

r(coefs)[3,4]
            model1:     model1:     model2:     model2:
                 b          se           b          se
weight   2.0440626   .37683413   1.7465592   .64135379
   mpg          .z          .z  -49.512221   86.156039
 _cons  -6.7073534   1174.4296   1946.0687   3597.0496

. esttab r(coefs, transpose)

---------------------------------------------------
                 r(coefs)
                   weight          mpg        _cons
---------------------------------------------------
model1
b                2.044063                 -6.707353
se               .3768341                   1174.43
---------------------------------------------------
model2
b                1.746559    -49.51222     1946.069
se               .6413538     86.15604      3597.05
---------------------------------------------------
---end log---

Solution 2: Again run -esttab- or -estout- to compile r(coefs) but
then, for each coefficient, collect the results and post them in e()
(i.e. post one "model" per coefficient). This approach requires some
programming but gives you full flexibility. Example:

---begin log---
. sysuse auto
(1978 Automobile Data)

. eststo model1: quietly reg price weight

. eststo model2: quietly reg price weight mpg

. esttab, se nostar

--------------------------------------
                      (1)          (2)
                    price        price
--------------------------------------
weight              2.044        1.747
                  (0.377)      (0.641)

mpg                             -49.51
                               (86.16)

_cons              -6.707       1946.1
                 (1174.4)     (3597.0)
--------------------------------------
N                      74           74
--------------------------------------
Standard errors in parentheses

. matrix C = r(coefs)

. eststo clear

. local rnames : rownames C

. local models : coleq C

. local models : list uniq models

. local i 0

. foreach name of local rnames {
  2.     local ++i
  3.     local j 0
  4.     capture matrix drop b
  5.     capture matrix drop se
  6.     foreach model of local models {
  7.         local ++j
  8.         matrix tmp = C[`i', 2*`j'-1]
  9.         if tmp[1,1]<. {
 10.             matrix colnames tmp = `model'
 11.             matrix b = nullmat(b), tmp
 12.             matrix tmp[1,1] = C[`i', 2*`j']
 13.             matrix se = nullmat(se), tmp
 14.         }
 15.     }
 16.     ereturn post b
 17.     quietly estadd matrix se
 18.     eststo `name'
 19. }

. esttab, se mtitle noobs

------------------------------------------------------------
                      (1)             (2)             (3)
                   weight             mpg           _cons
------------------------------------------------------------
model1              2.044***                       -6.707
                  (0.377)                        (1174.4)

model2              1.747**        -49.51          1946.1
                  (0.641)         (86.16)        (3597.0)
------------------------------------------------------------
Standard errors in parentheses
* p<0.05, ** p<0.01, *** p<0.001
---end log---

ben

Here are the example do-files:

---example1.do---
sysuse auto
eststo model1: quietly reg price weight
eststo model2: quietly reg price weight mpg
esttab, se nostar
mat list r(coefs)
esttab r(coefs, transpose)
---end---

---example2.do---
sysuse auto
eststo model1: quietly reg price weight
eststo model2: quietly reg price weight mpg
esttab, se nostar
matrix C = r(coefs)
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}
esttab, se mtitle noobs
---end---
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index