Announcement

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

  • collect: rows and columns' name changed?

    I am trying to learn Stata's -collect- and to manipulate tables. I find the manipulating part hard.


    What I did it with six:

    Code:
    collect clear
    * add custom results has_year_fe and has_panel
    collect _r_b _r_se has_year_fe="NO" has_panel="NO", tag(model[(1)]): reg d.ln_co2pc l.co2pc d.ln_gdppc d.URB, r
    collect _r_b _r_se has_year_fe="NO" has_panel="SI", tag(model[(2)]): xtreg d.ln_co2pc l.co2pc d.ln_gdppc d.URB,fe r
    collect _r_b _r_se has_year_fe="SI" has_panel="SI", tag(model[(3)]): xtreg d.ln_co2pc l.co2pc d.ln_gdppc d.URB i.year,fe r
    collect _r_b _r_se has_year_fe="SI" has_panel="SI", tag(model[(4)]): xtreg d.ln_co2pc l.co2pc d.ln_gdppc d.URB d.ei i.year,fe r
    collect _r_b _r_se has_year_fe="SI" has_panel="SI", tag(model[(5)]): xtreg d.ln_co2pc l.co2pc d.ln_gdppc d.URB d.res_share i.year,fe r
    collect _r_b _r_se has_year_fe="SI" has_panel="SI", tag(model[(6)]): xtreg d.ln_co2pc l.co2pc d.ln_gdppc d.URB d.ei d.res_share 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) 
    * modify layout to accommodate our custom results and suppress the i.year results
    collect layout (colname[d.ln_co2pc l.co2pc d.ln_gdppc d.URB d.ei##d.res_share]#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 "Efectos fijos año" has_panel "Efectos fijos país" N "N" r2 "R2", modify
    collect style cell result[N], nformat(%5.0f)
    collect preview
    
    -----------------------------------------------------------------------------------
                           (1)        (2)        (3)        (4)        (5)        (6)  
    -----------------------------------------------------------------------------------
    L.co2pc             -0.00*     -0.00      -0.00      -0.00      -0.00      -0.00   
                         (0.00)     (0.00)     (0.00)     (0.00)     (0.00)     (0.00) 
                                                                                       
    D.ln_gdppc           1.06***    1.08***    1.06***    1.10***    0.83***    0.87***
                         (0.11)     (0.16)     (0.21)     (0.20)     (0.15)     (0.15) 
                                                                                       
    D.URB                0.00***    0.00***    0.00**     0.00**     0.00*      0.00*  
                         (0.00)     (0.00)     (0.00)     (0.00)     (0.00)     (0.00) 
                                                                                       
    D.ei                                                  0.07***               0.08***
                                                          (0.02)                (0.03) 
                                                                                       
    D.res_share                                                     -0.02***   -0.02***
                                                                     (0.00)     (0.00) 
                                                                                       
    Efectos fijos año      NO         NO         SI         SI         SI         SI   
                                                                                       
    Efectos fijos país     NO         SI         SI         SI         SI         SI   
                                                                                       
    R2                    0.21       0.21       0.27       0.28       0.44       0.44  
                                                                                       
    N                     1100       1100       1100       1100       1089       1089  
    -----------------------------------------------------------------------------------
    I would like to change columns' names:
    Code:
    ---------------------------------------------------------------------------------------------------
                                       Tasa crecimiento emisiones CO2 pc
                            ---------------------------------------------------------------
                            Modelo 1   Modelo 2   Modelo 3   Modelo 4   Modelo 5   Modelo 6
    ---------------------------------------------------------------------------------------------------
    Emision CO2pc período anterior      -0.00*     -0.00      -0.00      -0.00      -0.00      -0.00   
                                     (0.00)     (0.00)     (0.00)     (0.00)     (0.00)     (0.00) 
                                                                                       
    Tasa crecimiento PIB pc            1.06***    1.08***    1.06***    1.10***    0.83***    0.87***
                                       (0.11)     (0.16)     (0.21)     (0.20)     (0.15)     (0.15) 
                                                                                       
    Variación Urbanismo               0.00***    0.00***    0.00**     0.00**     0.00*      0.00*  
                                     (0.00)     (0.00)     (0.00)     (0.00)     (0.00)     (0.00) 
                                                                                       
    Variación Intensidad Energética                            0.07***               0.08***
                                                               (0.02)                (0.03) 
                                                                                       
    Variación Participación ER                                                      -0.02***   -0.02***
                                                                                     (0.00)     (0.00) 
                                                                                       
    Efectos fijos año                  NO         NO         SI         SI         SI         SI   
                                                                                       
    Efectos fijos país                NO         SI         SI         SI         SI         SI   
                                                                                       
    R2                                0.21       0.21       0.27       0.28       0.44       0.44  
                                                                                       
    Número de observaciones            1100       1100       1100       1100       1089       1089  
    ------------------------------------------------------------------------------------------------------
    And to use continuos line but I don't know how to do it.

    Can someone help me?

    Regards,

    Sebastian.

  • #2
    To change col names use label dimension levels, see -help collect label-:
    Code:
    collect label levels dim level "label" [level "label" ...] [, name(cname) modify replace]
    some refs:To change the levels refer to the levels of the dim, here defined by tag():
    Code:
    webuse nhanes2l
    
    collect clear
    qui collect get , tag(model["(1)"]) : reg highbp age
    qui collect get , tag(model["(2)"]) : reg highbp age i.sex
    
    collect levelsof model
     
    collect label levels model "(1)" "Model 1" "(2)" "Model 2"
     
    collect layout (colname#result[_r_b]) (model)
    or change the definition in the tag definition:
    Code:
    qui collect get , tag(model["Model 1"]) : reg highbp age 
    qui collect get , tag(model["Model 2"]) : reg highbp age i.sex
    Last edited by Bjarte Aagnes; 19 Jun 2022, 04:14.

    Comment


    • #3
      Dear Bjarte,

      I read all your references but I cannot find how to do it changes. I am a newby!

      I would be so pleased if you can change my code to obtain my wished table.

      Can you show me in my example how to add "Tasa crecimiento emisiones CO2 pc" over tags (1), (2), ..., (6)?

      What is the code to change "L.co2pc" to "Emision CO2pc período anterior?

      I would like to do it with Stata and not to use modify in Word or Excel.

      See you,

      Sebastian.

      Comment


      • #4
        Bjarte provided code here. And he seems to be, to exaggerate only slightly, like a wizard on this subject. He seems to be the main contributor with the collect prefix. I would know, because his code's been absolutely quintessential to lots of my ado code working, on this exact subject.

        So, please try what he's suggested. I'm 25 now, I've used Stata since I was 19-ish, and even though I think I'm a pretty good Stata programmer, we're all new to something, and here at no matter how good you become, you'll find there's always more to learn, things you'd always needed but didn't know existed.

        Comment


        • #5
          Ref #3: First, make sure your Stata is updated -help update-. Several fixes has been done to collect the last updates, relevant here is update 06apr2022 number13 -help whatsnew17-.

          To get a common label centered above the cols:
          Code:
          webuse nhanes2l, clear
          
          collect clear
          
          qui collect get , tag(model["(1)"]) : reg highbp age
          qui collect get , tag(model["(2)"]) : reg highbp age i.sex
          
          collect levelsof model
           
          collect label levels model "(1)" "Model 1" "(2)" "Model 2"
          
          collect label dim model "common title"
          collect style header model, title(label)
          collect style column, dups(center)
          
          collect layout (colname#result[_r_b]) (model)
          Code:
          Collection: default
                Rows: colname#result[_r_b]
             Columns: model
             Table 1: 8 x 2
          
          -----------------------------------
                        |     common title   
                        |   Model 1   Model 2
          --------------+--------------------
          Age (years)   |                    
            Coefficient |  .0105478  .0105704
          Male          |                    
            Coefficient |                   0
          Female        |                    
            Coefficient |           -.0907434
          Intercept     |                    
            Coefficient | -.0790988 -.0325216
          -----------------------------------
          Changing levels for the rows should be similar to the example in #2 using -collect label levels- when using current update of Stata17:
          Code:
          sysuse census , clear
          generate t = _n
          tsset t
          regress death d.medage iL.region [aw=pop]
          
          collect clear
          collect get _r_b
          collect layout (colname)(result)
          
          /*
          Collection: default Rows: colname Columns: result Table 1: 6 x 1 ----------------------- | Coefficient ----------+------------ D.medage | 6078.444 L.NE | 0 L.N Cntrl | -10196.87 L.South | 42365.4 L.West | 29926.26 Intercept | 57584.25 -----------------------
          */ . collect levelsof colname /*
          Collection: default Dimension: colname Levels: D.medage 1L.region 2L.region 3L.region 4L.region _cons
          */ . collect label levels colname /// D.medage "difference medage" /// 1L.region "lag var NE" /// 2L.region "lag N Cntrl" /// 3L.region "lag South" /// 4L.region "lag West" . collect preview /*
          ------------------------------- | Coefficient ------------------+------------ difference medage | 6078.444 lag var NE | 0 lag N Cntrl | -10196.87 lag South | 42365.4 lag West | 29926.26 Intercept | 57584.25 -------------------------------
          */

          Comment

          Working...
          X