Announcement

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

  • xtreg how to see estimated coefficient for each obserevation

    Dear Stata users,
    I'm using xtreg and need to generate a variable to give the estimated coefficient for a specific explanatory variables for each observation. Does anyone know how to do this, please? Many thanks for your kind help.
    Best wishes,
    Meng Song

  • #2
    a variable to give the estimated coefficient for a specific explanatory variables for each observation.
    A coefficient for each observation? I don't understand.

    Comment


    • #3
      Meng:
      I do share Jared's surprise.
      Do you mean a coefficient for each panel (even though the purpose of such an approach looks obscure to me)?
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        Many thanks Gared and Carlo. Sorry for this strange question. I'm not sure about this either. I saw a paper with a graph showing the estimated marginal effects for each industry sector using xtreg. This doesn't come from estimating coefficients for each sector seperately. Instead, they estimate coefficients for all the sectors and then estimate the marginal effects for each sector to reflect which sector benefits for an explanatory variable the most and to the least. I'm trying to do a similar thing, but not sure if this is the way to do this.

        Comment


        • #5
          Meng:
          that make (more) sense.
          Let's assume that you want to go -xtreg,fe-:
          Code:
          . use "https://www.stata-press.com/data/r17/nlswork.dta"
          (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
          
          . bysort ind_code: xtreg ln_wage c.age##c.age, fe
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 1
          
          Fixed-effects (within) regression               Number of obs     =        241
          Group variable: idcode                          Number of groups  =        129
          
          R-squared:                                      Obs per group:
               Within  = 0.0848                                         min =          1
               Between = 0.0668                                         avg =        1.9
               Overall = 0.1040                                         max =          9
          
                                                          F(2,110)          =       5.09
          corr(u_i, Xb) = 0.0780                          Prob > F          =     0.0077
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |    .083563   .0373807     2.24   0.027     .0094833    .1576428
                       |
           c.age#c.age |  -.0011106   .0005965    -1.86   0.065    -.0022928    .0000717
                       |
                 _cons |  -.0572729   .5670733    -0.10   0.920    -1.181079    1.066533
          -------------+----------------------------------------------------------------
               sigma_u |  .41772157
               sigma_e |  .26583106
                   rho |  .71175198   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(128, 110) = 5.39                    Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 2
          
          Fixed-effects (within) regression               Number of obs     =         52
          Group variable: idcode                          Number of groups  =         25
          
          R-squared:                                      Obs per group:
               Within  = 0.0664                                         min =          1
               Between = 0.0021                                         avg =        2.1
               Overall = 0.0003                                         max =          8
          
                                                          F(2,25)           =       0.89
          corr(u_i, Xb) = -0.4338                         Prob > F          =     0.4234
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |  -.0694267   .1477619    -0.47   0.643    -.3737481    .2348947
                       |
           c.age#c.age |   .0015337   .0023712     0.65   0.524    -.0033498    .0064172
                       |
                 _cons |   2.528624     2.2797     1.11   0.278    -2.166505    7.223753
          -------------+----------------------------------------------------------------
               sigma_u |  .40347085
               sigma_e |  .34370956
                   rho |  .57947399   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(24, 25) = 1.86                      Prob > F = 0.0644
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 3
          
          Fixed-effects (within) regression               Number of obs     =        252
          Group variable: idcode                          Number of groups  =        136
          
          R-squared:                                      Obs per group:
               Within  = 0.0841                                         min =          1
               Between = 0.0082                                         avg =        1.9
               Overall = 0.0407                                         max =         13
          
                                                          F(2,114)          =       5.23
          corr(u_i, Xb) = -0.1991                         Prob > F          =     0.0067
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |  -.0127225   .0592723    -0.21   0.830    -.1301405    .1046955
                       |
           c.age#c.age |   .0006172   .0009344     0.66   0.510    -.0012338    .0024682
                       |
                 _cons |   1.595256   .9147096     1.74   0.084    -.2167766    3.407289
          -------------+----------------------------------------------------------------
               sigma_u |  .40566329
               sigma_e |  .28767498
                   rho |  .66538481   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(135, 114) = 3.54                    Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 4
          
          Fixed-effects (within) regression               Number of obs     =      5,841
          Group variable: idcode                          Number of groups  =      1,562
          
          R-squared:                                      Obs per group:
               Within  = 0.1013                                         min =          1
               Between = 0.0678                                         avg =        3.7
               Overall = 0.0673                                         max =         15
          
                                                          F(2,4277)         =     240.96
          corr(u_i, Xb) = 0.0182                          Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0289263   .0052082     5.55   0.000     .0187155    .0391372
                       |
           c.age#c.age |  -.0002448   .0000864    -2.83   0.005    -.0004142   -.0000754
                       |
                 _cons |   1.051751   .0758032    13.87   0.000      .903137    1.200364
          -------------+----------------------------------------------------------------
               sigma_u |  .34578795
               sigma_e |  .21374782
                   rho |  .72353359   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(1561, 4277) = 8.48                  Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 5
          
          Fixed-effects (within) regression               Number of obs     =      1,420
          Group variable: idcode                          Number of groups  =        418
          
          R-squared:                                      Obs per group:
               Within  = 0.3079                                         min =          1
               Between = 0.1203                                         avg =        3.4
               Overall = 0.1809                                         max =         14
          
                                                          F(2,1000)         =     222.47
          corr(u_i, Xb) = -0.0803                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0754539   .0123799     6.09   0.000     .0511603    .0997475
                       |
           c.age#c.age |   -.000705   .0002048    -3.44   0.001    -.0011068   -.0003032
                       |
                 _cons |   .4453384   .1805093     2.47   0.014     .0911179     .799559
          -------------+----------------------------------------------------------------
               sigma_u |  .40924108
               sigma_e |  .23191007
                   rho |  .75692783   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(417, 1000) = 8.47                   Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 6
          
          Fixed-effects (within) regression               Number of obs     =      4,946
          Group variable: idcode                          Number of groups  =      1,768
          
          R-squared:                                      Obs per group:
               Within  = 0.0818                                         min =          1
               Between = 0.0546                                         avg =        2.8
               Overall = 0.0640                                         max =         15
          
                                                          F(2,3176)         =     141.44
          corr(u_i, Xb) = -0.0360                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0257349   .0079368     3.24   0.001     .0101731    .0412967
                       |
           c.age#c.age |  -.0001354   .0001328    -1.02   0.308    -.0003956    .0001249
                       |
                 _cons |   .8491565   .1139542     7.45   0.000     .6257252    1.072588
          -------------+----------------------------------------------------------------
               sigma_u |  .36119112
               sigma_e |  .29076636
                   rho |  .60677458   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(1767, 3176) = 3.95                  Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 7
          
          Fixed-effects (within) regression               Number of obs     =      2,423
          Group variable: idcode                          Number of groups  =        716
          
          R-squared:                                      Obs per group:
               Within  = 0.1958                                         min =          1
               Between = 0.0688                                         avg =        3.4
               Overall = 0.1029                                         max =         15
          
                                                          F(2,1705)         =     207.51
          corr(u_i, Xb) = -0.1045                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |     .02301    .010137     2.27   0.023     .0031278    .0428923
                       |
           c.age#c.age |    .000053   .0001669     0.32   0.751    -.0002743    .0003803
                       |
                 _cons |   1.082766   .1486301     7.28   0.000     .7912498    1.374283
          -------------+----------------------------------------------------------------
               sigma_u |    .409073
               sigma_e |  .24380631
                   rho |  .73789166   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(715, 1705) = 6.93                   Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 8
          
          Fixed-effects (within) regression               Number of obs     =        846
          Group variable: idcode                          Number of groups  =        494
          
          R-squared:                                      Obs per group:
               Within  = 0.1472                                         min =          1
               Between = 0.0370                                         avg =        1.7
               Overall = 0.0395                                         max =         11
          
                                                          F(2,350)          =      30.21
          corr(u_i, Xb) = -0.2050                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0678358   .0243745     2.78   0.006      .019897    .1157747
                       |
           c.age#c.age |  -.0007052   .0004012    -1.76   0.080    -.0014942    .0000839
                       |
                 _cons |   .3230153   .3586533     0.90   0.368    -.3823713    1.028402
          -------------+----------------------------------------------------------------
               sigma_u |  .44317681
               sigma_e |  .23188395
                   rho |  .78507051   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(493, 350) = 5.38                    Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 9
          
          Fixed-effects (within) regression               Number of obs     =      1,710
          Group variable: idcode                          Number of groups  =        799
          
          R-squared:                                      Obs per group:
               Within  = 0.0855                                         min =          1
               Between = 0.0921                                         avg =        2.1
               Overall = 0.0785                                         max =         13
          
                                                          F(2,909)          =      42.51
          corr(u_i, Xb) = -0.0323                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0918517   .0194908     4.71   0.000     .0535996    .1301039
                       |
           c.age#c.age |  -.0011818   .0003248    -3.64   0.000    -.0018192   -.0005444
                       |
                 _cons |  -.3516255   .2798863    -1.26   0.209     -.900924    .1976729
          -------------+----------------------------------------------------------------
               sigma_u |  .47299644
               sigma_e |  .34859445
                   rho |   .6480223   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(798, 909) = 3.58                    Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 10
          
          Fixed-effects (within) regression               Number of obs     =        215
          Group variable: idcode                          Number of groups  =        128
          
          R-squared:                                      Obs per group:
               Within  = 0.1431                                         min =          1
               Between = 0.1174                                         avg =        1.7
               Overall = 0.1297                                         max =         11
          
                                                          F(2,85)           =       7.10
          corr(u_i, Xb) = -0.0930                         Prob > F          =     0.0014
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .2048109   .0603125     3.40   0.001     .0848934    .3247284
                       |
           c.age#c.age |  -.0029038   .0009294    -3.12   0.002    -.0047518   -.0010559
                       |
                 _cons |  -1.690479   .9375761    -1.80   0.075    -3.554631    .1736737
          -------------+----------------------------------------------------------------
               sigma_u |  .49958513
               sigma_e |  .31594208
                   rho |  .71431581   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(127, 85) = 4.69                     Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 11
          
          Fixed-effects (within) regression               Number of obs     =      8,475
          Group variable: idcode                          Number of groups  =      2,318
          
          R-squared:                                      Obs per group:
               Within  = 0.0752                                         min =          1
               Between = 0.0578                                         avg =        3.7
               Overall = 0.0476                                         max =         15
          
                                                          F(2,6155)         =     250.29
          corr(u_i, Xb) = 0.0074                          Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |    .017348    .005573     3.11   0.002     .0064231    .0282729
                       |
           c.age#c.age |  -.0000373   .0000893    -0.42   0.676    -.0002125    .0001378
                       |
                 _cons |   1.281788    .084381    15.19   0.000     1.116371    1.447204
          -------------+----------------------------------------------------------------
               sigma_u |  .42903126
               sigma_e |  .25357589
                   rho |   .7411076   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(2317, 6155) = 8.90                  Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = 12
          
          Fixed-effects (within) regression               Number of obs     =      1,748
          Group variable: idcode                          Number of groups  =        556
          
          R-squared:                                      Obs per group:
               Within  = 0.2233                                         min =          1
               Between = 0.0969                                         avg =        3.1
               Overall = 0.1205                                         max =         15
          
                                                          F(2,1190)         =     171.02
          corr(u_i, Xb) = -0.0995                         Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |   .0429337   .0107074     4.01   0.000     .0219262    .0639412
                       |
           c.age#c.age |  -.0002867   .0001716    -1.67   0.095    -.0006234      .00005
                       |
                 _cons |   .8791138   .1622294     5.42   0.000     .5608262    1.197401
          -------------+----------------------------------------------------------------
               sigma_u |  .38358906
               sigma_e |  .20810646
                   rho |   .7725988   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(555, 1190) = 7.54                   Prob > F = 0.0000
          
          ----------------------------------------------------------------------------------------------------------------------------------------
          -> ind_code = .
          
          Fixed-effects (within) regression               Number of obs     =        341
          Group variable: idcode                          Number of groups  =        321
          
          R-squared:                                      Obs per group:
               Within  = 0.2050                                         min =          1
               Between = 0.0180                                         avg =        1.1
               Overall = 0.0182                                         max =          2
          
                                                          F(2,18)           =       2.32
          corr(u_i, Xb) = -0.2217                         Prob > F          =     0.1269
          
          ------------------------------------------------------------------------------
               ln_wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   age |  -.1167352   .1405058    -0.83   0.417    -.4119269    .1784565
                       |
           c.age#c.age |   .0023781   .0023985     0.99   0.335    -.0026609    .0074171
                       |
                 _cons |   2.910309   1.960937     1.48   0.155    -1.209467    7.030086
          -------------+----------------------------------------------------------------
               sigma_u |  .44057017
               sigma_e |  .19684317
                   rho |  .83359524   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(320, 18) = 4.63                     Prob > F = 0.0002
          
          .
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Thank you very much Carlo. This helps. I'm thinking about after xtreg, using margins, dydx(var) subpop(if industry == ...). This is becausee I'm thiking about instead of running regression on different sectors seperately, I could estimate coefficients using all the sectors and then generate marginal effects for different subpopulation. However, unfortunately this says margins are not estimable. I'm still searching for an answer.

            Comment


            • #7
              Meng:
              do you mean something along the following lines?
              Code:
              use "https://www.stata-press.com/data/r17/nlswork.dta"
              quietly xtreg ln_wage c.age##c.age i.ind_code, fe
              margins, dydx(ind_code)
              
              Average marginal effects                                Number of obs = 28,169
              Model VCE: Conventional
              
              Expression: Linear prediction, predict()
              dy/dx wrt:  2.ind_code 3.ind_code 4.ind_code 5.ind_code 6.ind_code 7.ind_code 8.ind_code 9.ind_code 10.ind_code 11.ind_code
                          12.ind_code
              
              ------------------------------------------------------------------------------
                           |            Delta-method
                           |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                  ind_code |
                        2  |   .2441283    .057329     4.26   0.000     .1317656    .3564911
                        3  |   .1309688   .0353472     3.71   0.000     .0616895    .2002481
                        4  |   .1950476   .0272111     7.17   0.000     .1417148    .2483804
                        5  |   .2820969   .0296861     9.50   0.000     .2239132    .3402807
                        6  |  -.0204239   .0271743    -0.75   0.452    -.0736846    .0328369
                        7  |   .1285679   .0283902     4.53   0.000     .0729241    .1842118
                        8  |    .044726   .0295034     1.52   0.130    -.0130997    .1025517
                        9  |  -.1643109   .0281908    -5.83   0.000    -.2195638   -.1090579
                       10  |    .040567    .037616     1.08   0.281     -.033159    .1142931
                       11  |    .056187   .0273143     2.06   0.040      .002652    .1097221
                       12  |   .1801689   .0291056     6.19   0.000      .123123    .2372149
              ------------------------------------------------------------------------------
              Note: dy/dx for factor levels is the discrete change from the base level.
              Kind regards,
              Carlo
              (StataNow 18.5)

              Comment


              • #8
                Dear Carlo,
                Sorry for my late reply. I'm thinking, perhaps I should use
                quietly xtreg ln_wage c.age##c.age##i.ind_code, fe margins, dydx(ind_code) So in this way, I could see marginal effects of age in different sectors, without having to run the regression "quietly xtreg ln_wage c.age##c.age, fe" for all the industries seperately and then see the marginal effects of age seperately.
                Many thanks indeed for your suggestions. 😊

                Comment


                • #9
                  Meng:
                  with your code -i.ind_code- is interacted with -age- and its square term instead of considering -i.ind_code- as an independent predictor.
                  They are two different codes that give back slightly different -margins- results.
                  It's up to you to decide which way to go given the goal of your research.
                  Kind regards,
                  Carlo
                  (StataNow 18.5)

                  Comment

                  Working...
                  X