Announcement

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

  • Fixed effect regression with some conditions

    Hello Stata experts,

    I am using the following data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(IVOL DUM FirmID)
    
       -.012846745 0 1
        .003010829 0 1
        -.01994809 1 1
         -.0102941 1 1
       -.016133793 1 1
        .003362658 1 1
        .010100462 0 1
       -.005357542 1 1
         .01985686 1 1
        -.02125267 0 1
       -.013251625 0 2
       -.011405208 0 2
        .005397665 0 2
       -.016385317 0 2
        -.02082585 0 2
        .004482734 0 2
       -.008927207 0 2
    -1.3124484e-06 1 2
       -.002564483 0 2
        -.02041283 0 2
       -.021711256 0 3
        .019218445 0 3
        -.01553059 0 3
       -.001207368 0 3
       -.020130247 1 3
       -.015929932 1 3
       -.012757675 1 3
        .002185969 1 3
        -.01975868 1 3
       -.010493663 1 3
       -.021384994 1 4
       -.016919166 1 4
       -.016161574 0 4
        .006752182 0 4
       -.009183821 0 4
       -.022988187 0 4
        .005700997 0 4
       -.016790474 0 4
       -.010007303 1 4
       -.013928052 0 4
      
    end

    to run this regression:
    Code:
    xtreg IVOL DUM, fe
    where DUM is basically a dummy variable and FirmID is the panel firm ID.

    So, the observations of variable DUM ==1 vary from one firm to another. We have firms with no observations for DUM==1 and other firms with greater observations of DUM==1. So what I would like to do is to run the above regression if the observations of variable DUM==1 is greater than the average or mean of observations for DUM==1 for all firms. Is there a way to get to the mean of observations for DUM==1 for all firms without doing something like:

    Code:
    by FirmID: sum DUM if DUM==1
    because this will calculate the observations for DUM==1 for each firm and then I will have to calculate the mean observations for all firms manually.
    Once we do this I need to run the above regression for firms with observations of DUM==1 greater than the mean observations for all firms.

    Thank you

  • #2
    Saad:
    are you looking for something along the following lines?
    Code:
    bysort FirmID: egen flag_1=mean(DUM)
    egen flag_2=mean(DUM)
    list FirmID DUM IVOL flag_1 flag_2
    
         +--------------------------------------------+
         | FirmID   DUM        IVOL   flag_1   flag_2 |
         |--------------------------------------------|
      1. |      1     0   -.0128467       .6       .4 |
      2. |      1     0    .0030108       .6       .4 |
      3. |      1     1   -.0199481       .6       .4 |
      4. |      1     1   -.0102941       .6       .4 |
      5. |      1     1   -.0161338       .6       .4 |
         |--------------------------------------------|
      6. |      1     1    .0033627       .6       .4 |
      7. |      1     0    .0101005       .6       .4 |
      8. |      1     1   -.0053575       .6       .4 |
      9. |      1     1    .0198569       .6       .4 |
     10. |      1     0   -.0212527       .6       .4 |
         |--------------------------------------------|
     11. |      2     0   -.0132516       .1       .4 |
     12. |      2     0   -.0114052       .1       .4 |
     13. |      2     0    .0053977       .1       .4 |
     14. |      2     0   -.0163853       .1       .4 |
     15. |      2     0   -.0208258       .1       .4 |
         |--------------------------------------------|
     16. |      2     0    .0044827       .1       .4 |
     17. |      2     0   -.0089272       .1       .4 |
     18. |      2     1   -1.31e-06       .1       .4 |
     19. |      2     0   -.0025645       .1       .4 |
     20. |      2     0   -.0204128       .1       .4 |
         |--------------------------------------------|
     21. |      3     0   -.0217113       .6       .4 |
     22. |      3     0    .0192184       .6       .4 |
     23. |      3     0   -.0155306       .6       .4 |
     24. |      3     0   -.0012074       .6       .4 |
     25. |      3     1   -.0201302       .6       .4 |
         |--------------------------------------------|
     26. |      3     1   -.0159299       .6       .4 |
     27. |      3     1   -.0127577       .6       .4 |
     28. |      3     1     .002186       .6       .4 |
     29. |      3     1   -.0197587       .6       .4 |
     30. |      3     1   -.0104937       .6       .4 |
         |--------------------------------------------|
     31. |      4     1    -.021385       .3       .4 |
     32. |      4     1   -.0169192       .3       .4 |
     33. |      4     0   -.0161616       .3       .4 |
     34. |      4     0    .0067522       .3       .4 |
     35. |      4     0   -.0091838       .3       .4 |
         |--------------------------------------------|
     36. |      4     0   -.0229882       .3       .4 |
     37. |      4     0     .005701       .3       .4 |
     38. |      4     0   -.0167905       .3       .4 |
     39. |      4     1   -.0100073       .3       .4 |
     40. |      4     0   -.0139281       .3       .4 |
         +--------------------------------------------+
    
    xtreg IVOL DUM if flag_1>flag_2, fe
    
    Fixed-effects (within) regression               Number of obs     =         20
    Group variable: FirmID                          Number of groups  =          2
    
    R-sq:                                           Obs per group:
         within  = 0.0214                                         min =         10
         between =      .                                         avg =       10.0
         overall = 0.0207                                         max =         10
    
                                                    F(1,17)           =       0.37
    corr(u_i, Xb)  = 0.0000                         Prob > F          =     0.5504
    
    ------------------------------------------------------------------------------
            IVOL |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             DUM |  -.0037558   .0061638    -0.61   0.550    -.0167603    .0092486
           _cons |  -.0050274   .0047744    -1.05   0.307    -.0151006    .0050458
    -------------+----------------------------------------------------------------
         sigma_u |  .00329603
         sigma_e |  .01350418
             rho |  .05622311   (fraction of variance due to u_i)
    ------------------------------------------------------------------------------
    F test that all u_i=0: F(1, 17) = 0.60                       Prob > F = 0.4508
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X