I am trying to use `gmm` to replicate the results of using `xtreg, fe`. In short, I'm looking for a way to control for group fixed-effects in `gmm`. Here is an example using Stata's built-in data:


Code:
webuse nlswork, clear
xtset idcode
//OLS regression
regress ln_wage age collgrad 
//fixed-effects linear regression
xtreg ln_wage age collgrad, fe
//gmm estimation, results match these of OLS, how to include idcode fixed-effects to match xtreg?
gmm (ln_wage - {xb: age collgrad _cons}), instruments(age collgrad)

Of course, in this simple example, I can use `xtreg` but I need `gmm` for a more involved example where I have multiple equations with endogenous variables in a panel data.

The results of the code above are pasted below.



Code:
. webuse nlswork, clear
(National Longitudinal Survey.  Young Women 14-26 years of age in 1968)

. xtset idcode
       panel variable:  idcode (unbalanced)

. //OLS regression
. regress ln_wage age collgrad 

      Source |       SS           df       MS      Number of obs   =    28,510
-------------+----------------------------------   F(2, 28507)     =   2965.19
       Model |  1122.22339         2  561.111694   Prob > F        =    0.0000
    Residual |  5394.46676    28,507  .189233057   R-squared       =    0.1722
-------------+----------------------------------   Adj R-squared   =    0.1721
       Total |  6516.69015    28,509  .228583611   Root MSE        =    .43501

------------------------------------------------------------------------------
     ln_wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   .0162795   .0003895    41.79   0.000      .015516     .017043
    collgrad |   .3988395   .0069794    57.15   0.000     .3851595    .4125194
       _cons |   1.135051    .011479    98.88   0.000     1.112552    1.157551
------------------------------------------------------------------------------

. //fixed-effects linear regression
. xtreg ln_wage age collgrad, fe
note: collgrad omitted because of collinearity

Fixed-effects (within) regression               Number of obs     =     28,510
Group variable: idcode                          Number of groups  =      4,710

R-sq:                                           Obs per group:
     within  = 0.1026                                         min =          1
     between = 0.0877                                         avg =        6.1
     overall = 0.0774                                         max =         15

                                                F(1,23799)        =    2720.20
corr(u_i, Xb)  = 0.0314                         Prob > F          =     0.0000

------------------------------------------------------------------------------
     ln_wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   .0181349   .0003477    52.16   0.000     .0174534    .0188164
    collgrad |          0  (omitted)
       _cons |   1.148214   .0102579   111.93   0.000     1.128107     1.16832
-------------+----------------------------------------------------------------
     sigma_u |  .40635023
     sigma_e |  .30349389
         rho |  .64192015   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(4709, 23799) = 8.81                 Prob > F = 0.0000

. //gmm estimations, results match these of OLS, how to include idcode fixed-effects?
. gmm (ln_wage - {xb: age collgrad _cons}), instruments(age collgrad)

Step 1
Iteration 0:   GMM criterion Q(b) =  2.8447984  
Iteration 1:   GMM criterion Q(b) =  7.453e-28  
Iteration 2:   GMM criterion Q(b) =  8.448e-33  

Step 2
Iteration 0:   GMM criterion Q(b) =  4.585e-32  
Iteration 1:   GMM criterion Q(b) =  4.585e-32  (backed up)

note: model is exactly identified

GMM estimation 

Number of parameters =   3
Number of moments    =   3
Initial weight matrix: Unadjusted                 Number of obs   =     28,510
GMM weight matrix:     Robust

------------------------------------------------------------------------------
             |               Robust
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   .0162795   .0004112    39.59   0.000     .0154737    .0170854
    collgrad |   .3988395   .0071752    55.59   0.000     .3847763    .4129026
       _cons |   1.135051   .0114914    98.77   0.000     1.112529    1.157574
------------------------------------------------------------------------------
Instruments for equation 1: age collgrad _cons