I am trying to estimate a model by the GMM with moptimize() and the Gauss-Newton algorithm. Although I can get a solution the returned covariance matrix is filled with missing values.
I have tried to estimate a linear regression model by GMM to see if it was model specific, but I get a similar result. The coefficients are the same compared to the command regress but the covariance matrix has missing values. What am I doing wrong?
I've got this problem on Stata IC versions 12, 13 and 14 on Windows.
Best regards
Christophe
I have tried to estimate a linear regression model by GMM to see if it was model specific, but I get a similar result. The coefficients are the same compared to the command regress but the covariance matrix has missing values. What am I doing wrong?
I've got this problem on Stata IC versions 12, 13 and 14 on Windows.
Best regards
Christophe
Code:
. sysuse auto, clear
(1978 Automobile Data)
.
. clear mata
. mata:
------------------------------------------------- mata (type end to exit) -----------------------------------------------------------------
:
: void Reg(M, todo, b, r, S)
> {
>
> real scalar phi1
> real scalar phi2
> real colvector w, xb, zg, e, iota, t, u
> real scalar N
> real matrix W, Z
>
>
> xb = moptimize_util_xb(M,b,1)
> N = rows(xb)
>
>
> u = moptimize_util_depvar(M,1) :- xb
> W = moptimize_util_userinfo(M, 1)
>
> phi1 = quadcross(W, 1, u, 0)
> r = (phi1)
>
>
> }
note: argument todo unused
note: argument S unused
note: variable phi2 unused
note: variable w unused
note: variable zg unused
note: variable e unused
note: variable iota unused
note: variable t unused
note: variable N set but not used
note: variable Z unused
:
: Y = st_data(.,"weight")
: X = st_data(.,"mpg")
: M = moptimize_init()
: moptimize_init_evaluatortype(M,"q0")
: moptimize_init_evaluator(M,&Reg())
: moptimize_init_technique(M,"gn")
: moptimize_init_depvar(M,1,Y)
: moptimize_init_eq_indepvars(M,1,X)
:
: moptimize_init_userinfo(M, 1, (X) )
: moptimize(M)
initial: f(p) = 2.024e+13
alternative: f(p) = 2.024e+13
rescale: f(p) = 1.608e+12
Iteration 0: f(p) = 1.608e+12
Iteration 1: f(p) = 5.449e-11
Iteration 2: f(p) = 2.921e-16
: H = moptimize_result_Hessian(M)
: b = moptimize_result_coefs(M)
: N = rows(Y)
:
: sigma = quadcolsum( (Y-(X,J(N,1,1))*b'):^2)/(N-1)
: qrinv(quadcross(X, 1,X, 1))
1 2
+-------------------------------+
1 | .0004092558 -.0087160428 |
2 | -.0087160428 .1991416689 |
+-------------------------------+
: V = qrinv(quadcross(X, 1,X, 1))*sigma
: b',sqrt(diagonal(V))
1 2
+-------------------------------+
1 | -108.4315547 9.281294329 |
2 | 5328.758517 204.7350436 |
+-------------------------------+
:
:
: qrinv(H)
1 2
+-------------------------------+
1 | .0000760945 -.0017403548 |
2 | -.0017403548 .0398039838 |
+-------------------------------+
: b
1 2
+-------------------------------+
1 | -108.4315547 5328.758517 |
+-------------------------------+
: H
[symmetric]
1 2
+-----------------------------+
1 | 1299565119 |
2 | 56821055.74 2484419.577 |
+-----------------------------+
:
: moptimize_result_V(M)
[symmetric]
1 2
+---------+
1 | . |
2 | . . |
+---------+
: moptimize_result_display(M)
Number of obs = 74
------------------------------------------------------------------------------
Y | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x1 | -108.4316 . . . . .
_cons | 5328.759 . . . . .
------------------------------------------------------------------------------
:
:
: end
-------------------------------------------------------------------------------------------------------------------------------------------
.
.
. reg weight mpg
Source | SS df MS Number of obs = 74
-------------+------------------------------ F( 1, 72) = 134.62
Model | 28728735.3 1 28728735.3 Prob > F = 0.0000
Residual | 15365443.1 72 213408.932 R-squared = 0.6515
-------------+------------------------------ Adj R-squared = 0.6467
Total | 44094178.4 73 604029.841 Root MSE = 461.96
------------------------------------------------------------------------------
weight | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | -108.4316 9.345526 -11.60 0.000 -127.0615 -89.80159
_cons | 5328.759 206.1519 25.85 0.000 4917.802 5739.715
------------------------------------------------------------------------------
Code:
sysuse auto, clear
clear mata
mata:
void Reg(M, todo, b, r, S)
{
real scalar phi1
real scalar phi2
real colvector w, xb, zg, e, iota, t, u
real scalar N
real matrix W, Z
xb = moptimize_util_xb(M,b,1)
N = rows(xb)
u = moptimize_util_depvar(M,1) :- xb
W = moptimize_util_userinfo(M, 1)
phi1 = quadcross(W, 1, u, 0)
r = (phi1)
}
Y = st_data(.,"weight")
X = st_data(.,"mpg")
M = moptimize_init()
moptimize_init_evaluatortype(M,"q0")
moptimize_init_evaluator(M,&Reg())
moptimize_init_technique(M,"gn")
moptimize_init_depvar(M,1,Y)
moptimize_init_eq_indepvars(M,1,X)
moptimize_init_userinfo(M, 1, (X) )
moptimize(M)
H = moptimize_result_Hessian(M)
b = moptimize_result_coefs(M)
N = rows(Y)
sigma = quadcolsum( (Y-(X,J(N,1,1))*b'):^2)/(N-1)
qrinv(quadcross(X, 1,X, 1))
V = qrinv(quadcross(X, 1,X, 1))*sigma
b',sqrt(diagonal(V))
qrinv(H)
b
H
moptimize_result_V(M)
moptimize_result_display(M)
end
reg weight mpg

Comment