Announcement

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

  • Foreach and estimates

    Dear Stata users,

    I am coming with several questions concerning looping.

    Let's imagine I have 4 dummies: a, b, c and d.
    I would like to run the same regression on each of the dummies (2 regression per dummy) and store the estimates (N, R2, Adj R2 , both fixed effects (i.e. adding the line indicating the fixed effects) & dropping the constant + the other interaction terms) before using esttab.

    I tried to use group() by combining the different dummies and of course it did not work.
    How can we make levelsof go through all the different dummies to do the same regression and to store all the results into one single table?

    Code:
    levelsof a, local(groups)
    foreach group of local groups {
    reghdfe Sales i.var1##i.var2 var3 var4 if a==`group', absorb(year firm) vce(cluster firm)
    global  `group'1 =  _b[i.var1##i.var2]+_b[.var2]+_b[var3] +_b[var4]
       eststo `group'1
       global storelist = "${storelist} `yvar'1”
    }
    esttab ${storelist}
    Thanks in advance if you can help me,
    Eugene

  • #2
    Why "of course it did not work"?

    Seems to me that if you do

    Code:
    egen groups = group(a b c d)
    levelsof groups
    
    .... etc
    it should work.

    Comment


    • #3
      Hello Joro,

      Thank you for your answer.

      I tried egen groups = group (a b c d) and I got the following:

      Code:
      egen groups = group (a b c)
      levelsof groups, local(groups)
      foreach group of local groups {
      reghdfe outcome i.var1##i.var2 var3 var4 if groups==`group', absorb(year firmFE) vce(cluster firmFE)
      global  `group'1=  _b[i.var1##i.var2]+_b[i.var2]+_b[var3] +_b[var4]
         eststo `group'1
         global storelist = "${storelist} `yvar'1”
      }
      
      HDFE Linear regression                            Number of obs   =         24
      Absorbing 2 HDFE groups                           F(   2,      5) =       5.49
      Statistics robust to heteroskedasticity           Prob > F        =     0.0548
                                                        R-squared       =     0.6487
                                                        Adj R-squared   =     0.2656
                                                        Within R-sq.    =     0.0037
      Number of clusters (firmFE)  =          6         Root MSE        =     2.0152
      
                                       (Std. Err. adjusted for 6 clusters in firmFE)
      ------------------------------------------------------------------------------
                   |               Robust
           outcome |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
            0.var1 |          0  (omitted)
            1.var2 |          0  (omitted)
                   |
         var1#var2 |
              0 1  |          0  (omitted)
                   |
              var3 |   2.081938   6.395459     0.33   0.758    -14.35811    18.52199
              var4 |   .2415646   .0841595     2.87   0.035     .0252258    .4579035
             _cons |   8.837795   5.297387     1.67   0.156    -4.779571    22.45516
      ------------------------------------------------------------------------------
      
      Absorbed degrees of freedom:
      -----------------------------------------------------+
       Absorbed FE | Categories  - Redundant  = Num. Coefs |
      -------------+---------------------------------------|
              year |         5           0           5     |
            firmFE |         6           6           0    *|
      -----------------------------------------------------+
      * = FE nested within cluster; treated as redundant for DoF computation
      11 invalid name
      r(198);
      
      end of do-file
      
      r(198);
      
      esttab ${storelist}
      
      ----------------------------
                            (1)   
                        outcome   
      ----------------------------
      0.var1                  0   
                            (.)   
      
      0.var2                  0   
                            (.)   
      
      1.var2                  0   
                            (.)   
      
      0.var1#0.v~2            0   
                            (.)   
      
      0.var1#1.v~2            0   
                            (.)   
      
      var3                2.082   
                         (0.33)   
      
      var4                0.242*  
                         (2.87)   
      
      _cons               8.838   
                         (1.67)   
      ----------------------------
      N                      24   
      ----------------------------
      t statistics in parentheses
      * p<0.05, ** p<0.01, *** p<0.001
      I should have 6 columns since foreach is supposed to do the same steps in each dummies (regression for 0 & 1). Did I do something wrong?
      How is it possible to keep out the 0 of the esttab and to add R2 and the time /firm fixed effects?

      Thank you again for your answer,
      Best regards,
      Eugene



      Comment


      • #4
        In your code

        Code:
        egen groups = group (a b c)  
        levelsof groups, local(groups)  
        
        foreach group of local groups {      
            reghdfe outcome i.var1##i.var2 var3 var4 if groups==`group', absorb(year firmFE) vce(cluster firmFE)    
            global  `group'1=  _b[i.var1##i.var2]+_b[i.var2]+_b[var3] +_b[var4]    
            eststo `group'1    
            global storelist = "${storelist} `yvar'1”  
        }
        you define a global `group'1 but don't use it later. For your purposes defining

        Code:
        local `group'1  ...
        would work as well, I suspect, but you then need refer to it as ``group'1'' in later code

        FWIW, note that

        Code:
        egen groups = group(a b c)
        su groups, meanonly
        forval group = 1/`r(max)' {
        saves a line and will be a tiny bit faster.

        Also, note that the double quotation marks need to be " not ”,

        Comment


        • #5
          Dear Nick,

          Thank you very much for your answer.
          Since I am new in Stata, I have to say that I am not sure I understood what you meant, I got the following:

          Code:
          egen groups = group (a b c)  
          levelsof groups, local(groups)  
          foreach group of local `group'1 {      
              reghdfe outcome i.var1##i.var2 var3 var4 if groups==`group', absorb(year firmFE) vce(cluster firmFE)    
              global  `group'1=  _b[i.var1##i.var2]+_b[i.var2]+_b[var3] +_b[var4]    
              eststo `group'1    
              global storelist = "${storelist} `yvar'1" 
          }
          At the end, I would like to run the regression through my multiple dummies and to store all the results before creating a table.

          Also, for an additional dummy that has to go through the previous regression, I need also to do the following action, I had already this code:

          This dummy ("w") is supposed to equal to 1 if 3 or more variables equal to 1. Otherwise it equals to 0.
          I know that my code is not really correct since I got recently into looping and so on.
          But is it possible to include this as a loop?

          Code:
          levelsof a, local(levels)
          gen w = 0
          foreach i of local levels {
           replace w = 1 if `i'== 1
           }
          Thank you again Nick for responding,
          Eugene

          Comment


          • #6
            Did you write this yourself or are you trying to modify code you found somewhere? If the latter, what is the original?

            I don't use reghdfe or esttsto myself, but these are popular community-contributed command. (As signalled in FAQ Advice #12 you are asked to explain where they come from.)

            You introduced another bug in #5. What I suspect is closer to what you want is


            Code:
            egen groups = group(a b c)  
            levelsof groups, local(groups)  
            
            foreach group of local groups {      
                reghdfe outcome i.var1##i.var2 var3 var4 if groups==`group', absorb(year firmFE) vce(cluster firmFE)    
                local  this =  _b[i.var1##i.var2]+_b[i.var2]+_b[var3] +_b[var4]    
                eststo `this'     
                global storelist = "${storelist} `yvar'1"  
            }
            This is taken out of some larger context in which (presumably) the global storelist and the local yvar are defined,

            I'd fix one thing at a time. Your second block of code boils down to one line

            Code:
              
              gen w = a == 1


            which is some distance from I think what you want.
            egen has an anycount() function which seems closer to what you want.



            Comment


            • #7
              Dear Nick,

              Thank you for your answer.
              I am actually using reghdfe from SSC in Stata 16.0.

              1) Before using levels of with foreach, I was writing for each different dummies two regressions:

              Code:
              reghdfe outcome i.var1##i.var2 var3 var4 if dummy_1==1, absorb(year ghgrpid) vce(cluster ghgrpid)
              store estimates reg1
              reghdfe outcome i.var1##i.var2 var3 var4 if dummy_1==0, absorb(year ghgrpid) vce(cluster ghgrpid)
              store estimates reg2
              This was not efficient and I finally tried to use levelsof and foreach as I found here: https://www.stata.com/support/faqs/d...-with-foreach/
              My first attempt was as follows:

              Code:
              levelsof dummy_1, local(groups)
              foreach group of local groups {
              reghdfe outcome i.var1##i.var2 var3 var4 if dummy_1==`group', absorb(year ghgrpid) vce(cluster ghgrpid)
              }
              The code gave me the results for two regressions (1 & 0) for one dummy.
              My main concern was to shorten my lines of code because I have up to 6 dummies that run through the same regression (instead of having six times the same code).
              In addition to that, I wanted to find a way how to save the results for each dummies (again for 0 & 1) so that I can create a table with all the results.

              2) Concerning the next part and your code you provided me

              Code:
               
               gen w = a == 1
              I initially had this but then I figured out that it was not what I needed.
              I tried the anycount() function as follows and I got what I wanted ! Thank you very much !


              Best regards,
              Eugene

              Comment

              Working...
              X