Announcement

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

  • etable: re-ordering variables with multiple equations

    The code below produces the table which follows. The keep option orders the variables the way I want-- in this example with tenure before the industry dummies. Is there an option other than keep which re-orders some variables without requiring a full list of variables to be retained?

    Thanks,
    Devra

    Code:
    clear
    webuse nlswork  
    global v1 "age"
    global v2 "age tenure"
    reg ln_wage ${v1}  i.ind_code
    estimates store e1
    reg ln_wage ${v2}  i.ind_code
    estimates store e2
    
    etable, estimates( e1 e2 )  keep(${v1} ${v2} i.ind_code) ///
    title("2 models") ///
      cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
      showeq showstars showstarsnote
    Code:
    2 models
    -------------------------------------------
                              ln_wage  ln_wage
    -------------------------------------------
    ln(wage/GNP deflator)                      
      Age in current year    .0171 ** .00959 **
                              44.6        23   
                             .0171 ** .00959 **
                              44.6        23   
      Job tenure, in years             .0314 **
                                        41.7   
      Industry of employment                   
        2                     .474 **   .471 **
                              7.27      7.39   
    [lines omitted]
        11                    .367 **   .322 **
                              13.2      11.8   
        12                      .5 **   .427 **
                                17      14.9   
    Number of observations   28169     27764   
    -------------------------------------------
    ** p<.01, * p<.05
    Devra Golbe
    Professor Emerita, Dept. of Economics
    Hunter College, CUNY

  • #2
    There does not appear to be a direct way of doing that. However, you may parse the command line to pick up the RHS variable names. The following assumes that levels of factor variables will be ordered together, e.g., 1.ind_code, 2.ind_code, ..., \(n\).ind_code. If you want to separate these levels, pick out the RHS variable names from the column names of matrix e(b).

    Code:
    clear
    webuse nlswork  
    *TEST THAT CODE IS ROBUST TO VARIABLES SHARING THE SAME PREFIX
    rename msp age2
    global v1 "age"
    global v2 "age tenure"
    reg ln_wage ${v1}  i.ind_code age2
    estimates store e1
    reg ln_wage ${v2}  i.ind_code age2
    estimates store e2
    
    local all= ustrregexra("`e(cmdline)'", "`e(cmd) e(depvar)'", "")
    *SPECIFY ORDER HERE
    local order age i.ind_code
    local rest: list all- order
    
    etable, estimates( e1 e2 )  keep(`order' `rest') ///
    title("2 models") ///
      cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
      showeq showstars showstarsnote
    Res.:

    Code:
    . etable, estimates( e1 e2 )  keep(`order' `rest') ///
    > title("2 models") ///
    >   cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
    >   showeq showstars showstarsnote
    
    2 models
    ----------------------------------------------------
                                     ln_wage    ln_wage
    ----------------------------------------------------
    ln(wage/GNP deflator)                              
      Age in current year            .0172 **  .00962 **
                                      44.5       22.9  
      Industry of employment                            
        2                             .475 **    .472 **
                                      7.27       7.39  
        3                             .406 **    .392 **
                                      10.5       10.4  
        4                             .294 **    .244 **
                                      10.5        8.9  
        5                             .618 **     .53 **
                                      20.8       18.2  
        6                            .0923 **   .0768 **
                                      3.27       2.79  
        7                             .414 **     .36 **
                                      14.3       12.8  
        8                             .289 **    .285 **
                                      9.26       9.35  
        9                            -.131 **   -.135 **
                                     -4.46      -4.71  
        10                            .278 **    .269 **
                                      6.95       6.86  
        11                            .367 **    .322 **
                                      13.1       11.8  
        12                            .499 **    .427 **
                                        17       14.9  
      Job tenure, in years                      .0313 **
                                                 41.6  
      1 if married, spouse present -.00878    -.00257  
                                     -1.67        -.5  
    Number of observations           28153      27749  
    ----------------------------------------------------
    ** p<.01, * p<.05
    Last edited by Andrew Musau; 02 Feb 2024, 15:21.

    Comment


    • #3
      Thanks, Andrew!

      When I rewrite your code a bit tenure shows up where I want it, right after age. (I've also substuituted race for ind_code, because it has fewer categories.)


      Code:
      clear
      webuse nlswork  
      *TEST THAT CODE IS ROBUST TO VARIABLES SHARING THE SAME PREFIX
      rename msp age2
      global v1 "age"
      global v2 "age tenure"
      reg ln_wage ${v1}  i.race age2
      estimates store e1
      reg ln_wage ${v2}  i.race age2
      estimates store e2
      
      local all= ustrregexra("`e(cmdline)'", "`e(cmd) e(depvar)'", "")
      *SPECIFY ORDER HERE
      local order age tenure
      local rest: list all- order
      
      etable, estimates( e1 e2 )  keep(`order' `rest') ///
      title("2 models") ///
        cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
        showeq showstars showstarsnote
      Code:
      2 models
      --------------------------------------------------
                                      ln_wage   ln_wage
      --------------------------------------------------
      ln(wage/GNP deflator)                             
        Age in current year             .02 **  .0102 **
                                       49.3      23.4   
        Job tenure, in years                    .0395 **
                                                 51.2   
        Race                                            
          Black                       -.144 **  -.149 **
                                      -23.6     -25.4   
          Other                       .0655 *   .0777 **
                                       2.49      3.08   
        1 if married, spouse present -.0332 ** -.0253 **
                                      -5.88     -4.65   
      Number of observations          28494     28086   
      --------------------------------------------------
      ** p<.01, * p<.05
      But when I use age2 in the first equation and coll in the send, neither shows up in the table. Did I miss something?

      Code:
      clear
      webuse nlswork  
      *TEST THAT CODE IS ROBUST TO VARIABLES SHARING THE SAME PREFIX
      rename msp age2
      global v1 "age"
      global v2 "age tenure"
      reg ln_wage ${v1}  i.race age2
      estimates store e1
      reg ln_wage ${v2}  i.race coll
      estimates store e2
      
      local all= ustrregexra("`e(cmdline)'", "`e(cmd) e(depvar)'", "")
      *SPECIFY ORDER HERE
      local order age tenure
      local rest: list all- order
      
      etable, estimates( e1 e2 )  keep(`order' `rest') ///
      title("2 models") ///
        cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
        showeq showstars showstarsnote
      Code:
      2 models
      -----------------------------------------
                              ln_wage  ln_wage
      -----------------------------------------
      ln(wage/GNP deflator)                    
        Age in current year    .02 ** .00696 **
                              49.3      16.8   
        Job tenure, in years           .0382 **
                                        52.2   
        Race                                   
          Black              -.144 **  -.114 **
                             -23.6     -20.7   
          Other              .0655 *   .0503 *
                              2.49       2.1   
      Number of observations 28494     28101   
      -----------------------------------------
      ** p<.01, * p<.05
      Devra Golbe
      Professor Emerita, Dept. of Economics
      Hunter College, CUNY

      Comment


      • #4
        Two points:

        1. If the RHS variables are not common across regressions, you have to define the local "all" after each regression.
        2. Variable abbreviation bites here. Stata allows you to enter the variable "collgrad" as "coll'. But since what is stored is the full variable name, specifying -keep(coll)- won't capture the variable. You need to enter the full name.

        Code:
        clear
        webuse nlswork  
        *TEST THAT CODE IS ROBUST TO VARIABLES SHARING THE SAME PREFIX
        rename msp age2
        global v1 "age"
        global v2 "age tenure"
        reg ln_wage ${v1}  i.race age2
        estimates store e1
        *FIRST TIME
        local all= ustrregexra("`e(cmdline)'", "`e(cmd) e(depvar)'", "")
        
        *DO NOT ABBREVIATE VARNAMES
        reg ln_wage ${v2}  i.race collgrad
        estimates store e2
        
        *ALL SUBSEQUENT TIMES
        local all `all' `=ustrregexra("`e(cmdline)'", "`e(cmd) e(depvar)'", "")'
        local all: list uniq all
        
        *SPECIFY ORDER HERE
        local order age tenure
        local rest: list all- order
        
        etable, estimates( e1 e2 )  keep(`order' `rest') ///
        title("2 models") ///
          cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
          showeq showstars showstarsnote
        Res.:

        Code:
        . etable, estimates( e1 e2 )  keep(`order' `rest') ///
        > title("2 models") ///
        >   cstat(_r_b, nformat(%9.3g)) cstat(_r_z, nformat(%9.3g)) ///
        >   showeq showstars showstarsnote
        
        2 models
        --------------------------------------------------
                                        ln_wage   ln_wage
        --------------------------------------------------
        ln(wage/GNP deflator)                            
          Age in current year             .02 ** .00696 **
                                         49.3      16.8  
          Job tenure, in years                    .0382 **
                                                   52.2  
          Race                                            
            Black                       -.144 **  -.114 **
                                        -23.6     -20.7  
            Other                       .0655 *   .0503 *
                                         2.49       2.1  
          1 if married, spouse present -.0332 **          
                                        -5.88            
          1 if college graduate                    .372 **
                                                   55.6  
        Number of observations          28494     28101  
        --------------------------------------------------
        ** p<.01, * p<.05
        Last edited by Andrew Musau; 02 Feb 2024, 17:03.

        Comment


        • #5
          Thank you!
          Devra Golbe
          Professor Emerita, Dept. of Economics
          Hunter College, CUNY

          Comment

          Working...
          X