Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Customized table using esttab, reversing rows and columns with several columns

    Dear Stata-Listers,

    I estimate several models, which always contain the same coefficient of interest. Then I do the same with another outcome, and the same coefficient of interest (the structure of the models remains the same for the second outcome). Then I'd like to have a table, where each row shows the coefficient of interest and each column represents one of the different outcomes. Thus, I want something like this:
    price outcome rep78 outcome
    Model 1 outcome 1 **
    (s.e.)
    outcome 3*
    (s.e.)
    Model 2 outcome 2 **
    (s.e.)
    outcome 4
    (s.e.)

    Following, I copied an example from http://repec.org/bocode/e/estout/adv...ml#advanced905 which provides me with the solution with one single column, the one with weight as outcome.

    Code:
    sysuse auto
    eststo model1: quietly reg price weight
    eststo model2: quietly reg price weight mpg
    
    esttab model1 model2, se nostar
    
    matrix c = r(coefs)
    mat list c
    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
          estadd matrix se
          eststo `name'
      }
    
     esttab , se mtitle noob
    (the output table can be found on the website posted above)

    I can only keep e.g. weight as coefficient of interest by adding esttab "keep(weight)" to the first esttab command. Then I have two rows, one for each model estimated, and one column.
    Due to the fact that I am not very familiar with matrix programming in Stata, I am really struggling how to extend this example to a second column, which e.g. estimates the same models with rep78 as outcome:

    Code:
    sysuse auto
    eststo model1: quietly reg rep78 weight
    eststo model2: quietly reg rep78 weight mpg
    Thus, the second row would again display the result of the weight coefficient, but now on rep78.

  • #2
    A clever use of rename would save you any complicated coding. estout is from the Stata Journal/ SSC (FAQ Advice #12).

    Comment


    • #3
      Thank you for the reply!
      Wouldn't rename just help out if I wanted to put different coefficients into one row? I don't really see how I can use it such that it reaches my desired result.
      To be clear: I have four different outcomes, which I want to allocate in four different columns. For each outcome, I run 24 regressions, of which I want to retrieve one single coefficient into the rows. So the table should have the format of 24x4 (rows x columns).
      The coefficient of estimation 1 for each outcome is the same. So row 1 would always contain coef1 etc.
      Due to the fact that the estimations take a relatively long time, I do not want to re-run the estimations each time I want to change the table in some way. This is why I have saved them using estimates save. So the best way would be to use those stored estimates somehow.

      Comment


      • #4
        Use appendmodels (unpublished; Ben Jann) for coefficients belonging to the same outcome and esttab the models from appendmodels. You can find the appendmodels code at https://www.statalist.org/forums/for...ally-in-3-rows

        Comment


        • #5
          Okay, I think this is getting pretty close, thanks!
          Just one more thing: all regressions in your suggested posts are bivariate regressions. I am running multivariate regressions (with many controls) and want to extract the coefficient of interest. Is there an easy workaround to do so?
          Note that I already have all estimates stored in .est files. Thus, I'd like to use these estimates if possible!
          Last edited by Philipp Schrauth; 27 Oct 2021, 05:55.

          Comment


          • #6
            If you install erepost from SSC, you can write a loop that replaces the coefficient of a bivarate regression. Here is an example (Stata 16+)

            Code:
            sysuse auto, replace
            regress mpg weight disp turn foreign
            *SAY, YOU ARE INTERESTED IN THE VARIABLE "weight"
            mat b= e(b)[1, "weight"]
            mat V= e(V)["weight", "weight"]
            *QUI RUN BIVARATE REGRESSION
            qui regress mpg weight, nocons
            *REPLACE ESTIMATES
            erepost b=b
            erepost V=V
            *STORE ESTIMATES
            est sto m1
            *ESTTAB
            esttab m1
            It seems a hassle, but you are repeating the same set of commands, so you can loop.

            Res.:

            Code:
            . regress mpg weight disp turn foreign
            
                  Source |       SS           df       MS      Number of obs   =        74
            -------------+----------------------------------   F(4, 69)        =     35.11
                   Model |  1638.44068         4  409.610169   Prob > F        =    0.0000
                Residual |  805.018782        69  11.6669389   R-squared       =    0.6705
            -------------+----------------------------------   Adj R-squared   =    0.6514
                   Total |  2443.45946        73  33.4720474   Root MSE        =    3.4157
            
            ------------------------------------------------------------------------------
                     mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                  weight |  -.0057497   .0014155    -4.06   0.000    -.0085735   -.0029258
            displacement |   .0015526   .0100313     0.15   0.877    -.0184594    .0215645
                    turn |  -.2343184   .1849765    -1.27   0.210    -.6033364    .1346996
                 foreign |  -2.034334   1.160522    -1.75   0.084    -4.349512    .2808447
                   _cons |   48.24709   5.567542     8.67   0.000     37.14015    59.35404
            ------------------------------------------------------------------------------
            
            . esttab m1
            
            ----------------------------
                                  (1)   
                                  mpg   
            ----------------------------
            weight           -0.00575***
                              (-4.06)   
            ----------------------------
            N                      74   
            ----------------------------
            t statistics in parentheses
            * p<0.05, ** p<0.01, *** p<0.001
            
            

            Comment


            • #7
              Thank you so much for your fast response and help! I will try out the solution, it looks very promising!

              Comment


              • #8
                Your solution works perfectly fine! Just one (hopefully) last question: Now I have 24 rows and each row is named after my variable of interest, in my case "treatment" and in your case this would correspond to 24 rows of "weight".
                Is there a way to individually rename each case? So for example, I want the first line to be labelled "case 1", the second line "case 2" etc.?

                I thought about "rename", but this does not work in my case since I used the stored estimates.

                To be clear, the code I use so far is:

                Code:
                forvalues i = 0/23 {
                estimates use "${estresults}model`i'.est", number(1)
                estimates store est`i'
                
                mat b = e(b)[1, "treatment"]
                mat V = e(V)["treatment", "treatment"]
                mat list b
                mat list V
                regress outcome treatment, nocons
                erepost b=b
                erepost V=V
                eststo m`i'
                }
                
                eststo allmodels: appendmodels m0 m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19 m20 m21 m22 m23
                
                esttab allmodels

                Comment


                • #9
                  Try

                  Code:
                  forvalues i = 0/23 {
                  local j = `i'+1
                  estimates use "${estresults}model`i'.est", number(1)
                  estimates store est`i'
                  
                  mat b = e(b)[1, "treatment"]
                  mat V = e(V)["treatment", "treatment"]
                  mat colname b = "case_`j'"
                  mat colname V= "case_`j'"
                  regress outcome treatment, nocons
                  erepost b=b, rename
                  erepost V=V, rename
                  eststo m`i'
                  }
                  
                  eststo allmodels: appendmodels m0 m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19 m20 m21 m22 m23
                  
                  esttab allmodels, substitute(_ " ")
                  Last edited by Andrew Musau; 27 Oct 2021, 08:51.

                  Comment


                  • #10
                    Beautiful! Thank you so much, you made my day!!

                    Comment

                    Working...
                    X