Announcement

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

  • XTGLS2 - Estimate General GLS for Pooled, Fixed-effects and First-difference models in large N, small T panel data

    Thanks to Prof. Kit Baum, xtgls2 package are now available on SSC. You can install them from SSC by entering the following command into Stata:
    Code:
    ssc install xtgls2
    xtgls2 helps STATA users estimate General GLS for Pooled, Fixed-effects, First-differences models, as described in Kiefer (1980) and Wooldridge (2002, 2010).
    In which, the error covariance matrix has an arbitrary form within panels (allowing for heteroskedasticity over time periods and arbitrary serial correlations) and this structure is identical across panels.
    It is possible to relax these assumptions, i.e. to allow for heteroskedasticity across panels, by using Arellano (1987) standard errors. xtgls2 does this using the cluster(varname) option with varname = panel variables.
    For any comments about the command, please send to [email protected]
    Examples:

    Code:
    . webuse abdata, clear
    
    . xtbalance, range(1980 1984)
    
    (498 observations deleted due to out of range)
    
    (358 observations deleted due to discontinues)
    
    . xtgls2 n l(1/2).w c.(k ys)##c.(k ys) i.ind i.year, c(c) fd nocons cl(id) igls nolo
    
    Convergenced after 30 iterations
    
    First-difference Generalized Least Squares (FDGLS) regression
    
    Error covariance structure
         Time series:     Heteroskedasticity and serial correlation
         Cross-section:   Homoskedasticity
    
    Estimated covariances:    3                     Number of obs     =         70
    R-squared:                                      Number of groups  =         35
         Within   =  0.0096                         Time periods      =          2
         Between  =  0.0004                         Wald chi2(9)      =     91.594
         Overall  =  0.0004                         Prob > chi2       =     0.0000
    
                                        (Std. err. adjusted for 35 clusters in id)
    ------------------------------------------------------------------------------
                 |               Robust
               n | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
               w |
             L1. |   .4604677   .1173617     3.92   0.000      .230443    .6904925
             L2. |   .1945558   .2115014     0.92   0.358    -.2199794     .609091
                 |
               k |   2.538793   1.366328     1.86   0.063    -.1391604    5.216747
              ys |  -28.17702   30.72851    -0.92   0.359    -88.40379    32.04975
                 |
         c.k#c.k |   -.103131   .0643289    -1.60   0.109    -.2292133    .0229513
                 |
        c.k#c.ys |  -.4682237   .2880477    -1.63   0.104    -1.032787    .0963393
                 |
       c.ys#c.ys |   2.904299   3.291465     0.88   0.378    -3.546854    9.355452
                 |
            year |
           1982  |  -.0318849   .0522636    -0.61   0.542    -.1343197    .0705499
           1983  |  -.0247939   .0231626    -1.07   0.284    -.0701917     .020604
    ------------------------------------------------------------------------------
    Manh Hoang-Ba,
    Facebook,
    Eureka! Uni - YouTube,
    ManhHB94 (Manh Hoang Ba),
    Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

  • #2
    This looks like a command I’ve been waiting for! I’d prefer the syntax for the choice of variance estimator to be vce(robust) as well as vce(cluster cid). In the former case, inference is robust to what I call “system heteroskedasticity,” which arises from estimating a constant T x T variance covariance matrix that’s not also the conditional var-cov matrix. That’s a separate issue from clustering. Hopefully the current command does what I want if you use cluster(id) where id is the panel id.

    Comment


    • #3
      Originally posted by Jeff Wooldridge View Post
      This looks like a command I’ve been waiting for! I’d prefer the syntax for the choice of variance estimator to be vce(robust) as well as vce(cluster cid). In the former case, inference is robust to what I call “system heteroskedasticity,” which arises from estimating a constant T x T variance covariance matrix that’s not also the conditional var-cov matrix. That’s a separate issue from clustering. Hopefully the current command does what I want if you use cluster(id) where id is the panel id.
      Hi prof. Jeffrey Wooldridge,

      xtgls2 with the cluster(panel_var) option does indeed compute standard errors that are robust to “system heteroskedasticity”, as you prefer.

      Specifically, the formula for computing robust standard errors can be found in Wooldridge (2010):
      • Pooled GLS: formula (7.52) on page 172
      • FEGLS: formula in chapter 10, on page 314
      • FDGSL: similar to FEGLS and PGLS but with first-different transformed data.
      The Pooled GLS estimation results with robust standard errors from xtgls2 can be compared with the corresponding output from EViews, with the declaration GLS weights = Period SUR and Covariance method = White period (no d.f. correction).

      Thank you so much and wish you a good day.
      Manh Hoang-Ba,
      Facebook,
      Eureka! Uni - YouTube,
      ManhHB94 (Manh Hoang Ba),
      Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

      Comment


      • #4
        Update for xtgls2 and xttest4 command

        xttest4 performs Kézdi (2003) test for heteroskedasticity in fixed effects models.
        Version 1.3 for xtgls2 can be installed by downloading the .ado and .sthlp files attached to this comment.

        The latest versions of these commands can be found at my github address: ManhHB94 (Manh Hoang Ba)

        Any comments are welcome.
        Attached Files
        Manh Hoang-Ba,
        Facebook,
        Eureka! Uni - YouTube,
        ManhHB94 (Manh Hoang Ba),
        Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

        Comment


        • #5
          Thanks to prof. KitBaum , xtgls2 version 2.0 has been updated on SSC.
          HTML Code:
          ssc install xtgls2 , replace
          Where:
          1. option cov() has been disabled. The general within-panel covariance structure is now applied by default.
          2. correct R-squared between/overall
          3. estimation, iterated GLS, robust and cluster-robust variances are all coded in Mata
          4. REGLS (re) now really uses the RE (Swamy-Arora) residuals to build Omega instead the POLS residuals
          5. strict balance check. When the igls is specified, REGLS and PGLS yield identical results upon convergence.
          6. aweight and pweight allowed.
          7. by: allowed (byable)
          HTML Code:
          . . webuse abdata, clear
          
          . . keep if year > 1977 & year < 1983
          (331 observations deleted)
          
          . xtgls2 n w k ys, igls
          
          Iteration 1:  tolerance =      .171652
          Iteration 2:  tolerance =     .1158126
          Iteration 3:  tolerance =      .071945
          Iteration 4:  tolerance =    .04250447
          Iteration 5:  tolerance =    .02422975
          Iteration 6:  tolerance =    .01350557
          Iteration 7:  tolerance =   .007430293
          Iteration 8:  tolerance =   .004058082
          Iteration 9:  tolerance =   .002207422
          Iteration 10:  tolerance =   .001198103
          Iteration 11:  tolerance =  .0006495051
          Iteration 12:  tolerance =  .0003518754
          Iteration 13:  tolerance =  .0001905646
          Iteration 14:  tolerance =  .0001031841
          Iteration 15:  tolerance =  .0000558648
          Iteration 16:  tolerance =   .000030244
          Iteration 17:  tolerance =   .000016373
          Iteration 18:  tolerance =  8.86357e-06
          Iteration 19:  tolerance =  4.79828e-06
          Iteration 20:  tolerance =  2.59753e-06
          Iteration 21:  tolerance =  1.40616e-06
          Iteration 22:  tolerance =  7.61216e-07
          Iteration 23:  tolerance =  4.12080e-07
          Iteration 24:  tolerance =  2.23076e-07
          Iteration 25:  tolerance =  1.20761e-07
          Iteration 26:  tolerance =  6.53731e-08
          
          Pooled Generalized Least Squares (PGLS) regression
          
          Estimated covariances:   15                     Number of obs     =        700
          R-squared:                                      Number of groups  =        140
               Within   =  0.6308                         Time periods      =          5
               Between  =  0.8459                         Wald chi2(3)      =    748.525
               Overall  =  0.8399                         Prob > chi2       =     0.0000
          
          ------------------------------------------------------------------------------
                     n | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                     w |  -.3780657   .0529568    -7.14   0.000    -.4818591   -.2742723
                     k |   .4923089    .021337    23.07   0.000     .4504892    .5341286
                    ys |    .510608   .0731743     6.98   0.000      .367189    .6540271
                 _cons |   .0932548   .3700124     0.25   0.801    -.6319562    .8184658
          ------------------------------------------------------------------------------
          Note: Reported R-squared values are not used for model comparison
                and serve only as measures of predictive ability on the original data.
          
          . xtgls2 n w k ys, igls re
          
          Iteration 1:  tolerance =     .1565815
          Iteration 2:  tolerance =    .08811389
          Iteration 3:  tolerance =     .0520652
          Iteration 4:  tolerance =    .02995676
          Iteration 5:  tolerance =    .01680959
          Iteration 6:  tolerance =   .009284662
          Iteration 7:  tolerance =   .005082106
          Iteration 8:  tolerance =   .002767821
          Iteration 9:  tolerance =   .001503265
          Iteration 10:  tolerance =   .000815232
          Iteration 11:  tolerance =  .0004417462
          Iteration 12:  tolerance =  .0002392612
          Iteration 13:  tolerance =  .0001295591
          Iteration 14:  tolerance =  .0000701466
          Iteration 15:  tolerance =  .0000379765
          Iteration 16:  tolerance =  .0000205593
          Iteration 17:  tolerance =  .0000111299
          Iteration 18:  tolerance =  6.02516e-06
          Iteration 19:  tolerance =  3.26170e-06
          Iteration 20:  tolerance =  1.76571e-06
          Iteration 21:  tolerance =  9.55856e-07
          Iteration 22:  tolerance =  5.17446e-07
          Iteration 23:  tolerance =  2.80116e-07
          Iteration 24:  tolerance =  1.51639e-07
          Iteration 25:  tolerance =  8.20889e-08
          
          Random-effects Generalized Least Squares (REGLS) regression
          
          Estimated covariances:   15                     Number of obs     =        700
          R-squared:                                      Number of groups  =        140
               Within   =  0.6308                         Time periods      =          5
               Between  =  0.8459                         Wald chi2(3)      =    748.525
               Overall  =  0.8399                         Prob > chi2       =     0.0000
          
          ------------------------------------------------------------------------------
                     n | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                     w |  -.3780657   .0529568    -7.14   0.000    -.4818591   -.2742723
                     k |   .4923089    .021337    23.07   0.000     .4504892    .5341286
                    ys |    .510608   .0731743     6.98   0.000      .367189    .6540271
                 _cons |   .0932548   .3700124     0.25   0.801    -.6319562    .8184658
          ------------------------------------------------------------------------------
          Note: Reported R-squared values are not used for model comparison
                and serve only as measures of predictive ability on the original data.
          Manh Hoang-Ba,
          Facebook,
          Eureka! Uni - YouTube,
          ManhHB94 (Manh Hoang Ba),
          Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

          Comment


          • #6
            I continue to use this very nice command for recent work on improved efficiency in staggered difference-in-differences. Two additional features would be nice.

            1. Allow unbalanced panels. There's no good statistical reason not to allow this.
            2. Allow for the vce(uncond) option when using margins after xtgls2.

            Comment


            • #7
              Ugh.
              Last edited by Jeff Wooldridge; 15 Nov 2026, 12:43. Reason: Posted the response in the wrong thread.

              Comment


              • #8
                Originally posted by Jeff Wooldridge View Post
                I continue to use this very nice command for recent work on improved efficiency in staggered difference-in-differences. Two additional features would be nice.

                1. Allow unbalanced panels. There's no good statistical reason not to allow this.
                2. Allow for the vce(uncond) option when using margins after xtgls2.
                Hi prof. Jeff Wooldridge,

                Thank you for your suggestion. I must admit that I was genuinely surprised by your suggestion to extend GLS to unbalanced panels. I had always assumed that GLS estimators with an arbitrary form for Ω could only be applied to balanced panels.

                In Kiefer (1980) and Wooldridge (2002, 2010), I could only find the formulas for balanced panels, with no explicit discussion of their extension to unbalanced panels. EViews, R (pggls), and Stata (xtgls) also impose a balanced-panel requirement when an unrestricted covariance structure is specified for Ω. Consequently, when developing xtgls2, I also restricted its application to balanced panels.

                Before implementing this extension, I would like to make sure that my proposed generalization of the pooled GLS estimator to unbalanced panels, as presented in the attached image, is mathematically correct.
                I would greatly appreciate it if you could point out any mistakes or misunderstandings in my derivation.

                Click image for larger version

Name:	Screenshot 2026-11-16 202831.png
Views:	1
Size:	79.0 KB
ID:	1788169

                Manh Hoang-Ba,
                Facebook,
                Eureka! Uni - YouTube,
                ManhHB94 (Manh Hoang Ba),
                Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

                Comment


                • #9
                  Thanks to prof. KitBaum , xtgls2 version 2.1 has been updated on SSC.
                  Code:
                  ssc install xtgls2, update
                  Where:
                  1. work on unbalanced panels
                  2. Omega is estimated pairwise, with elements Omega = (1/N_ts) * sum_i s_it*s_is*u_it*u_is (T = number of distinct time values of the sample, and u is the first-step residual)
                  3. Omega not positive definite: when a sub-matrix that is actually used fails, the whole Omega is repaired by a spectral floor (options nopdfix, pdfloor(#)).
                  4. fegls: panel i is demeaned over its own observed periods and Helmert-transformed (T_i-1 rows).
                  5. fdgls: FD are taken over calendar neighbours (t - delta); factor variables are not allowed.
                  6. add predict , scores (equation-level score r_it; the unit score is sum_t x_it*r_it)
                  7. allow margins, vce(unconditional)
                  Code:
                  . . webuse abdata, clear
                  
                  . . xtgls2 n w k ys, pgls robust
                  
                  note: - some Omega_i was not positive definite
                        - smallest eigenvalue of the estimated Omega is -.1777113
                        - eigenvalues below .01 x the largest were raised to that floor
                          in 1 of 1 iteration(s)
                  
                  Pooled Generalized Least Squares (PGLS) regression
                  
                  Estimated covariances:   45                     Number of obs     =      1,031
                  Group variable: id                              Number of groups  =        140
                  
                  R-squared:                                      Obs per group:
                       Within  = 0.6007                                         min =          7
                       Between = 0.8465                                         avg =        7.4
                       Overall = 0.8348                                         max =          9
                  
                                                                  Wald chi2(3)      =    776.368
                                                                  Prob > chi2       =     0.0000
                  
                                                     (Std. err. adjusted for 140 clusters in id)
                  ------------------------------------------------------------------------------
                               |               Robust
                             n | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
                  -------------+----------------------------------------------------------------
                             w |  -.2088632   .1131379    -1.85   0.065    -.4306094    .0128831
                             k |   .7390918   .0300086    24.63   0.000     .6802761    .7979075
                            ys |    .357972   .0915926     3.91   0.000     .1784538    .5374903
                         _cons |   .3045197    .636871     0.48   0.633    -.9437245    1.552764
                  ------------------------------------------------------------------------------
                  note: reported R-squared values are not used for model comparison
                        and serve only as measures of predictive ability on the original data.
                  
                  . . margins, dydx(w k ys) vce(unconditional)
                  
                  Average marginal effects                                 Number of obs = 1,031
                  
                  Expression: Linear prediction, predict()
                  dy/dx wrt:  w k ys
                  
                                                     (Std. err. adjusted for 140 clusters in id)
                  ------------------------------------------------------------------------------
                               |            Unconditional
                               |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
                  -------------+----------------------------------------------------------------
                             w |  -.2088632   .1131379    -1.85   0.065    -.4306094    .0128831
                             k |   .7390918   .0300086    24.63   0.000     .6802761    .7979075
                            ys |    .357972   .0915926     3.91   0.000     .1784538    .5374903
                  ------------------------------------------------------------------------------
                  Manh Hoang-Ba,
                  Facebook,
                  Eureka! Uni - YouTube,
                  ManhHB94 (Manh Hoang Ba),
                  Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

                  Comment

                  Working...
                  X