Announcement

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

  • Running regressions bysort and creating a variable from a particular coefficient?

    Hi,

    I am running a regression using by sort for different states for different time periods. The regression that I am running:

    Code:
    bysort group: reg log_hourly_wage log_head_wage age age2 [weight=FWT], vce(cluster HHBASE)
    I want to create a variable equaling the coefficients of log_head_wage for the different regressions. Some of the tables from the regression are as follows:

    Code:
    -> group = IHDS1 1 Maharashtra 27
    (sum of wgt is 1,271,084)
    
    Linear regression                               Number of obs     =        213
                                                    F(3, 194)         =      25.86
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.4375
                                                    Root MSE          =      .4996
    
                                    (Std. Err. adjusted for 195 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .4896915   .0659689     7.42   0.000     .3595831    .6197998
              age |    .058052   .0451598     1.29   0.200    -.0310152    .1471192
             age2 |  -.0005804   .0008478    -0.68   0.494    -.0022524    .0010917
            _cons |   .2539306   .5793389     0.44   0.662    -.8886806    1.396542
    -------------------------------------------------------------------------------
    
    ----------------------------------------------------------------------------------------------------------------------------------
    -> group = IHDS1 1 Andhra Pradesh 28
    (sum of wgt is 1,198,790)
    
    Linear regression                               Number of obs     =        165
                                                    F(3, 147)         =      16.58
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.3463
                                                    Root MSE          =     .42385
    
                                    (Std. Err. adjusted for 148 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .5209964   .0926074     5.63   0.000     .3379826    .7040101
              age |   .1041692   .0508455     2.05   0.042     .0036865    .2046519
             age2 |  -.0017842    .001097    -1.63   0.106     -.003952    .0003837
            _cons |  -.1354863   .5649494    -0.24   0.811    -1.251958    .9809854
    -------------------------------------------------------------------------------
    
    ----------------------------------------------------------------------------------------------------------------------------------
    -> group = IHDS1 1 Karnataka 29
    (sum of wgt is 817,074)
    
    Linear regression                               Number of obs     =        302
                                                    F(3, 266)         =      35.23
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.3672
                                                    Root MSE          =     .46594
    
                                    (Std. Err. adjusted for 267 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .5086444   .0698769     7.28   0.000     .3710622    .6462267
              age |   .1168777   .0311152     3.76   0.000     .0556143    .1781412
             age2 |  -.0017555   .0006355    -2.76   0.006    -.0030066   -.0005043
            _cons |  -.4971166   .3684162    -1.35   0.178      -1.2225    .2282663
    -------------------------------------------------------------------------------
    
    -> group = IHDS2 2 Goa 30
    (sum of wgt is 61,914)
    
    Linear regression                               Number of obs     =         14
                                                    F(3, 9)           =       5.89
                                                    Prob > F          =     0.0165
                                                    R-squared         =     0.6524
                                                    Root MSE          =     .54908
    
                                     (Std. Err. adjusted for 10 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .7001349   .4157968     1.68   0.127    -.2404629    1.640733
              age |  -.1718412   .2350178    -0.73   0.483    -.7034884     .359806
             age2 |    .003321   .0031315     1.06   0.317     -.003763     .010405
            _cons |   2.900353   4.014352     0.72   0.488    -6.180741    11.98145
    -------------------------------------------------------------------------------
    
    ----------------------------------------------------------------------------------------------------------------------------------
    -> group = IHDS2 2 Kerala 32
    (sum of wgt is 358,505)
    
    Linear regression                               Number of obs     =         83
                                                    F(3, 75)          =      11.57
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.3706
                                                    Root MSE          =      .4588
    
                                     (Std. Err. adjusted for 76 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .4552167    .118712     3.83   0.000     .2187302    .6917031
              age |   .2023204   .0479121     4.22   0.000     .1068745    .2977662
             age2 |  -.0030443   .0008444    -3.61   0.001    -.0047264   -.0013621
            _cons |  -1.039481   .8385517    -1.24   0.219    -2.709962    .6309995
    -------------------------------------------------------------------------------
    
    ----------------------------------------------------------------------------------------------------------------------------------
    -> group = IHDS2 2 Tamil Nadu 33
    (sum of wgt is 1,772,150)
    
    Linear regression                               Number of obs     =        225
                                                    F(3, 186)         =      13.15
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.3120
                                                    Root MSE          =     .45707
    
                                    (Std. Err. adjusted for 187 clusters in HHBASE)
    -------------------------------------------------------------------------------
                  |               Robust
    log_hourly_~e |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    log_head_wage |   .4604571   .0966749     4.76   0.000     .2697368    .6511774
              age |   .1406676   .0535941     2.62   0.009     .0349371    .2463982
             age2 |  -.0019522   .0010247    -1.91   0.058    -.0039738    .0000694
            _cons |  -.4447117   .8278055    -0.54   0.592    -2.077806    1.188383

  • #2
    Jose:
    you may want to consider:
    Code:
    statsby, by(group): reg log_hourly_wage log_head_wage age age2 [weight=FWT], vce(cluster HHBASE)
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Dear Carlo Lazzaro ,

      Thank you for your reply. I get the error when I run your code: no; dataset in memory has changed since last saved.

      I tried something like and this seems to work.

      Code:
      capture program drop state_IGE
      program define state_IGE
          regress log_hourly_wage log_head_wage age age2 [weight=FWT], vce(cluster HHBASE)
          foreach v of varlist log_head_wage {
              gen b`v' = _b[`v']
          }
          exit
      end
      
      runby state_IGE, by(group)
      Last edited by Jose Williams; 18 Apr 2022, 06:24.

      Comment


      • #4
        Jose:
        works for me on -auto.dta- dataset (no -weight-, too):
        Code:
        . use "C:\Program Files\Stata17\ado\base\a\auto.dta"
        (1978 automobile data)
        
        . statsby, by(foreign): regress price mpg headroom trunk, vce(cluster foreign)
        (running regress on estimation sample)
        
              Command: regress price mpg headroom trunk, vce(cluster foreign)
                   By: foreign
        
        Statsby groups
        ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
        ..
        
        . list
        
             +--------------------------------------------------------+
             |  foreign      _b_mpg   _b_head~m   _b_trunk    _b_cons |
             |--------------------------------------------------------|
          1. | Domestic   -317.9484   -783.7371   142.0722   12752.58 |
          2. |  Foreign   -236.1947    414.3798   138.0251   9578.089 |
             +--------------------------------------------------------+
        
        .
        Great that your code works as expected.
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment

        Working...
        X