Announcement

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

  • capturing unit and time specific coefficients

    Hello.
    I am using Stata 16. I have an unbalanced panel data set with firm (gvkey) observations over time (fiscal_quarter). I'm running the following regression

    HTML Code:
    reg DV x1 x2 x3 x4 c.x1#i.gvkey c.x1#i.fiscal_quarter c.x2#i.gvkey c.x2#i.fiscal_quarter c.x3#i.gvkey c.x3#i.fiscal_quarter c.x4#i.gvkey c.x4#i.fiscal_quarter, beta nocons
    and would like to generate new variables (one assoicated with x1, x2, x3, x4) akin to

    HTML Code:
    gen x1total = _b[x1] + _b[c.x1#gvkey] + _b[c.x1#fiscal_quarter]
    for x1. In other words, I would like to capture the coefficient for x1, which can be done with

    HTML Code:
    gen x1coeff = _b[x1]
    and will be the same for each observation.

    Next, I need to capture the coefficient for for the interaction of x1 with gvkey (my firm identifier), which will be the same for all observations within a firm but vary across firms, and capture the coefficient for the interaction of x1 with fiscal_quarter (my time variable), which will be the same for each time period across firms, but varies within firms.

    I have searched extensively for a code on how to capture these coefficients to no prevail. I looked into using statsby, but it appears I would have to run the regression by groups (i.e., firms and then time periods), which is not what I want.

    Any suggestions are greatly appreciated.
    Best,
    Annette

  • #2
    If I understand you correctly, you just want to loop.

    Next, I need to capture the coefficient for for the interaction of x1 with gvkey (my firm identifier), which will be the same for all observations within a firm but vary across firms
    Code:
    levelsof gvkey, local(firms)
    gen wanted= .
    foreach num of numlist `firms'{
    replace wanted= _b[c.x1#`num'.gvkey] if gvkey==`num'
    }
    Same logic for the other variables. Please note our preference for reproducible examples.

    Comment


    • #3
      Hi, Andrew,

      Thank you so much. This works perfectly and was easily replicated for the remaining coefficients. I'm also interested in capturing standardized coefficients and ran the following model:

      Code:
      reg DV x1 x2 x3 x4 c.x1#i.gvkey c.x1#i.fiscal_quarter c.x2#i.gvkey c.x2#i.fiscal_quarter c.x3#i.gvkey c.x3#i.fiscal_quarter c.x4#i.gvkey c.x4#i.fiscal_quarter, beta
      Next, I used

      Code:
      listcoef
      and attempted to capture the standardized coefficient of x1 with

      Code:
      gen x1_stand_coeff = _bStdXY[x1]
      which resulted in the error message: _bStdXY not found

      I have not been able to find a command that accesses standardized coefficients similar to the way _b accesses "regular" coefficients. Do you have any suggestions?

      Again, thank you very much!

      Annette

      Comment


      • #4
        One method that I use is to call esttab from SSC by Benn Jann that creates a matrix with the beta coefficients.

        Code:
        ssc install esttab
        reg DV x1 x2 x3 x4...
        esttab, beta
        mat l e(beta)
        You can now obtain the coefficients from this matrix. Let me know if you have any difficulties.

        Comment


        • #5
          Hi, Andrew,

          Again, thank you for your support.

          I followed your suggestion and wrote code to obtain the standardized coefficients from the matrix by recalling scalars based on their position as identified by row and column positions:

          Code:
          esttab, beta
          mat l e(beta)
          
          gen x1_time=.
          levelsof fiscal_quarter, local(quarters)
          foreach num of numlist `quarters' {
          replace x1_time= beta[1,colnumb(beta,"`num'.fiscal_quarter#c.x1 if fiscal_quarter==`num'")]
          }
          Unfortunately, I receive

          (0 real changes made)
          (0 real changes made)
          (0 real changes made)
          (0 real changes made)
          (0 real changes made)
          (0 real changes made)
          (0 real changes made)
          ...

          I also noted that while
          Code:
          esttab, beta
          returns all beta coefficients, once I write them to matrix "beta", all of the coefficients from the interaction terms (e.g., c.x1#i.fiscal_quarter) are listed as .z .

          To evaluate whether the above listed code didn't result in any changes made because the interaction terms are missing in the matrix of standardized coefficients, I also ran the code and gathered "regular" coefficients. The matrix shows regular interaction coefficients, but I'm not able to capture them in my new variable and receive the same error message (0 real changes made).

          Thank you very much for your time.

          Annette

          Comment


          • #6
            There is an issue with the -beta- option of esttab and factor variables which I had not noticed. Hopefully Ben Jann will view this thread and address it in a future update. esttab does not calculate the coefficients of factor variable interactions but instead assigns them missing values similar to the intercept.

            Code:
            webuse grunfeld,clear
            regress invest mvalue i.company##c.kstock, beta
            esttab, beta
            mat list e(beta)
            Results in:

            Code:
            . regress invest mvalue i.company##c.kstock, beta
            
                  Source |       SS           df       MS      Number of obs   =       200
            -------------+----------------------------------   F(20, 179)      =    225.72
                   Model |  9002974.32        20  450148.716   Prob > F        =    0.0000
                Residual |  356969.596       179  1994.24355   R-squared       =    0.9619
            -------------+----------------------------------   Adj R-squared   =    0.9576
                   Total |  9359943.92       199  47034.8941   Root MSE        =    44.657
            
            ----------------------------------------------------------------------------------
                      invest |      Coef.   Std. Err.      t    P>|t|                     Beta
            -----------------+----------------------------------------------------------------
                      mvalue |   .1057073   .0103627    10.20   0.000                 .6406865
                             |
                     company |
                          2  |   178.1454   33.57003     5.31   0.000                 .2470439
                          3  |  -61.01103   32.20552    -1.89   0.060                -.0846073
                          4  |   72.51596   39.98803     1.81   0.071                 .1005618
                          5  |    123.015   50.88138     2.42   0.017                 .1705917
                          6  |   90.24424   46.25708     1.95   0.053                 .1251466
                          7  |   89.24089   49.96549     1.79   0.076                 .1237552
                          8  |   72.46716   42.59822     1.70   0.091                 .1004942
                          9  |   83.09874     53.648     1.55   0.123                 .1152376
                         10  |   89.15305   47.73111     1.87   0.063                 .1236334
                             |
                      kstock |   .3798827   .0174874    21.72   0.000                 .5274191
                             |
            company#c.kstock |
                          2  |    .028188   .0673826     0.42   0.676                 .0131362
                          3  |  -.2436443   .0442144    -5.51   0.000                -.1607756
                          4  |  -.0767067   .0934675    -0.82   0.413                -.0177305
                          5  |  -.3578027   .0559295    -6.40   0.000                -.2605544
                          6  |  -.2148316   .1549131    -1.39   0.167                -.0373009
                          7  |  -.2562131   .0827691    -3.10   0.002                 -.121073
                          8  |  -.4273763   .1666695    -2.56   0.011                 -.063353
                          9  |  -.3129159   .1105414    -2.83   0.005                -.1358529
                         10  |   .1019809   3.237561     0.03   0.975                 .0009578
                             |
                       _cons |  -96.42841   43.24391    -2.23   0.027                        .
            ----------------------------------------------------------------------------------
            
            
            
            . mat l e(beta)
            
            e(beta)[1,23]
                                      1b.           2.           3.           4.           5.           6.           7.           8.           9.          10.
                     mvalue      company      company      company      company      company      company      company      company      company      company
            y1    .64068651            0    .24704393   -.08460733    .10056184    .17059168    .12514662    .12375522    .10049417    .11523756     .1236334
            
                              1b.company#   2.company#   3.company#   4.company#   5.company#   6.company#   7.company#   8.company#   9.company#  10.company#
                     kstock    co.kstock     c.kstock     c.kstock     c.kstock     c.kstock     c.kstock     c.kstock     c.kstock     c.kstock     c.kstock
            y1    .52741909           .z           .z           .z           .z           .z           .z           .z           .z           .z           .z
            
                            
                      _cons
            y1           .z
            The workaround is to resort to the xi prefix to create the interactions. Make sure that you are obtaining the same coefficients as the original regression. Here is the code that estimates the regression in my example.

            Code:
            xi: reg invest mvalue i.company*kstock, beta
            esttab, beta
            mat list e(beta)
            Res.:

            Code:
            . xi: reg invest mvalue i.company*kstock, beta
            i.company         _Icompany_1-10      (naturally coded; _Icompany_1 omitted)
            i.comp~y*kstock   _IcomXksto_#        (coded as above)
            
                  Source |       SS           df       MS      Number of obs   =       200
            -------------+----------------------------------   F(20, 179)      =    225.72
                   Model |  9002974.32        20  450148.716   Prob > F        =    0.0000
                Residual |  356969.596       179  1994.24355   R-squared       =    0.9619
            -------------+----------------------------------   Adj R-squared   =    0.9576
                   Total |  9359943.92       199  47034.8941   Root MSE        =    44.657
            
            -------------------------------------------------------------------------------
                   invest |      Coef.   Std. Err.      t    P>|t|                     Beta
            --------------+----------------------------------------------------------------
                   mvalue |   .1057073   .0103627    10.20   0.000                 .6406865
              _Icompany_2 |   178.1454   33.57003     5.31   0.000                 .2470439
              _Icompany_3 |  -61.01103   32.20552    -1.89   0.060                -.0846073
              _Icompany_4 |   72.51596   39.98803     1.81   0.071                 .1005618
              _Icompany_5 |    123.015   50.88138     2.42   0.017                 .1705917
              _Icompany_6 |   90.24424   46.25708     1.95   0.053                 .1251466
              _Icompany_7 |   89.24089   49.96549     1.79   0.076                 .1237552
              _Icompany_8 |   72.46716   42.59822     1.70   0.091                 .1004942
              _Icompany_9 |   83.09874     53.648     1.55   0.123                 .1152376
             _Icompany_10 |   89.15305   47.73111     1.87   0.063                 .1236334
                   kstock |   .3798827   .0174874    21.72   0.000                 .5274191
             _IcomXksto_2 |    .028188   .0673826     0.42   0.676                 .0131362
             _IcomXksto_3 |  -.2436443   .0442144    -5.51   0.000                -.1607756
             _IcomXksto_4 |  -.0767067   .0934675    -0.82   0.413                -.0177305
             _IcomXksto_5 |  -.3578027   .0559295    -6.40   0.000                -.2605544
             _IcomXksto_6 |  -.2148316   .1549131    -1.39   0.167                -.0373009
             _IcomXksto_7 |  -.2562131   .0827691    -3.10   0.002                 -.121073
             _IcomXksto_8 |  -.4273763   .1666695    -2.56   0.011                 -.063353
             _IcomXksto_9 |  -.3129159   .1105414    -2.83   0.005                -.1358529
            _IcomXksto_10 |   .1019809   3.237561     0.03   0.975                 .0009578
                    _cons |  -96.42841   43.24391    -2.23   0.027                        .
            -------------------------------------------------------------------------------
            
            
            
            . mat list e(beta)
            
            e(beta)[1,21]
                      mvalue   _Icompany_2   _Icompany_3   _Icompany_4   _Icompany_5   _Icompany_6   _Icompany_7   _Icompany_8   _Icompany_9  _Icompany_10
            y1     .64068651     .24704393    -.08460733     .10056184     .17059168     .12514662     .12375522     .10049417     .11523756      .1236334
            
                      kstock  _IcomXksto_2  _IcomXksto_3  _IcomXksto_4  _IcomXksto_5  _IcomXksto_6  _IcomXksto_7  _IcomXksto_8  _IcomXksto_9  _IcomXkst~10
            y1     .52741909     .01313622    -.16077563    -.01773047    -.26055436     -.0373009    -.12107297    -.06335298    -.13585291     .00095784
            
                       _cons
            y1            .z
            Now you have the coefficients - just named differently.

            Comment


            • #7
              Hi, Andrew,

              One final THANK YOU for your assistance. Below is the code that allowed me to capture the standardized coefficients:

              Code:
              xi: reg DV i.gvkey|x1 i.fiscal_quarter|x1, nocons beta
              
              esttab, beta
              mat beta=e(beta) //create matrix "beta" which contains standardized coefficients
              mat li beta
              
              gen x1_firm=. 
              levelsof gvkey, local(firms)
              foreach num of numlist `firms' {
              replace x1_firm= beta[1,colnumb(beta,"_IgvkXx1_`num'")] if gvkey==`num'
              }
              Best,
              Annette

              Comment

              Working...
              X