Announcement

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

  • Align NLS and MLE estimates for the same variable in the same row -esttab-

    Hi Statalisters,

    I fitted a model both with NLS and MLE, so I'd like to produce a table with the estimates for the same independent variable in the same row, like this one:

    I've tried with -esttab- to get the table, but I'm basically still in this stage:

    Code:
    . sysuse auto, clear
    
    . // NLS estimates
    . generate const = 1
    . eststo nls: qui nl (price = exp({xb: mpg weight foreign const})), nolog
    
    . // MLE Estimates
    . eststo mle: qui mlexp (exp({theta})*-{b:mpg weight foreign _cons} ///
    >                 - lngamma(exp({theta})) ///
    >                 + (exp({theta})-1)*ln(price) - exp(-{b:})*price), nolog
    
    . nlcom (theta: exp(_b[theta:_cons]))
    
           theta:  exp(_b[theta:_cons])
    
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           theta |   13.94963   2.266414     6.15   0.000     9.507538    18.39172
    ------------------------------------------------------------------------------
    
    . // Tabulate NLS and MLE estimates
    . esttab nls mle, nonumber nostar se mtitle("NLS" "MLE")
    
    --------------------------------------
                          NLS          MLE
    --------------------------------------
    main                                  
    _cons             0.00489        2.635
                     (0.0102)      (0.162)
    --------------------------------------
    xb_weight                            
    _cons            0.000660            
                  (0.0000853)            
    --------------------------------------
    xb_foreign                            
    _cons               0.777            
                      (0.105)            
    --------------------------------------
    xb_const                              
    _cons               6.316            
                      (0.473)            
    --------------------------------------
    b                                    
    mpg                           0.000612
                                 (0.00930)
    
    weight                        0.000471
                               (0.0000758)
    
    foreign                          0.525
                                  (0.0825)
    
    _cons                            4.450
                                   (0.447)
    --------------------------------------
    N                      74           74
    --------------------------------------
    Standard errors in parentheses
    As you can see, estimates for the same variable are shown in different parts of the table depending on the estimation method. I tried the -esttab- options -rename- and -coeflabels-, but I couldn't produce a meaningful progress. For example,

    Code:
      . local coeflab xb_mpg:_cons mpg xb_weight:_cons weight xb_foreign:_cons foreign
    . local rename xb_cons:_weight weight
    
    . esttab nls mle, nonumber nostar se mtitle("NLS" "MLE")  ///
    >                                 coeflabels(`coeflab') rename(`rename')
    
    --------------------------------------
                          NLS          MLE
    --------------------------------------
    main                                  
    _cons             0.00489        2.635
                     (0.0102)      (0.162)
    --------------------------------------
    xb_weight                             
    weight           0.000660             
                  (0.0000853)             
    --------------------------------------
    xb_foreign                            
    foreign             0.777             
                      (0.105)             
    --------------------------------------
    xb_const                              
    _cons               6.316             
                      (0.473)             
    --------------------------------------
    b                                     
    mpg                           0.000612
                                 (0.00930)
    
    weight                        0.000471
                               (0.0000758)
    
    foreign                          0.525
                                  (0.0825)
    
    _cons                            4.450
                                   (0.447)
    --------------------------------------
    N                      74           74
    --------------------------------------
    Standard errors in parentheses
    Any suggestions are welcome! Thank you.

  • #2
    esttab is from Stata Journal/ SSC (FAQ Advice #12). Thanks for the reproducible example. The issue is not at the esttab level, you have to address differences in how Stata names the equations and coefficients across estimators. Ben Jann's program erepost from SSC can help here.

    Code:
    sysuse auto, clear
    eststo clear
    gen const=1
    qui nl (price = exp({xb: mpg weight foreign const})), nolog
    mat l e(b)
    qui mlexp (exp({theta})*-{b:mpg weight foreign _cons} - ///
    lngamma(exp({theta})) + (exp({theta})-1)*ln(price) ///
    - exp(-{b:})*price), nolog
    mat l e(b)
    Res.:

    Code:
    e(b)[1,4]
             xb_mpg:   xb_weight:  xb_foreign:    xb_const:
              _cons        _cons        _cons        _cons
    y1    .00488766    .00066012    .77669395    6.3156397
    
    . mat l e(b)
    
    e(b)[1,5]
            theta:         b:         b:         b:         b:
            _cons        mpg     weight    foreign      _cons
    y1  2.6354529  .00061219  .00047111  .52481295  4.4498879
    So we can rename the equations and coefficients in the first matrix as in the second matrix.

    Code:
    sysuse auto, clear
    eststo clear
    gen const=1
    qui nl (price = exp({xb: mpg weight foreign const})), nolog
    mat b=e(b)
    local coln "b:mpg b:weight b:foreign b:_cons"
    mat colnames b= `coln'
    capt prog drop replace_b
    program replace_b, eclass
    erepost b= b, rename
    end
    replace_b
    eststo nls
    eststo mle: qui mlexp (exp({theta})*-{b:mpg weight foreign _cons} - ///
    lngamma(exp({theta})) + (exp({theta})-1)*ln(price) ///
    - exp(-{b:})*price), nolog
    esttab nls mle, nonumber nostar se mtitle("NLS" "MLE")
    Res.:

    Code:
    . esttab nls mle, nonumber nostar se mtitle("NLS" "MLE")
    
    --------------------------------------
                          NLS          MLE
    --------------------------------------
    b                                    
    mpg               0.00489     0.000612
                     (0.0102)    (0.00930)
    
    weight           0.000660     0.000471
                  (0.0000853)  (0.0000758)
    
    foreign             0.777        0.525
                      (0.105)     (0.0825)
    
    _cons               6.316        4.450
                      (0.473)      (0.447)
    --------------------------------------
    theta                                
    _cons                            2.635
                                   (0.162)
    --------------------------------------
    N                      74           74
    --------------------------------------
    Standard errors in parentheses

    Comment


    • #3
      Thank you Andrew! Your suggestion helped a lot.

      I managed to include the true value for theta by adding the following right after the -mlexp- command expression:

      Code:
      mat b=e(b)
      qui nlcom (theta: exp(_b[theta:_cons]))
      mat b[1,1] = r(b)
      erepost b = b
      eststo mle
      As part of the topic we're discussing, I'd like to get a table with the estimate and three other columns with the standard errors that result from different corrections to the covariance matrix. For example, from these estimations

      Code:
      sysuse auto, clear
      
      eststo m1: regress price mpg weight foreign
      eststo m2: regress price mpg weight foreign, vce(robust)
      eststo m3: regress price mpg weight foreign, vce(hc2)
      I would like to have this table

      Click image for larger version

Name:	Screen Shot 2020-05-06 at 2.02.30 PM.png
Views:	3
Size:	160.1 KB
ID:	1551652

      I get something in the middle with this line,

      Code:
      esttab m1 m2 m3, nonumber nostar se mtitle("Model 1" "Model 2" "Model 3") wide
      Thank you.
      Attached Files

      Comment


      • #4
        Stata 16 allows direct extraction of matrices in e() and r(). For earlier versions, first save these matrices as regular Stata matrices. There is no need to repeat the number of observations as you are estimating the same model. Only the standard errors differ.

        Code:
        sysuse auto, clear
        regress price mpg weight foreign
        local N=e(N)
        matrix Res1 = r(table)[1..2, "mpg".."_cons"]
        regress price mpg weight foreign, vce(robust)
        matrix Res2 = r(table)[2, "mpg".."_cons"]
        regress price mpg weight foreign, vce(hc2)
        matrix Res3 = r(table)[2, "mpg".."_cons"]
        mat Res= (Res1\Res2\Res3)'
        esttab mat(Res), collabels("Beta" "Std. Error" "Robust" "HC2") nomtitles note("Observations         `N'")
        Res.:

        Code:
        . esttab mat(Res), collabels("Beta" "Std. Error" "Robust" "HC2") nomtitles note("Observations
        >          `N'")
        
        ----------------------------------------------------------------
                             Beta   Std. Error       Robust          HC2
        ----------------------------------------------------------------
        mpg               21.8536     74.22114     80.74674     83.18166
        weight           3.464706      .630749     .7776165     .8016721
        foreign           3673.06     683.9783     664.9361     677.9511
        _cons           -5853.696     3376.987     3873.723     3996.761
        ----------------------------------------------------------------
        Observations         74
        Last edited by Andrew Musau; 06 May 2020, 14:38.

        Comment


        • #5
          Superb Andrew!

          Fortunately, I use Stata 16, so things were easier. Based in your explanation about matrices, I also found the way of adding another column with the mean of the dependent variables. The challenge here was to add a new column with a zero for the constant. But, all in all, today’s work was largely done following your suggestions. Thank you!

          Comment


          • #6
            Hi community, I have a similar problem and need to align identical estimates via esttab. I managed to manipulate labels respective the estimation matrix, but it only works for my continuous interaction and not for former factor variables. Can someone help to adjust this scenario? Thank you very much


            Code:
            // data
            sysuse auto, clear
            
            // interaction model 1
            regress price c.mpg##c.mpg##foreign
            estimates store interaction1
               
            // interaction model 2
            gen mpg_mpg_foreign = mpg*mpg*foreign
            gen mpg_mpg = mpg*mpg
            gen mpg_foreign = mpg*foreign
            regress price mpg mpg_mpg foreign mpg_foreign mpg_mpg_foreign
            estimates store interaction2a
            
            // manual label manual interactions (continuous-continuous)
            *label variable mpg_mpg "Mileage (mpg) # Mileage (mpg)"
            *esttab interaction1 interaction2, label
            
            * store estimation results in matrix b
            mat b=e(b)
            * access all colnames in the matrix b
            local colnames: colnames b
            * substitute particular string in colnames list
            local colnames : subinstr local colnames "mpg_mpg" "c.mpg#c.mpg", all
            local colnames : subinstr local colnames "mpg_foreign" "foreign#c.mpg", all
            * local colnames : subinstr local colnames "foreign " "1.foreign", all
            
            * rename matrix colnames with new local list
            matrix colname b = `colnames'
            
            * replace all estimates with erepost package
            capt prog drop replace_b
            program replace_b, eclass
            erepost b= b, rename
            end
            replace_b
            
            estimates store interaction2b
            
            // make table
            esttab interaction1 interaction2b, nobaselevels
             
            esttab interaction1 interaction2b, nobaselevels label
            Click image for larger version

Name:	esttab.png
Views:	1
Size:	12.5 KB
ID:	1608848


            Comment


            • #7
              I think that you are making this more complicated than it should be. Examine one of the matrices and extract its column names, define a regression with the same number of regressors and repost the extracted columns to this regression's e(b) matrix.


              Code:
              // data
              sysuse auto, clear
              eststo clear
              
              // interaction model 1
              regress price c.mpg##c.mpg##foreign
              estimates store interaction1
              
              mat b=e(b)
              local cnames: colnames b
              mat l b
              
                
              // interaction model 2
              gen mpg_mpg_foreign = mpg*mpg*foreign
              gen mpg_mpg = mpg*mpg
              gen mpg_foreign = mpg*foreign
              gen zero=0
              regress price mpg mpg_mpg zero foreign zero mpg_foreign zero mpg_mpg_foreign
              estimates store interaction2a
              
              mat b= e(b)
              mat colnames b= `cnames'
              
              
              * replace all estimates with erepost package
              capt prog drop replace_b
              program replace_b, eclass
              erepost b= b, rename
              end
              replace_b
              
              estimates store interaction2b
              
              // make table
              esttab interaction1 interaction2b, nobaselevels
              esttab interaction1 interaction2b, nobaselevels label
              Res.:

              Code:
              . esttab interaction1 interaction2b, nobaselevels label
              
              ----------------------------------------------------
                                            (1)             (2)  
                                          Price           Price  
              ----------------------------------------------------
              Mileage (mpg)             -1964.8***      -1964.8***
                                        (-4.23)         (-4.23)  
              
              Mileage (mpg) # Mi~)        37.79***        37.79***
                                         (3.56)          (3.56)  
              
              Foreign                   -2874.1         -2874.1  
                                        (-0.36)         (-0.36)  
              
              Foreign # Mileage ~)        612.1           612.1  
                                         (0.90)          (0.90)  
              
              Foreign # Mileage ~g       -17.30          -17.30  
                                        (-1.24)         (-1.24)  
              
              Constant                  29337.9***      29337.9***
                                         (5.99)          (5.99)  
              ----------------------------------------------------
              Observations                   74              74  
              ----------------------------------------------------
              t statistics in parentheses
              * p<0.05, ** p<0.01, *** p<0.001
              
              .
              end of do-file
              
              .

              Comment


              • #8
                Hey Andrew, excellent support! I was wondering why you created the zero variable. But after inspecting your matrix b I noticed that Stata saves a zero for each baseline level of a factor variable. I'm trying to account for this in my model (which contains reasonably more factors). For a threefold interaction I have to use "3x zero". I also try to extend this procedure to multiple models.
                Last edited by Marco Kuehne; 17 May 2021, 05:10.

                Comment


                • #9
                  The way to avoid this is to specify indicators in the interaction with the positive category (i.e., using 1.catvar instead of i.catvar). Example:

                  Code:
                  sysuse auto, clear
                  regress price c.mpg##c.mpg##i.foreign
                  mat l e(b)
                  regress price c.mpg##c.mpg##1.foreign
                  mat l e(b)
                  Res.:

                  Code:
                  . regress price c.mpg##c.mpg##i.foreign
                  
                        Source |       SS           df       MS      Number of obs   =        74
                  -------------+----------------------------------   F(5, 68)        =     10.53
                         Model |   277159523         5  55431904.5   Prob > F        =    0.0000
                      Residual |   357905874        68  5263321.67   R-squared       =    0.4364
                  -------------+----------------------------------   Adj R-squared   =    0.3950
                         Total |   635065396        73  8699525.97   Root MSE        =    2294.2
                  
                  -------------------------------------------------------------------------------------
                                price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  --------------------+----------------------------------------------------------------
                                  mpg |  -1964.807   464.1278    -4.23   0.000     -2890.96   -1038.654
                                      |
                          c.mpg#c.mpg |   37.79287   10.60985     3.56   0.001     16.62124    58.96449
                                      |
                              foreign |
                             Foreign  |  -2874.139   8084.599    -0.36   0.723     -19006.7    13258.43
                                      |
                        foreign#c.mpg |
                             Foreign  |   612.0917   677.1573     0.90   0.369    -739.1546    1963.338
                                      |
                  foreign#c.mpg#c.mpg |
                             Foreign  |  -17.29979   13.95036    -1.24   0.219     -45.1373    10.53771
                                      |
                                _cons |   29337.92   4897.256     5.99   0.000      19565.6    39110.24
                  -------------------------------------------------------------------------------------
                  
                  . mat l e(b)
                  
                  e(b)[1,9]
                                                                                                  
                                         c.mpg#          0b.           1.  0b.foreign#   1.foreign#
                              mpg        c.mpg      foreign      foreign       co.mpg        c.mpg
                  y1   -1964.8069    37.792866            0    -2874.139            0    612.09169
                  
                       0b.foreign#   1.foreign#            
                           co.mpg#       c.mpg#            
                           co.mpg        c.mpg        _cons
                  y1            0    -17.29979    29337.919
                  
                  . regress price c.mpg##c.mpg##1.foreign
                  
                        Source |       SS           df       MS      Number of obs   =        74
                  -------------+----------------------------------   F(5, 68)        =     10.53
                         Model |   277159523         5  55431904.5   Prob > F        =    0.0000
                      Residual |   357905874        68  5263321.67   R-squared       =    0.4364
                  -------------+----------------------------------   Adj R-squared   =    0.3950
                         Total |   635065396        73  8699525.97   Root MSE        =    2294.2
                  
                  -------------------------------------------------------------------------------------
                                price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  --------------------+----------------------------------------------------------------
                                  mpg |  -1964.807   464.1278    -4.23   0.000     -2890.96   -1038.654
                                      |
                          c.mpg#c.mpg |   37.79287   10.60985     3.56   0.001     16.62124    58.96449
                                      |
                              foreign |
                             Foreign  |  -2874.139   8084.599    -0.36   0.723     -19006.7    13258.43
                                      |
                        foreign#c.mpg |
                             Foreign  |   612.0917   677.1573     0.90   0.369    -739.1546    1963.338
                                      |
                  foreign#c.mpg#c.mpg |
                             Foreign  |  -17.29979   13.95036    -1.24   0.219     -45.1373    10.53771
                                      |
                                _cons |   29337.92   4897.256     5.99   0.000      19565.6    39110.24
                  -------------------------------------------------------------------------------------
                  
                  . mat l e(b)
                  
                  e(b)[1,6]
                                                                       1.foreign#            
                                       c.mpg#          1.  1.foreign#      c.mpg#            
                             mpg       c.mpg     foreign       c.mpg       c.mpg       _cons
                  y1  -1964.8069   37.792866   -2874.139   612.09169   -17.29979   29337.919
                  
                  .

                  Comment

                  Working...
                  X