Announcement

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

  • #16
    Code:
    xtset id time
    xtreg salary i.treatment#i.time i.time, fe cluster(id) // NOTE #, NOT ##
    should give you a model that will support estimation of -margins-. If it does not, post back showing the exact regression and margins command(s) used, and the exact, complete results you got from Stata.

    Comment


    • #17
      Hi again,

      Thanks for your answer. When I do not apply noestimcheck, the margins are "not estimable". But when I use noestimcheck, I get margins, including from the first timeperiod, but not the last (given that the regression omits the last timeperiod). However, I reckon that the noestimcheck is ill-advised?

      Here you have my dataset:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id time treatment salary)
       1  98 1 20000
       1  99 1 20000
       1 100 1 15000
       1 101 1 15000
       1 102 1 15000
       1 103 1 15000
       2  98 1 20000
       2  99 1 20000
       2 100 1 17000
       2 101 1 17000
       2 102 1 17000
       2 103 1 17000
       3  98 1 21000
       3  99 1 21000
       3 100 1 24000
       3 101 1 24000
       3 102 1 24000
       3 103 1 24000
       4  98 1 19000
       4  99 1 19000
       4 100 1 15000
       4 101 1 15000
       4 102 1 15000
       4 103 1 15000
       5  98 1 20000
       5  99 1 20000
       5 100 1 21000
       5 101 1 22000
       5 102 1 22000
       5 103 1 22000
       6  98 0 24000
       6  99 0 24000
       6 100 0 24000
       6 101 0 24000
       6 102 0 24000
       6 103 0 24000
       7  98 0 20000
       7  99 0 20000
       7 100 0 20000
       7 101 0 20000
       7 102 0 20000
       7 103 0 20000
       8  98 0 20000
       8  99 0 20000
       8 100 0 20000
       8 101 0 20000
       8 102 0 20000
       8 103 0 20000
       9  98 0 21000
       9  99 0 21000
       9 100 0 21000
       9 101 0 21000
       9 102 0 21000
       9 103 0 21000
      10  98 0 21000
      10  99 0 21000
      10 100 0 20000
      10 101 0 20000
      10 102 0 20000
      10 103 0 20000
      12  98 1 20000
      12  99 1 20000
      12 100 1 20000
      12 101 1 20000
      12 102 1 19000
      12 103 1 19000
      13  98 0 23000
      13  99 0 23000
      13 100 0 23000
      13 101 0 23000
      13 102 0 23000
      13 103 0 22000
      14  98 0 20000
      14  99 0 22000
      14 100 0 20000
      14 101 0 20000
      14 102 0 20000
      14 103 0 20000
      end
      And here you have my code and results:
      Code:
      . xtset id time
      
      Panel variable: id (strongly balanced)
       Time variable: time, 98 to 103
               Delta: 1 unit
      
      .
      . xtreg salary i.treatment#i.time i.time, fe cluster(id)
      note: 1.treatment#103.time omitted because of collinearity.
      
      Fixed-effects (within) regression               Number of obs     =         78
      Group variable: id                              Number of groups  =         13
      
      R-squared:                                      Obs per group:
           Within  = 0.1642                                         min =          6
           Between = 0.2194                                         avg =        6.0
           Overall = 0.0026                                         max =          6
      
                                                      F(6,12)           =          .
      corr(u_i, Xb) = -0.1448                         Prob > F          =          .
      
                                            (Std. err. adjusted for 13 clusters in id)
      --------------------------------------------------------------------------------
                     |               Robust
              salary | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
      ---------------+----------------------------------------------------------------
      treatment#time |
              1  98  |   1047.619   1371.415     0.76   0.460    -1940.438    4035.676
              1  99  |   761.9048   1392.428     0.55   0.594    -2271.936    3795.745
              1 100  |  -142.8571   301.5741    -0.47   0.644    -799.9306    514.2163
              1 101  |   23.80952    224.942     0.11   0.917    -466.2969    513.9159
              1 102  |  -142.8571   147.5766    -0.97   0.352     -464.399    178.6847
              1 103  |          0  (omitted)
                     |
                time |
                 99  |   285.7143   295.1533     0.97   0.352    -357.3694     928.798
                100  |  -142.8571   147.5766    -0.97   0.352     -464.399    178.6847
                101  |  -142.8571   147.5766    -0.97   0.352     -464.399    178.6847
                102  |  -142.8571   147.5766    -0.97   0.352     -464.399    178.6847
                103  |  -285.7143   190.5206    -1.50   0.160     -700.823    129.3945
                     |
               _cons |   20208.79   259.4511    77.89   0.000      19643.5    20774.09
      ---------------+----------------------------------------------------------------
             sigma_u |   2364.618
             sigma_e |  1171.6652
                 rho |  .80287816   (fraction of variance due to u_i)
      --------------------------------------------------------------------------------
      
      .
      . margins treatment, at(time =(98 99 100 101 102))
      
      Adjusted predictions                                        Number of obs = 78
      Model VCE: Robust
      
      Expression: Linear prediction, predict()
      1._at: time =  98
      2._at: time =  99
      3._at: time = 100
      4._at: time = 101
      5._at: time = 102
      
      -------------------------------------------------------------------------------
                    |            Delta-method
                    |     Margin   std. err.      z    P>|z|     [95% conf. interval]
      --------------+----------------------------------------------------------------
      _at#treatment |
               1 0  |          .  (not estimable)
               1 1  |          .  (not estimable)
               2 0  |          .  (not estimable)
               2 1  |          .  (not estimable)
               3 0  |          .  (not estimable)
               3 1  |          .  (not estimable)
               4 0  |          .  (not estimable)
               4 1  |          .  (not estimable)
               5 0  |          .  (not estimable)
               5 1  |          .  (not estimable)
      -------------------------------------------------------------------------------
      Last edited by Gustav Egede Hansen; 25 Apr 2023, 01:01.

      Comment


      • #18
        Yes, it is inadvisable to ignore the -noestimcheck- here.

        What you need to do is create a new variable, let's call it treated, that takes on the value 1 in observations that are both in the treatment group and at a time when the treatment has been administered, and 0 elsewhere. Then you do:
        Code:
        xtreg salary i.treated i.time, fe
        margins treated, at(time =(98 99 100 101 102))

        Comment


        • #19
          Hi again,
          Thanks for your reply. Everything is no running; however, something strange appears to be happening. First, here is a dataset with the new variable.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(id time treatment salary treated)
           1  98 1 20000 0
           1  99 1 20000 0
           1 100 1 15000 0
           1 101 1 15000 1
           1 102 1 15000 1
           1 103 1 15000 1
           2  98 1 20000 0
           2  99 1 20000 0
           2 100 1 17000 0
           2 101 1 17000 1
           2 102 1 17000 1
           2 103 1 17000 1
           3  98 1 21000 0
           3  99 1 21000 0
           3 100 1 24000 0
           3 101 1 24000 1
           3 102 1 24000 1
           3 103 1 24000 1
           4  98 1 19000 0
           4  99 1 19000 0
           4 100 1 15000 0
           4 101 1 15000 1
           4 102 1 15000 1
           4 103 1 15000 1
           5  98 1 20000 0
           5  99 1 20000 0
           5 100 1 21000 0
           5 101 1 22000 1
           5 102 1 22000 1
           5 103 1 22000 1
           6  98 0 24000 0
           6  99 0 24000 0
           6 100 0 24000 0
           6 101 0 24000 0
           6 102 0 24000 0
           6 103 0 24000 0
           7  98 0 20000 0
           7  99 0 20000 0
           7 100 0 20000 0
           7 101 0 20000 0
           7 102 0 20000 0
           7 103 0 20000 0
           8  98 0 20000 0
           8  99 0 20000 0
           8 100 0 20000 0
           8 101 0 20000 0
           8 102 0 20000 0
           8 103 0 20000 0
           9  98 0 21000 0
           9  99 0 21000 0
           9 100 0 21000 0
           9 101 0 21000 0
           9 102 0 21000 0
           9 103 0 21000 0
          10  98 0 21000 0
          10  99 0 21000 0
          10 100 0 20000 0
          10 101 0 20000 0
          10 102 0 20000 0
          10 103 0 20000 0
          12  98 1 20000 0
          12  99 1 20000 0
          12 100 1 20000 0
          12 101 1 20000 1
          12 102 1 19000 1
          12 103 1 19000 1
          13  98 0 23000 0
          13  99 0 23000 0
          13 100 0 23000 0
          13 101 0 23000 0
          13 102 0 23000 0
          13 103 0 22000 0
          14  98 0 20000 0
          14  99 0 22000 0
          14 100 0 20000 0
          14 101 0 20000 0
          14 102 0 20000 0
          14 103 0 20000 0
          end
          As you said, I am now able to calculate margins after running the regression:
          Code:
          xtset id time
          xtreg salary i.treated i.time, fe
          margins treated, at(time =(98 99 100 101 102))
          Hereafter, I ran a test where I changed the salary for the treatmentgroup after the intervention (so that the treatment effect should be bigger), but very strangely, the salary also changes in the pre-period.

          Code:
          gen salary_new = salary
          replace salary_new = salary_new*2 if treated == 1
          
          xtset id time
          xtreg salary_new i.treated i.time, fe
          margins treated, at(time =(98 99 100 101 102))
          For instance, in timeperiode 98 the average new_salary should be 20.000 for the treatmentgroup (sum salary_new if treatment == 1 & time == 98) but according to margins it appears to be 38819.29.

          Do you have any idea what is going on?

          Best
          Gustav

          Comment


          • #20
            I cannot replicate your problem in the example data:

            Code:
            . xtreg salary i.treated i.time, fe
            
            Fixed-effects (within) regression               Number of obs     =         78
            Group variable: id                              Number of groups  =         13
            
            R-squared:                                      Obs per group:
                 Within  = 0.1360                                         min =          6
                 Between = 0.2194                                         avg =        6.0
                 Overall = 0.0893                                         max =          6
            
                                                            F(6, 59)          =       1.55
            corr(u_i, Xb) = 0.1448                          Prob > F          =     0.1789
            
            ------------------------------------------------------------------------------
                  salary | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
               1.treated |  -595.2381   522.4779    -1.14   0.259    -1640.714    450.2379
                         |
                    time |
                     99  |   153.8462   451.1385     0.34   0.734    -748.8799    1056.572
                    100  |  -692.3077   451.1385    -1.53   0.130    -1595.034    210.4183
                    101  |  -340.6593    511.543    -0.67   0.508    -1364.254    682.9358
                    102  |  -417.5824    511.543    -0.82   0.418    -1441.178    606.0127
                    103  |  -494.5055    511.543    -0.97   0.338    -1518.101    529.0896
                         |
                   _cons |   20692.31   319.0031    64.87   0.000     20053.98    21330.63
            -------------+----------------------------------------------------------------
                 sigma_u |  2230.8089
                 sigma_e |   1150.182
                     rho |  .78999382   (fraction of variance due to u_i)
            ------------------------------------------------------------------------------
            F test that all u_i=0: F(12, 59) = 20.64                     Prob > F = 0.0000
            
            . margins treated, at(time =(98 99 100 101 102))
            
            Adjusted predictions                                        Number of obs = 78
            Model VCE: Conventional
            
            Expression: Linear prediction, predict()
            1._at: time =  98
            2._at: time =  99
            3._at: time = 100
            4._at: time = 101
            5._at: time = 102
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   std. err.      z    P>|z|     [95% conf. interval]
            -------------+----------------------------------------------------------------
             _at#treated |
                    1 0  |   20692.31   319.0031    64.87   0.000     20067.07    21317.54
                    1 1  |   20097.07   612.1652    32.83   0.000     18897.25    21296.89
                    2 0  |   20846.15   319.0031    65.35   0.000     20220.92    21471.39
                    2 1  |   20250.92   612.1652    33.08   0.000     19051.09    21450.74
                    3 0  |      20000   319.0031    62.70   0.000     19374.77    20625.23
                    3 1  |   19404.76   612.1652    31.70   0.000     18204.94    20604.58
                    4 0  |   20351.65   399.8915    50.89   0.000     19567.88    21135.42
                    4 1  |   19756.41   425.3374    46.45   0.000     18922.76    20590.06
                    5 0  |   20274.73   399.8915    50.70   0.000     19490.95     21058.5
                    5 1  |   19679.49   425.3374    46.27   0.000     18845.84    20513.13
            ------------------------------------------------------------------------------
            
            
            
            . xtreg salary_new i.treated i.time, fe
            
            Fixed-effects (within) regression               Number of obs     =         78
            Group variable: id                              Number of groups  =         13
            
            R-squared:                                      Obs per group:
                 Within  = 0.9034                                         min =          6
                 Between = 0.5968                                         avg =        6.0
                 Overall = 0.7879                                         max =          6
            
                                                            F(6, 59)          =      92.01
            corr(u_i, Xb) = -0.1697                         Prob > F          =     0.0000
            
            ------------------------------------------------------------------------------
              salary_new | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
               1.treated |   18126.98   1038.707    17.45   0.000     16048.54    20205.43
                         |
                    time |
                     99  |   153.8462   896.8814     0.17   0.864    -1640.809    1948.502
                    100  |  -692.3077   896.8814    -0.77   0.443    -2486.963    1102.348
                    101  |  -289.3773   1016.968    -0.28   0.777    -2324.325    1745.571
                    102  |  -443.2234   1016.968    -0.44   0.665    -2478.172    1591.725
                    103  |  -520.1465   1016.968    -0.51   0.611    -2555.095    1514.802
                         |
                   _cons |   20692.31   634.1909    32.63   0.000     19423.29    21961.32
            -------------+----------------------------------------------------------------
                 sigma_u |  3241.4483
                 sigma_e |  2286.6079
                     rho |  .66772236   (fraction of variance due to u_i)
            ------------------------------------------------------------------------------
            F test that all u_i=0: F(12, 59) = 11.57                     Prob > F = 0.0000
            
            . margins treated, at(time =(98 99 100 101 102))
            
            Adjusted predictions                                        Number of obs = 78
            Model VCE: Conventional
            
            Expression: Linear prediction, predict()
            1._at: time =  98
            2._at: time =  99
            3._at: time = 100
            4._at: time = 101
            5._at: time = 102
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   std. err.      z    P>|z|     [95% conf. interval]
            -------------+----------------------------------------------------------------
             _at#treated |
                    1 0  |   20692.31   634.1909    32.63   0.000     19449.32     21935.3
                    1 1  |   38819.29   1217.009    31.90   0.000        36434    41204.59
                    2 0  |   20846.15   634.1909    32.87   0.000     19603.16    22089.15
                    2 1  |   38973.14   1217.009    32.02   0.000     36587.84    41358.43
                    3 0  |      20000   634.1909    31.54   0.000     18757.01    21242.99
                    3 1  |   38126.98   1217.009    31.33   0.000     35741.69    40512.28
                    4 0  |   20402.93   795.0004    25.66   0.000     18844.76     21961.1
                    4 1  |   38529.91   845.5879    45.57   0.000     36872.59    40187.24
                    5 0  |   20249.08   795.0004    25.47   0.000     18690.91    21807.26
                    5 1  |   38376.07   845.5879    45.38   0.000     36718.75    40033.39
            ------------------------------------------------------------------------------

            Comment


            • #21
              Hi again,

              I just copypasted your results from the example dataset. I have highlighted some of the results that are currious. For the treatmentgroup, new_salary was supposed to be higher only in the posttreatmentperiod (replace salary_new = salary_new*2 if treated == 1). Yet, the margins are also suddenly high in the pretreatmentperiod.

              Code:
              . xtreg salary_new i.treated i.time, fe
              
              Fixed-effects (within) regression               Number of obs     =         78
              Group variable: id                              Number of groups  =         13
              
              R-squared:                                      Obs per group:
                   Within  = 0.9034                                         min =          6
                   Between = 0.5968                                         avg =        6.0
                   Overall = 0.7879                                         max =          6
              
                                                              F(6, 59)          =      92.01
              corr(u_i, Xb) = -0.1697                         Prob > F          =     0.0000
              
              ------------------------------------------------------------------------------
                salary_new | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                 1.treated |   18126.98   1038.707    17.45   0.000     16048.54    20205.43
                           |
                      time |
                       99  |   153.8462   896.8814     0.17   0.864    -1640.809    1948.502
                      100  |  -692.3077   896.8814    -0.77   0.443    -2486.963    1102.348
                      101  |  -289.3773   1016.968    -0.28   0.777    -2324.325    1745.571
                      102  |  -443.2234   1016.968    -0.44   0.665    -2478.172    1591.725
                      103  |  -520.1465   1016.968    -0.51   0.611    -2555.095    1514.802
                           |
                     _cons |   20692.31   634.1909    32.63   0.000     19423.29    21961.32
              -------------+----------------------------------------------------------------
                   sigma_u |  3241.4483
                   sigma_e |  2286.6079
                       rho |  .66772236   (fraction of variance due to u_i)
              ------------------------------------------------------------------------------
              F test that all u_i=0: F(12, 59) = 11.57                     Prob > F = 0.0000
              
              . margins treated, at(time =(98 99 100 101 102))
              
              Adjusted predictions                                        Number of obs = 78
              Model VCE: Conventional
              
              Expression: Linear prediction, predict()
              1._at: time =  98
              2._at: time =  99
              3._at: time = 100
              4._at: time = 101
              5._at: time = 102
              
              ------------------------------------------------------------------------------
                           |            Delta-method
                           |     Margin   std. err.      z    P>|z|     [95% conf. interval]
              -------------+----------------------------------------------------------------
               _at#treated |
                      1 0  |   20692.31   634.1909    32.63   0.000     19449.32     21935.3
                      1 1  |   38819.29   1217.009    31.90   0.000        36434    41204.59
                      2 0  |   20846.15   634.1909    32.87   0.000     19603.16    22089.15
                      2 1  |   38973.14   1217.009    32.02   0.000     36587.84    41358.43
                      3 0  |      20000   634.1909    31.54   0.000     18757.01    21242.99
                      3 1  |   38126.98   1217.009    31.33   0.000     35741.69    40512.28
                      4 0  |   20402.93   795.0004    25.66   0.000     18844.76     21961.1
                      4 1  |   38529.91   845.5879    45.57   0.000     36872.59    40187.24
                      5 0  |   20249.08   795.0004    25.47   0.000     18690.91    21807.26
                      5 1  |   38376.07   845.5879    45.38   0.000     36718.75    40033.39
              ------------------------------------------------------------------------------
              Here is the plain average new_salary for the treatmentgroup over time:

              Code:
              .
              bysort time: sum salary_new if treatment == 1
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 98
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6       20000    632.4555      19000      21000
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 99
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6       20000    632.4555      19000      21000
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 100
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6    18666.67    3614.784      15000      24000
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 101
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6    37666.67    7527.727      30000      48000
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 102
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6    37333.33    7447.595      30000      48000
              
              ------------------------------------------------------------------------------------------------------------------------------------------------
              -> time = 103
              
                  Variable |        Obs        Mean    Std. dev.       Min        Max
              -------------+---------------------------------------------------------
                salary_new |          6    37333.33    7447.595      30000      48000
              To me, it is quite strange that the margins are also high in the pretreatment period. Do you have any idea why or am I completely misunderstanding something?

              Best
              Gustav

              Comment


              • #22
                I see. Well, the expectation that only the margins for treated = 1 will change is not correct. At its simplest, this has nothing to do with fixed-effects, or the time variables. You can reproduce the same phenomenon with simple linear regressions against a single dichotomous predictor. And it's nothing really to do with -margins-. -margins- is just compiling the predicted values. Run:
                Code:
                regress salary i.treated
                predict xbxb1, xb
                rvfplot, name(rvf1, replace)
                regress salary_new i.treated
                predict xbxb2, xb
                tabstat xbxb*, by(treated)
                rvfplot, name(rvf2, replace)
                Take look at those -rvfplot- graphs. When you change the dependent variable, the residual structure changes in both groups - it has to in order to be optimal for the entire sample under least squares. When the residual structure changes, the predicted values also necessarily change. And, as I noted, -margins- is just averaging predicted values, so those must change as well.

                Comment


                • #23
                  Yes that makes perfect sense (that the margins-command is just the predicted values from the regression output). So, to backup a bit, my goal is to have graph I can use to assess the trends for the treatment and control group before the intervention. And I need this with the fe-option. From our back-and-forth, I gather that the proposed option #18 does not allow for this because the coefficients in, say #21, are not enough to visualise the trends for both groups but just provide me with did-coefficient. Is there a way arrive at the following visualization (marginsplot), yet with the fe-option specified:
                  Code:
                  reg salary_new i.treatment##i.time
                  margins treatment, at(time =(98 99 100 101 102 103))
                  marginsplot
                  Last edited by Gustav Egede Hansen; 28 Apr 2023, 01:00.

                  Comment


                  • #24
                    I think for this purpose you need to -collapse- the data to get the mean outcome in each group at each time point. Then plot those means.

                    Comment

                    Working...
                    X