Dear Stas,
I apologise for my late answer. I have tried your solution and I have encountered two difficulties.
The first problem was the incompatibility of -optimize_result_V_robust()- with an evaluator of type d. I replaced - optimize_init_evaluatortype(S, "d2")- by -optimize_init_evaluatortype(S, "v2"). The initial do file still worked fine after this modification.
The second problem is the output after inserting the code that you suggested:
"
clear mata
capture drop cons
global xlist private medicaid age age2 educyr actlim totchr
* Nonlinear 2SLS IV estimator for Poisson: computation using command optimize
generate cons = 1
local y docvis
local xlist private medicaid age age2 educyr actlim totchr cons
local zlist income ssiratio medicaid age age2 educyr actlim totchr cons
. mata
------------------------------------------------- mata (type end to exit) -------------------------------------------------------------------------------------------------
: void pgmm(todo, b, y, X, Z, Qb, g, H)
> {
> Xb = X*b'
> mu = exp(Xb)
> h = Z'(y-mu)
> W = cholinv(cross(Z,Z))
> Qb = h'W*h
> if (todo == 0) return
> G = -(mu:*Z)'X
> g = (G'W*h)'
> if (todo == 1) return
> H = G'W*G
> _makesymmetric(H)
> }
: st_view(y=., ., "`y'")
: st_view(X=., ., tokens("`xlist'"))
: st_view(Z=., ., tokens("`zlist'"))
: S = optimize_init()
: optimize_init_which(S,"min")
: optimize_init_evaluator(S, &pgmm())
: optimize_init_evaluatortype(S, "v2")
: optimize_init_argument(S, 1, y)
: optimize_init_argument(S, 2, X)
: optimize_init_argument(S, 3, Z)
: optimize_init_params(S, J(1,cols(X),0))
: optimize_init_technique(S,"nr")
: b = optimize(S)
Iteration 0: f(p) = 156836.04
Iteration 1: f(p) = 21765.741
Iteration 2: f(p) = 2087.4467
Iteration 3: f(p) = 186.55764
Iteration 4: f(p) = 182.298
Iteration 5: f(p) = 182.29546
Iteration 6: f(p) = 182.29541
Iteration 7: f(p) = 182.29541
:
: optimize_result_V_robust( S )
[symmetric]
1 2 3 4 5 6 7 8
+---------------------------------+
1 | . |
2 | . . |
3 | . . . |
4 | . . . . |
5 | . . . . . |
6 | . . . . . . |
7 | . . . . . . . |
8 | . . . . . . . . |
+---------------------------------+
:
:
: // some prep work in creating empty score_vars
: st_view( score_vars=., ., tokens("`score_vars'"))
: score_vars[,] = optimize_result_scores(S)
<istmt>: 3200 conformability error
r(3200);
: end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
. // of Mata
. _robust `score_vars' , cluster( cluster )
varlist required
r(100);
end of do-file
r(100);
"
Do you have any ideas on how to tweak the code?
Best regards,
Rodolphe
________________________________________
From: [email protected] [[email protected]] On Behalf Of Stas Kolenikov [[email protected]]
Sent: 17 July 2009 16:59
To: [email protected]
Subject: Re: st: Cluster-robust estimate of the VCE, GMM, Mata
Without going into any of the substantive details -- you could get the
sandwich VCE with
optimize_result_V_robust( S )
and in all likelihood you could get cluster corrected standard errors
along the lines of
// some prep work in creating empty score_vars
st_view( score_vars=., ., tokens("`score_vars'"))
score_vars[,] = optimize_result_scores(S)
end
// of Mata
_robust `score_vars' , cluster( cluster )
As a side note, I personally find clustering on age to be a relatively
strange idea, to tell you the truth. If you think there's dependence
on age, you could just as well use it as a covariate in your model
somewhere. Clustering is a problem when your sample was collected
using clusters of units sampled together; imposing clustering based on
exogenous variables is infrequently sensible, I think.
On Thu, Jul 16, 2009 at 6:53 AM, Rodolphe
Desbordes<[email protected]> wrote:
> Dear all,
>
> In "Microeconometrics Using Stata" by Colin Cameron and Pravin Trivedi, they explain, pp. 381-383, how to calculate a GMM estimator for a Poisson model with an endogenous regressor. The Mata code can be found here http://cameron.econ.ucdavis.edu/racd/trcount2009.do and I reproduce it below:
>
> "
> * Nonlinear 2SLS IV estimator for Poisson
>
> capture drop cons
> capture drop cluster
>
> gen cluster=group(age)
>
> clear mata
> generate cons = 1
> local y docvis
> local xlist private chronic female income cons
> local zlist private chronic female income cons
> local cluster cluster
> mata
> void pgmm(todo, b, y, X, Z, Qb, g, H)
> {
> Xb = X*b'
> mu = exp(Xb)
> h = Z'(y-mu)
> W = cholinv(cross(Z,Z))
> Qb = h'W*h
> if (todo == 0) return
> G = -(mu:*Z)'X
> g = (G'W*h)'
> if (todo == 1) return
> H = G'W*G
> _makesymmetric(H)
> }
> st_view(y=., ., "`y'")
> st_view(X=., ., tokens("`xlist'"))
> st_view(Z=., ., tokens("`zlist'"))
>
> st_view(C=., ., "`cluster'")
>
> S = optimize_init()
> optimize_init_which(S,"min")
> optimize_init_evaluator(S, &pgmm())
> optimize_init_evaluatortype(S, "d2")
> optimize_init_argument(S, 1, y)
> optimize_init_argument(S, 2, X)
> optimize_init_argument(S, 3, Z)
> optimize_init_params(S, J(1,cols(X),0))
> optimize_init_technique(S,"nr")
> b = optimize(S)
> // Compute robust estimate of VCE
> Xb = X*b'
> mu = exp(Xb)
> h = Z'(y-mu)
> W = cholinv(cross(Z,Z))
> G = -(mu:*Z)'X
> Shat = ((y-mu):*Z)'((y-mu):*Z)*rows(X)/(rows(X)-cols(X))
> Vb = luinv(G'W*G)*G'W*Shat*W*G*luinv(G'W*G)
> st_matrix("b",b)
> st_matrix("Vb",Vb)
> end
> "
>
> I would like to obtain a cluster-robust estimate of the variance-covariance matrix of the estimator (VCE). Hence, in the Mata code, I tried to replace the expression of Shat by:
>
> "
>
> V = J(cols(Z), cols(Z), 0)
> for(i=1; i<=colmax(C[.,1]); i++) {
> st_subview(x_j=., select(X, C:==i), ., .)
> st_subview(z_j=., select(Z, C:==i), ., .)
> st_subview(y_j=., select(y, C:==i), ., .)
> V = V +(cross(cross(z_j, (y_j- exp(x_j*b')))', cross(z_j, (y_j- exp(x_j* b')))'))
> }
>
>
> Shat = V*(rows(X))/(rows(X)-cols(X))*(colmax(C[.,1])/(colmax(C[.,1])-1))
>
> "
>
> However, I do not obtain the same estimates as those given by "poisson docvis private chronic female income, cl(age)". I am not sure what I am doing wrong. Any help would be greatly appreciated.
>
> Best regards,
>
> Rodolphe
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
--
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/