Announcement

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

  • how to use Stata 17 to output a table of panel fixed effects regression?

    Dear Statalist,

    I am using Stata 17 to run panel fixed effects regression.I want to use the command --collect-- to create a table.but how can I add the fixed effects into the table, such as "Country fixed effects YES" or "Year fixed effects YES".In Stata 16,we can easily use --outreg2-- to add them.But In the Collections Systerm, How can I add them?I will give an example below.
    Code:
    webuse grunfeld,clear
    collect clear
    collect _r_b _r_se, tag(model[(1)]):xtreg invest mvalue kstock i.year,fe r
    collect layout (colname#result) (model)
    collect style showbase off
    collect style cell, nformat(%5.2f) border(right, pattern(nil))
    collect style cell result[_r_se], sformat("(%s)")
    collect preview
    collect style cell cell_type[item column-header], halign(center)
    collect style header result, level(hide)
    collect style column, extraspace(1)
    collect style row stack
    collect preview
    collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b) 
    collect layout (colname#result[_r_b _r_se] result[r2 N]) (model) 
    collect style header result[r2 N], level(label)
    collect label levels result r2 "R-squared", modify
    collect style cell result[N], nformat(%5.0f)
    collect preview
    -------------------------------------
                                   (1)   
    -------------------------------------
    Market value (prior year)    0.12*** 
                                  (0.01) 
    Capital stock (prior year)   0.36*** 
                                  (0.05) 
    Year=1936                   -19.20   
                                 (20.70) 
    Year=1937                   -40.69   
                                 (33.28) 
    Year=1938                   -39.23** 
                                 (15.74) 
    Year=1939                   -69.47** 
                                 (27.00) 
    Year=1940                   -44.24** 
                                 (17.37) 
    Year=1941                   -18.80   
                                 (17.85) 
    Year=1942                   -21.14   
                                 (14.16) 
    Year=1943                   -42.98***
                                 (12.54) 
    Year=1944                   -43.10***
                                 (11.00) 
    Year=1945                   -55.68***
                                 (15.20) 
    Year=1946                   -31.17   
                                 (20.92) 
    Year=1947                   -39.39   
                                 (26.44) 
    Year=1948                   -43.72   
                                 (38.88) 
    Year=1949                   -73.50*  
                                 (38.25) 
    Year=1950                   -75.90*  
                                 (36.80) 
    Year=1951                   -62.48   
                                 (49.42) 
    Year=1952                   -64.63   
                                 (51.56) 
    Year=1953                   -67.72   
                                 (43.74) 
    Year=1954                   -93.53** 
                                 (31.73) 
    Intercept                   -32.84   
                                 (19.78) 
    R-squared                      0.80  
    Number of observations         200   
    -------------------------------------
    
    collect export myreg.docx,replace


    Best regards.

    Raymond Zhang
    Stata 17.0,MP

  • #2
    How to display fixed effects ?

    Code:
    webuse grunfeld,clear
    collect clear
    collect _r_b _r_se, tag(model[(1)]):xtreg invest mvalue kstock i.year,fe r
    collect _r_b _r_se, tag(model[(2)]):xtreg invest c.mvalue##c.kstock i.year,fe r
    collect layout (colname#result) (model)
    collect style showbase off
    collect style cell, nformat(%5.2f) border(right, pattern(nil))
    collect style cell result[_r_se], sformat("(%s)")
    collect preview
    collect style cell cell_type[item column-header], halign(center)
    collect style header result, level(hide)
    collect style column, extraspace(1)
    collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b) 
    collect layout (colname#result[_r_b _r_se] result[r2 N]) (cmdset) 
    collect style header result[r2 N], level(label)
    collect style header colname, level(value)
    collect style row stack, spacer delimiter(" x ")
    collect label levels result N "N" r2 "R2", modify
    collect style cell result[N], nformat(%5.0f)
    collect preview
    --------------------------------------
                        (1)         (2)   
    --------------------------------------
    mvalue            0.12***     0.05**  
                       (0.01)      (0.02) 
                                          
    kstock            0.36***     -0.10   
                       (0.05)      (0.09) 
                                          
    Year=1936        -19.20       8.67    
                      (20.70)     (13.04) 
    Year=1937        -40.69       10.28   
                      (33.28)     (21.93) 
    Year=1938        -39.23**     -5.39   
                      (15.74)      (9.69) 
    Year=1939        -69.47**    -17.28   
                      (27.00)     (11.53) 
    Year=1940        -44.24**     12.77   
                      (17.37)     (14.41) 
    Year=1941        -18.80       40.78   
                      (17.85)     (23.91) 
    Year=1942        -21.14       37.33   
                      (14.16)     (24.42) 
    Year=1943        -42.98***    26.04   
                      (12.54)     (21.26) 
    Year=1944        -43.10***    29.63   
                      (11.00)     (22.01) 
    Year=1945        -55.68***    24.21   
                      (15.20)     (20.04) 
    Year=1946        -31.17       53.22   
                      (20.92)     (31.08) 
    Year=1947        -39.39       51.77   
                      (26.44)     (31.41) 
    Year=1948        -43.72       59.54   
                      (38.88)     (38.97) 
    Year=1949        -73.50*      39.20   
                      (38.25)     (32.82) 
    Year=1950        -75.90*      44.72   
                      (36.80)     (35.26) 
    Year=1951        -62.48       64.30   
                      (49.42)     (43.71) 
    Year=1952        -64.63       75.70   
                      (51.56)     (50.52) 
    Year=1953        -67.72       79.40   
                      (43.74)     (51.40) 
    Year=1954        -93.53**     68.77   
                      (31.73)     (48.68) 
                                          
    mvalue x kstock               0.00*** 
                                   (0.00) 
                                          
    _cons            -32.84       41.99*  
                      (19.78)     (21.39) 
                                          
    R2                  0.80        0.87  
                                          
    N                   200         200   
    --------------------------------------
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

    Comment


    • #3
      Hua Peng (StataCorp)
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        Raymond, you did not get a speedy reply. Given that Stata 17 is newly released and you are asking about a new feature, you may not get a quick reply because many users probably aren't too familiar with the collect system yet.

        Comment


        • #5
          Originally posted by Leonardo Guizzetti View Post
          Raymond, you did not get a speedy reply. Given that Stata 17 is newly released and you are asking about a new feature, you may not get a quick reply because many users probably aren't too familiar with the collect system yet.
          Dear Leonardo,

          Thank you for your reply.Yes, the collect system is new to us.I think it is a good way to create table.Maybe someone from StataCorp can solve my question.

          Best
          Raymond
          Best regards.

          Raymond Zhang
          Stata 17.0,MP

          Comment


          • #6
            Can you show us how you would use outreg2 to get what you want?

            Comment


            • #7
              collect get and collect: prefix commands allow you to add custom results. Here is an example based on Raymond's second example:

              Code:
              webuse grunfeld
              
              collect clear
              * add custom results has_year_fe and has_panel
              collect _r_b _r_se has_year_fe="YES" has_panel="YES", tag(model[(1)]): ///
                  xtreg invest mvalue kstock i.year,fe r
              collect _r_b _r_se has_year_fe="YES" has_panel="YES", tag(model[(2)]): ///
                  xtreg invest c.mvalue##c.kstock i.year,fe r
              collect _r_b _r_se has_year_fe="NO" has_panel="YES", tag(model[(3)]): ///
                  xtreg invest c.mvalue##c.kstock,fe r
              collect _r_b _r_se has_year_fe="NO" has_panel="NO", tag(model[(4)]): ///
                  reg invest c.mvalue##c.kstock, r
              collect layout (colname#result) (model)
              collect style showbase off
              collect style cell, nformat(%5.2f) border(right, pattern(nil))
              collect style cell result[_r_se], sformat("(%s)")
              collect preview
              collect style cell cell_type[item column-header], halign(center)
              collect style header result, level(hide)
              collect style column, extraspace(1)
              collect stars _r_p 0.01 "***" 0.05 "** " 0.1 "*  " 1 "   ", attach(_r_b) 
              * modify layout to accommodate our custom results and suppress the i.year results
              collect layout (colname[mvalue kstock mvalue#kstock]#result[_r_b _r_se] result[has_year_fe has_panel r2 N]) (model)
              * show label of our custom results in the header
              collect style header result[has_year_fe has_panel r2 N], level(label)
              collect style header colname, level(value)
              collect style row stack, spacer delimiter(" x ")
              * label our custom result levels
              collect label levels result ///
                  has_year_fe "Year fixed effects" ///
                  has_panel "Panel fixed effects" ///
                  N "N" ///
                  r2 "R2" ///
                  , modify
              collect style cell result[N], nformat(%5.0f)
              collect preview
              Here is the resulting table
              Code:
              . collect preview
              
              -----------------------------------------------------------
                                     (1)        (2)       (3)       (4)  
              -----------------------------------------------------------
              mvalue               0.12***    0.05**    0.07***   0.09***
                                    (0.01)    (0.02)     (0.02)    (0.01)
                                                                         
              kstock               0.36***   -0.10      0.07      0.05   
                                    (0.05)    (0.09)     (0.05)    (0.03)
                                                                         
              mvalue x kstock                 0.00***   0.00***   0.00***
                                              (0.00)     (0.00)    (0.00)
                                                                         
              Year fixed effects     YES        YES        NO        NO  
                                                                         
              Panel fixed effects    YES        YES       YES        NO  
                                                                         
              R2                     0.80      0.87       0.84      0.85 
                                                                         
              N                      200        200       200       200  
              -----------------------------------------------------------

              Comment


              • #8
                Dear @Jeff Pitblado (StataCorp),
                Perfect! This is just what I want.Thank you very much for your kind help.

                Best regards.
                Raymond
                Best regards.

                Raymond Zhang
                Stata 17.0,MP

                Comment


                • #9
                  Dear @Jeff Pitblado (StataCorp),
                  How can I use --collect-- to output the table of 2SLS with the command --ivreg2--? I can only output the second-stage regression coefficients, but can not output the first-stage results.Here is an example.

                  Code:
                  use http://fmwww.bc.edu/ec-p/data/hayashi/griliches76.dta,clear
                   ivreg2 lw s expr tenure rns smsa i.year (iq=med kww age mrt)
                   collect clear
                   collect _r_b _r_first _r_se:ivreg2 lw s expr tenure rns smsa i.year (iq=med kww age mrt),ffirst
                   collect layout (colname[iq s expr tenure rns smsa _cons]#result) (cmdset)
                   collect style use myxtreg,replace
                   collect preview
                  
                  --------------
                             1  
                  --------------
                  iq       0.00 
                          (0.00)
                                
                  s        0.07 
                          (0.01)
                                
                  expr     0.03 
                          (0.01)
                                
                  tenure   0.04 
                          (0.01)
                                
                  rns      -0.10
                          (0.03)
                                
                  smsa     0.14 
                          (0.03)
                                
                  _cons    4.40 
                          (0.27)
                  --------------
                  Best regards.

                  Raymond Zhang
                  Stata 17.0,MP

                  Comment


                  • #10
                    There is no result named _r_first. I think Raymond wants to include some of the elements of e(first) in the table. e(first) is a column vector containing first stage statistics. The row stripe elements identify the statistics. So just like we use colname to identify and label which coefficients to report in a table, here we need to use rowname to perform a similar task.

                    Here is a modified version of Raymonds' example from #9.
                    Code:
                    use http://fmwww.bc.edu/ec-p/data/hayashi/griliches76.dta
                    
                    collect: ivreg2 lw s expr tenure rns smsa i.year (iq=med kww age mrt), ffirst
                    
                    * approximate styles in Raymond's myxtreg.stjson file
                    
                    collect style cell border_block, border(right, pattern(nil))
                    collect style cell, nformat(%5.2f)
                    collect style cell result[_r_se], sformat("(%s)")
                    collect style header result, level(hide)
                    collect style header colname, level(value)
                    
                    * stack coefficients and their SEs (result[_r_b _r_se]) above the list
                    * of first stage statistics (result[first]) --
                    * use colname to label the coefficients/SEs
                    * use rowname to label the first stage statistics
                    
                    collect layout ///
                        (colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se] ///
                         rowname#result[first]) ///
                        (cmdset)
                    
                    * select the first stage stats by specifying levels of rowname
                    
                    collect layout ///
                        (colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se] ///
                         rowname[rmse F df df_r pvalue]#result[first]) ///
                        (cmdset)
                    Here are the resulting tables.
                    Code:
                    . * stack coefficients and their SEs (result[_r_b _r_se]) above the list
                    . * of first stage statistics (result[first]) --
                    . * use colname to label the coefficients/SEs
                    . * use rowname to label the first stage statistics
                    . 
                    . collect layout ///
                    >         (colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se] ///
                    >          rowname#result[first]) ///
                    >         (cmdset)
                    
                    Collection: default
                          Rows: colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se]
                                rowname#result[first]
                       Columns: cmdset
                       Table 1: 35 x 1
                    
                    --------------
                                 1
                    --------------
                    iq        0.00
                            (0.00)
                    s         0.07
                            (0.01)
                    expr      0.03
                            (0.01)
                    tenure    0.04
                            (0.01)
                    rns      -0.10
                            (0.03)
                    smsa      0.14
                            (0.03)
                    _cons     4.40
                            (0.27)
                    rmse     11.21
                    sheapr2   0.07
                    pr2       0.07
                    F        13.79
                    df        4.00
                    df_r    742.00
                    pvalue    0.00
                    SWF      13.79
                    SWFdf1    4.00
                    SWFdf2  742.00
                    SWFp      0.00
                    SWchi2   56.33
                    SWchi2p   0.00
                    SWr2      0.07
                    APF      13.79
                    APFdf1    4.00
                    APFdf2  742.00
                    APFp      0.00
                    APchi2   56.33
                    APchi2p   0.00
                    APr2      0.07
                    --------------
                    
                    . 
                    . * select the first stage stats by specifying levels of rowname
                    . 
                    . collect layout ///
                    >         (colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se] ///
                    >          rowname[rmse F df df_r pvalue]#result[first]) ///
                    >         (cmdset)
                    
                    Collection: default
                          Rows: colname[iq s expr tenure rns smsa _cons]#result[_r_b _r_se]
                                rowname[rmse F df df_r pvalue]#result[first]
                       Columns: cmdset
                       Table 1: 19 x 1
                    
                    -------------
                                1
                    -------------
                    iq       0.00
                           (0.00)
                    s        0.07
                           (0.01)
                    expr     0.03
                           (0.01)
                    tenure   0.04
                           (0.01)
                    rns     -0.10
                           (0.03)
                    smsa     0.14
                           (0.03)
                    _cons    4.40
                           (0.27)
                    rmse    11.21
                    F       13.79
                    df       4.00
                    df_r   742.00
                    pvalue   0.00
                    -------------

                    Comment


                    • #11
                      @Jeff Pitblado (StataCorp) Dear Jeff, what you export is the second-stage regression coefficients ,but 2SLS has first-stage and second-stage regression coefficients.Here I will give the results of outreg2.How can I use collect system to get the same table.

                      Code:
                      sysuse auto,clear
                      ivreg2 mpg weight (length=displacement),first savefp(first)
                      eststo second
                      outreg2 [firstlength second] using xxx1.doc, tstat bdec(3) tdec(2) replace
                      Code:
                        
                      (1) (2)
                      firstlength second
                      VARIABLES length mpg
                      displacement -0.014
                      (-0.67)
                      weight 0.029*** 0.004
                      (11.61) (0.19)
                      length -0.379
                      (-0.47)
                      Constant 104.398*** 79.673
                      (24.48) (0.92)
                      Observations 74 74
                      R-squared 0.522
                      Best regards.

                      Raymond Zhang
                      Stata 17.0,MP

                      Comment


                      • #12
                        just like the picture below
                        Attached Files
                        Best regards.

                        Raymond Zhang
                        Stata 17.0,MP

                        Comment


                        • #13
                          Thank you Raymond. Your example tells me exactly what you want.

                          Here is how you can use collect to reproduce this table.
                          Code:
                          sysuse auto,clear
                          collect _r_b _r_z, tag(stage[second] outcome[mpg]) ///
                              : ivreg2 mpg weight (length=displacement), first savefp(first)
                          estimates restore firstlength
                          collect get _r_b _r_z, tag(stage[first] outcome[length])
                          
                          collect style cell border_block, border(right, pattern(nil))
                          collect style cell result[r2], nformat(%5.3f)
                          collect style cell result[_r_z], sformat("(%s)")
                          collect style cell result[_r_b], nformat(%9.3f)
                          collect style header result, level(hide)
                          collect style header result[N r2], level(label)
                          collect label levels colname _cons "Constant", replace
                          collect label levels result N "Observations" r2 "R-squared", modify
                          collect style cell cell_type[column-header item], halign(center)
                          
                          collect style autolevels colname displacement weight length _cons, clear
                          collect style autolevels stage first second, clear
                          
                          collect stars _r_p .01 "***" .05 "**" .1 "*", attach(_r_b)
                          
                          collect layout (colname#result result[N r2]) (stage#outcome)
                          Here is the resulting table.
                          Code:
                          -------------------------------
                                          first    second
                                         length     mpg  
                          -------------------------------
                          displacement   -0.014          
                                         (-0.67)         
                          weight        0.029***   0.004 
                                         (11.61)   (0.19)
                          length                   -0.379
                                                  (-0.47)
                          Constant     104.398***  79.673
                                         (24.48)   (0.92)
                          Observations     74        74  
                          R-squared                0.522 
                          -------------------------------

                          Comment


                          • #14
                            Jeff Pitblado (StataCorp) Thank you very much.It works well.
                            Now I have another problem.I want to use collect system to export Pearson and Spearman Correlation Coefficients in one table.
                            The upper triangle is Spearman correlation coefficient, and the lower triangle is Pearson correlation coefficient.

                            I can use the SSC command --corr2docx-- to export the table.
                            Code:
                            sysuse auto, clear
                            corr2docx mpg weight length rep78 foreign using correlation.docx, replace star  ///
                                    pearson(pw) spearman(pw)
                            The table is below.We can see the upper triangle is Spearman correlation coefficient and
                            the lower triangle is Pearson correlation coefficient.How can I use collect to get the same table.
                            Attached Files
                            Last edited by Raymond Zhang; 25 May 2021, 12:05.
                            Best regards.

                            Raymond Zhang
                            Stata 17.0,MP

                            Comment


                            • #15
                              @Jeff Pitblado (StataCorp)
                              Dear Jeff, have you seen my question? I want to combine the pearson correlations coefficient with spearman correlation coefficient in one table.
                              the upper triangle is Spearman correlation coefficient and the lower triangle is Pearson correlation coefficient.I will give you an example.

                              Code:
                              . sysuse auto
                              (1978 automobile data)
                              
                              . pwcorr price headroom mpg displacement
                              
                                           |    price headroom      mpg displa~t
                              -------------+------------------------------------
                                     price |   1.0000 
                                  headroom |   0.1145   1.0000 
                                       mpg |  -0.4686  -0.4138   1.0000 
                              displacement |   0.4949   0.4745  -0.7056   1.0000 
                              
                              . spearman price headroom mpg displacement,pw
                              (obs=74)
                              
                                           |    price headroom      mpg displa~t
                              -------------+------------------------------------
                                     price |   1.0000 
                                  headroom |   0.0969   1.0000 
                                       mpg |  -0.5419  -0.4866   1.0000 
                              displacement |   0.3737   0.4785  -0.7713   1.0000
                              Best regards.

                              Raymond Zhang
                              Stata 17.0,MP

                              Comment

                              Working...
                              X