Announcement

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

  • mixed: random time effects in a multi-level model

    I'm running a multi level panel data model. For illustration I used the dataset used in Example 4 for the mixed command.

    use https://www.stata-press.com/data/r18/productivity
    mixed gsp private emp hwy water other unemp || region: || state:


    Here there is no control of yearly shocks. I could include it as time-fixed-effects by adding "i.year"

    mixed gsp private emp hwy water other unemp i.year || region: || state:

    However, let me assume there are some region-specific shocks, which I assume are drawn from a normal distribution.
    It has been suggested to me that I could use the code sequence below. However, it does not seem not work.

    egen region_year = group(region year)
    mixed gsp private emp hwy water other unemp || region: || state: || region_year:


    Does anyone have a suggestion how this could be done?

    Best,

    Øivind



  • #2
    In your example, states are nested within regions, or region-years are nested within regions, but there is no three-way nesting. One approach to running a n-level nested model is to include one of the non-nested random effects as a fixed effect.

    Code:
    use https://www.stata-press.com/data/r18/productivity, clear
    egen region_year = group(region year)
    mixed gsp private emp hwy water other unemp i.state || region:  || region_year:
    Res.:

    Code:
    . mixed gsp private emp hwy water other unemp i.state || region:  || region_year:
    
    Performing EM optimization ...
    
    Performing gradient-based optimization: 
    Iteration 0:  Log likelihood =  1618.2107  
    Iteration 1:  Log likelihood =  1618.4368  
    Iteration 2:  Log likelihood =   1618.441  
    Iteration 3:  Log likelihood =   1618.441  
    
    Computing standard errors ...
    
    Mixed-effects ML regression                          Number of obs =       816
    
            Grouping information
            -------------------------------------------------------------
                            |     No. of       Observations per group
             Group variable |     groups    Minimum    Average    Maximum
            ----------------+--------------------------------------------
                     region |          9         51       90.7        136
                region_year |        153          3        5.3          8
            -------------------------------------------------------------
    
                                                         Wald chi2(53) = 606451.90
    Log likelihood =   1618.441                          Prob > chi2   =    0.0000
    
    ------------------------------------------------------------------------------
             gsp | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
         private |   .1678433   .0246308     6.81   0.000     .1195679    .2161187
             emp |   .8411632   .0292839    28.72   0.000     .7837679    .8985586
             hwy |   .0686575   .0286903     2.39   0.017     .0124256    .1248893
           water |   .0529819   .0142241     3.72   0.000     .0251032    .0808606
           other |  -.0526887   .0163131    -3.23   0.001    -.0846618   -.0207156
           unemp |  -.0025485   .0010539    -2.42   0.016    -.0046142   -.0004829
                 |
           state |
              2  |   .1685498   .0175702     9.59   0.000     .1341129    .2029867
              3  |   .1075591   .0204384     5.26   0.000     .0675005    .1476177
              4  |   .1740046   .0450249     3.86   0.000     .0857575    .2622517
              5  |   .1743435   .0190096     9.17   0.000     .1370854    .2116017
              6  |   .1928321   .0208185     9.26   0.000     .1520286    .2336357
              7  |   .2595597   .0356268     7.29   0.000     .1897324    .3293869
              8  |   .0616153   .0229163     2.69   0.007     .0167003    .1065304
              9  |   .0276292   .0159675     1.73   0.084    -.0036665    .0589249
             10  |   .1978655   .0319947     6.18   0.000     .1351569     .260574
             11  |    .103282   .0314116     3.29   0.001     .0417163    .1648477
             12  |    .051283   .0143452     3.57   0.000     .0231669     .079399
             13  |   .1060347   .0158968     6.67   0.000     .0748776    .1371919
             14  |   .1595039   .0154171    10.35   0.000     .1292868    .1897209
             15  |   .1546546   .0143087    10.81   0.000       .12661    .1826991
             16  |   .3629511    .022631    16.04   0.000     .3185951     .407307
             17  |   .0491186   .0328154     1.50   0.134    -.0151984    .1134355
             18  |   .1041718   .0231926     4.49   0.000     .0587152    .1496283
             19  |   .1039931   .0231399     4.49   0.000     .0586398    .1493464
             20  |    .115387   .0268919     4.29   0.000     .0626797    .1680942
             21  |   .0535544   .0189622     2.82   0.005      .016389    .0907197
             22  |   .0627747   .0132735     4.73   0.000     .0367592    .0887902
             23  |   .0635558   .0162901     3.90   0.000     .0316277    .0954838
             24  |   .2023661   .0300517     6.73   0.000     .1434658    .2612663
             25  |   .1539351    .020832     7.39   0.000     .1131051    .1947651
             26  |   .2015537   .0331494     6.08   0.000     .1365821    .2665254
             27  |   .1131772   .0352483     3.21   0.001     .0440919    .1822625
             28  |   .1605794    .021939     7.32   0.000     .1175797    .2035791
             29  |   .3091891   .0253934    12.18   0.000      .259419    .3589592
             30  |   .1550842   .0422202     3.67   0.000     .0723341    .2378343
             31  |  -.0054743   .0171769    -0.32   0.750    -.0391404    .0281917
             32  |   .2338759   .0334445     6.99   0.000     .1683259     .299426
             33  |   .0211758   .0304467     0.70   0.487    -.0384985    .0808502
             34  |   .2605091   .0161532    16.13   0.000     .2288495    .2921688
             35  |   .1121409    .016382     6.85   0.000     .0800326    .1442491
             36  |  -.0015246   .0306134    -0.05   0.960    -.0615258    .0584765
             37  |   .1389025   .0404964     3.43   0.001     .0595309    .2182741
             38  |  -.0712285   .0204299    -3.49   0.000    -.1112703   -.0311866
             39  |   .1396751   .0297095     4.70   0.000     .0814455    .1979048
             40  |   -.021179   .0146131    -1.45   0.147    -.0498201    .0074621
             41  |   .1659266   .0353039     4.70   0.000     .0967322     .235121
             42  |   .1554185   .0232684     6.68   0.000     .1098134    .2010237
             43  |   .1259509    .038859     3.24   0.001     .0497886    .2021133
             44  |   .0843257   .0219323     3.84   0.000     .0413391    .1273122
             45  |   .2137898   .0202833    10.54   0.000     .1740353    .2535444
             46  |   .1227678   .0217213     5.65   0.000     .0801948    .1653408
             47  |   .0566869   .0188973     3.00   0.003     .0196489    .0937249
             48  |   .5863681   .0422289    13.89   0.000      .503601    .6691352
                 |
           _cons |   2.195105   .2278911     9.63   0.000     1.748446    2.641763
    ------------------------------------------------------------------------------
    
    ------------------------------------------------------------------------------
      Random-effects parameters  |   Estimate   Std. err.     [95% conf. interval]
    -----------------------------+------------------------------------------------
    region: Identity             |
                      var(_cons) |   7.49e-14   1.15e-12      6.79e-27    .8263233
    -----------------------------+------------------------------------------------
    region_year: Identity        |
                      var(_cons) |   .0004662   .0000799      .0003332    .0006522
    -----------------------------+------------------------------------------------
                   var(Residual) |   .0008657   .0000483      .0007761    .0009658
    ------------------------------------------------------------------------------
    LR test vs. linear model: chi2(2) = 105.88                Prob > chi2 = 0.0000
    
    Note: LR test is conservative and provided only for reference.

    Comment


    • #3
      Thanks, Andrew.

      But wouldn't the following code work?

      . mixed gsp private emp hwy water other unemp || _all: R.region_year || state:

      Performing EM optimization ...

      Performing gradient-based optimization:
      Iteration 0: Log likelihood = 1475.774
      Iteration 1: Log likelihood = 1475.774

      Computing standard errors ...

      Mixed-effects ML regression Number of obs = 816

      Grouping information
      -------------------------------------------------------------
      | No. of Observations per group
      Group variable | groups Minimum Average Maximum
      ----------------+--------------------------------------------
      _all | 1 816 816.0 816
      state | 48 17 17.0 17
      -------------------------------------------------------------

      Wald chi2(6) = 11680.07
      Log likelihood = 1475.774 Prob > chi2 = 0.0000

      ------------------------------------------------------------------------------
      gsp | Coefficient Std. err. z P>|z| [95% conf. interval]
      -------------+----------------------------------------------------------------
      private | .221683 .0209596 10.58 0.000 .1806029 .262763
      emp | .7743015 .0262051 29.55 0.000 .7229405 .8256626
      hwy | .0637544 .0230055 2.77 0.006 .0186644 .1088443
      water | .0485353 .0138855 3.50 0.000 .0213203 .0757504
      other | -.0503394 .0160622 -3.13 0.002 -.0818209 -.018858
      unemp | -.0033719 .001021 -3.30 0.001 -.005373 -.0013709
      _cons | 2.290285 .1515078 15.12 0.000 1.993335 2.587235
      ------------------------------------------------------------------------------

      ------------------------------------------------------------------------------
      Random-effects parameters | Estimate Std. err. [95% conf. interval]
      -----------------------------+------------------------------------------------
      _all: Identity |
      var(R.region_year) | .0004699 .0000847 .0003301 .0006689
      -----------------------------+------------------------------------------------
      state: Identity |
      var(_cons) | .0081555 .0018308 .0052526 .0126629
      -----------------------------+------------------------------------------------
      var(Residual) | .0009365 .0000544 .0008358 .0010494
      ------------------------------------------------------------------------------


      Comment


      • #4
        That will be a two-way crossed random effects model and compared to the three-level nested model, you lose the nesting structure. As region-year effects entail region effects, I think dropping region and estimating a crossed random effects model is fine in this instance.

        Code:
        mixed gsp private emp hwy water other unemp || _all: R.region_year || _all: R.state
        Also, note that in my example in #2, the variance of the region random effects is effectively zero. Such a model would not typically be estimated in practice because region effects are accounted for by state effects (as the state level represents a lower level of aggregation). Therefore, the state dummies absorb all the variance. My illustration would make sense if there was no significant nesting.
        Last edited by Andrew Musau; 19 Feb 2025, 09:38.

        Comment


        • #5
          Thanks for your quick response.

          Comment

          Working...
          X