Does anyone have insights why gmm computation time is much greater when factor variables are used? The question arose in a real application where the additional computation time was problematic but here's a toy (and hopefully replicable) example that demonstrates the issue.
Results:
Code:
cap preserve cap drop _all sysuse auto gen trep=rep-1 loc lhs "trep" loc rhs1 "mpg i.foreign" loc rhs2 "mpg foreign" loc max=4 expand 100 timer clear /* gmm: first residual equation is of form y - f(x,b) */ /* second residual equation is of form y^2 - g(x,b) */ timer on 1 qui gmm (1: `lhs'-`max'*exp({xa:`rhs1' _cons})/(exp({xa:})+exp({xb:`rhs1' _cons}))) /// (2: `lhs'^2-(`max'^2)*(exp({xa:})/(exp({xa:})+exp({xb:}))^2)* /// ((exp({xa:})+exp({xb:})+exp({xa:})^2+exp({xa:})*exp({xb:}))/ /// (exp({xa:})+exp({xb:})+1))), inst(`rhs1') igmm vce(robust) winit(i) gmm timer off 1 timer on 2 qui gmm (1: `lhs'-`max'*exp({xa:`rhs2' _cons})/(exp({xa:})+exp({xb:`rhs2' _cons}))) /// (2: `lhs'^2-(`max'^2)*(exp({xa:})/(exp({xa:})+exp({xb:}))^2)* /// ((exp({xa:})+exp({xb:})+exp({xa:})^2+exp({xa:})*exp({xb:}))/ /// (exp({xa:})+exp({xb:})+1))), inst(`rhs2') igmm vce(robust) winit(i) gmm timer off 2 timer list cap restore
Code:
. cap preserve
. cap drop _all
.
. sysuse auto
(1978 automobile data)
.
. gen trep=rep-1
(5 missing values generated)
.
. loc lhs "trep"
. loc rhs1 "mpg i.foreign"
. loc rhs2 "mpg foreign"
. loc max=4
.
. expand 100
(7,326 observations created)
.
. timer clear
.
. /* gmm: first residual equation is of form y - f(x,b) */
. /* second residual equation is of form y^2 - g(x,b) */
.
. timer on 1
.
. qui gmm (1: `lhs'-`max'*exp({xa:`rhs1' _cons})/(exp({xa:})+exp({xb:`rhs1' _cons}))) ///
> (2: `lhs'^2-(`max'^2)*(exp({xa:})/(exp({xa:})+exp({xb:}))^2)* ///
> ((exp({xa:})+exp({xb:})+exp({xa:})^2+exp({xa:})*exp({xb:}))/ ///
> (exp({xa:})+exp({xb:})+1))), inst(`rhs1') igmm vce(robust) winit(i)
.
. gmm
GMM estimation
Number of parameters = 6
Number of moments = 6
Initial weight matrix: Identity Number of obs = 6,900
GMM weight matrix: Robust
------------------------------------------------------------------------------
| Robust
| Coefficient std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
xa |
mpg | -.0892447 .0047576 -18.76 0.000 -.0985693 -.07992
|
foreign |
Foreign | .6791125 .0533686 12.72 0.000 .574512 .783713
_cons | 2.747949 .1006754 27.30 0.000 2.550628 2.945269
-------------+----------------------------------------------------------------
xb |
mpg | -.124634 .0059788 -20.85 0.000 -.1363523 -.1129158
|
foreign |
Foreign | -.637334 .0660776 -9.65 0.000 -.7668436 -.5078243
_cons | 3.418192 .1225606 27.89 0.000 3.177977 3.658406
------------------------------------------------------------------------------
Instruments for equation 1: mpg 0b.foreign 1.foreign _cons
Instruments for equation 2: mpg 0b.foreign 1.foreign _cons
.
. timer off 1
.
. timer on 2
.
. qui gmm (1: `lhs'-`max'*exp({xa:`rhs2' _cons})/(exp({xa:})+exp({xb:`rhs2' _cons}))) ///
> (2: `lhs'^2-(`max'^2)*(exp({xa:})/(exp({xa:})+exp({xb:}))^2)* ///
> ((exp({xa:})+exp({xb:})+exp({xa:})^2+exp({xa:})*exp({xb:}))/ ///
> (exp({xa:})+exp({xb:})+1))), inst(`rhs2') igmm vce(robust) winit(i)
.
. gmm
GMM estimation
Number of parameters = 6
Number of moments = 6
Initial weight matrix: Identity Number of obs = 6,900
GMM weight matrix: Robust
------------------------------------------------------------------------------
| Robust
| Coefficient std. err. z P>|z| [95% conf. interval]
-------------+----------------------------------------------------------------
xa |
mpg | -.0892447 .0047576 -18.76 0.000 -.0985693 -.07992
foreign | .6791125 .0533686 12.72 0.000 .574512 .783713
_cons | 2.747949 .1006754 27.30 0.000 2.550628 2.945269
-------------+----------------------------------------------------------------
xb |
mpg | -.124634 .0059788 -20.85 0.000 -.1363523 -.1129158
foreign | -.637334 .0660776 -9.65 0.000 -.7668436 -.5078243
_cons | 3.418192 .1225606 27.89 0.000 3.177977 3.658406
------------------------------------------------------------------------------
Instruments for equation 1: mpg foreign _cons
Instruments for equation 2: mpg foreign _cons
.
. timer off 2
.
. timer list
1: 14.28 / 1 = 14.2830
2: 5.30 / 1 = 5.3010
.
. cap restore
.

Comment