Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: question: getting GMM working with a moment-evaluator program


From   László Sándor <sandorl@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: question: getting GMM working with a moment-evaluator program
Date   Thu, 1 Oct 2009 13:58:29 -0400

Hi all,

I tried to write up a relatively simple GMM estimation, but I needed a
moment-evaluator program to do some simulation for each evaluation
(this basically becomes the method of simulated moments). I tried to
follow of the example of David Drukker, presented in DC this summer
(slides 23-26): http://www.stata.com/meeting/dcconf09/abstracts.html
(A similar example is in the -help gmm- file too.)

His GMM evaluator function is tailored for the application, thus uses
some actual variable names, not locals coming from the command. I did
the same (though I also needed to use Mata). Crucially, he had no
varlist in his command either: "gmm xtfe ," (But mentioned a varlist
in the syntax -- still, it worked for him.)

Could you look into what goes wrong here? The error messages did not
help me much. I don't know where is it expecting a varlist, and how I
could provide it. (I admit I am not good with ado files yet.)

The error:

. gmm msm moment, nequations(1) parameters(cons hp weight ac) instruments(cons
> hp weight ac cons2 hp2 weight2 ac2, noconstant)
varlist required
varlist required
error calling msm moment at initial values
varlist required
(error occurred while loading msm.ado)
r(100);

The mata code defining the function that the evaluator function uses
(this is in the main do-file, but the .mo is saved in the project's
folder, so accessible for the ado-file too, I presume):

    cap drop deltastar sigma xi
    gen deltastar = 0
    gen sigma = 0
    gen xi = 0
mata:
void fingerscrossed(real rowvector beta) {
    real scalar ns
    real vector d, sig, s, p, xi, y
    real matrix X, term
    ns = 1000
    st_view(X,.,"cons hp weight ac")
    st_view(d,.,"deltastar")
    st_view(sig,.,"sig")
    st_view(s,.,"share")
    st_view(p,.,"price")
    st_view(xi,.,"xi")
    y  = rnormal(1,ns,35000,45000)
    term = J(rows(d),ns,.)
    while (norm(log(s)-log(sig),2) > 0.01) {
        for (i=1;i<=ns;i++) {
            term[.,i] = exp(d - 1/exp(y[1,i])*p+X*beta')
            term[.,i] = 1/(1+quadsum(term[.,i]))*term[.,i]
        }
        sig = mean(term')
        d[.,.]=d[.,.] + log(s[.,.])-log(sig[.,.])
    }
    xi = d-X*beta'
}
mata mosave fingerscrossed(), replace
end

Finally, the moment evaluator function itself (in a separate msm.ado file):

program msm
    version 11
    syntax varlist [if], at(name)
    quietly {
        cap drop deltastar sigma xi
        gen deltastar = 0
        gen sigma = 0
        gen xi = 0
        matrix beta = `at'
        mata:
            beta = st_matrix("beta")
            fingerscrossed(beta)
        end
        replace `varlist' = xi `if'
    }
end
*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index