Statalist


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

RE: st: coefficients lost when mata code placed in eclass program


From   "Nelson, Carl" <chnelson@illinois.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: coefficients lost when mata code placed in eclass program
Date   Mon, 17 Aug 2009 14:19:56 -0500

Austin,

Thanks for you quick response. The suggested rewrite did not change the loss of 17 coefficients. But the local comment is spot on. The problem was
solved by changing the macro to a global macro and replacing st_local with st_global in the mata code.
I am still learning the syntax explained by Bill Gould in his mata matters article of SJ 8-3.

Carl
________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] On Behalf Of Austin Nichols [austinnichols@gmail.com]
Sent: Monday, August 17, 2009 2:07 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: coefficients lost when mata code placed in eclass program

locals are just that: local.
Rewrite
        st_view(x1, .,tokens(st_local(x1name)))
as
        st_view(x1, .,tokens(x1name))
and
mata: gmmrsk("share_riskyassets_broad","risktol_m risktol_f","wlt
wlt2","x1name")
as
mata: gmmrsk("share_riskyassets_broad","risktol_m risktol_f","wlt
wlt2","`x1name'")
and make sure `x1name' contains your list when you call the program.

On Mon, Aug 17, 2009 at 2:31 PM, Nelson, Carl<chnelson@illinois.edu> wrote:
> I have the following mata program to estimate parameters by exactly identified gmm
> mata: mata clear
> mata:
>        mata set matastrict on
>        void gmmrsk(string scalar yname, string scalar hname, ///
>        string scalar x2name, string scalar x1name)
> {
>        real colvector y, coeff, coeffreduce, coeffstruc, c1, c2, c
>        real matrix h, x1, x2, xx, b11, b12, b21, b22, b
>        real scalar samvar, lambda
>
>        y = st_data(.,yname)
>        h = st_data(.,tokens(hname))
>        x2 = st_data(., tokens(x2name))
>        st_view(x1, .,tokens(st_local(x1name)))
>        lambda = 106.1196
>
> // construct matrices for parameter estimation
> // pieces are constructed and put together
> // coefficient estimated
> // first collect x into a single matrix
>
> xx = x2, x1
>
> // form sub-matrices of cross product matrix
>
> b11 = h'h
> b12 = h'xx
> b21 = lambda*(xx'h)
> b22 = xx'xx
> c1 = h'y
> c2 = xx'y
> c = c1 \ c2
> b = (b11 , b12) \ (b21, b22)
> coeff = luinv(b)*c
>
> // return coefficient vector to stata to bootstrap standard errors
>
> st_matrix("coeff",coeff)
>
> }
> end
>
> When I run this program with
>
> mata: gmmrsk("share_riskyassets_broad","risktol_m risktol_f","wlt wlt2","x1name")
> matrix dir
>    coeff[21,1]
> x1name is a local macro containing the names of 17 variables, so 21 is the right dimension for coeff
>
> When I place gmmrsk in an eclass program in order to bootstrap, as follows
>
> . // Bootstrap gmm estimation to get standard errors
> .
> . capt prog drop gmmbarg
>
> . prog def gmmbarg, eclass
>  1. mata: gmmrsk("share_riskyassets_broad","risktol_m risktol_f","wlt wlt2","x1name")
>  2. matrix b = coeff'
>  3. ereturn post b
>  4. ereturn local cmd = "bootstrap"
>  5. end
>
> . gmmbarg
>
> . matrix dir
>        coeff[4,1]
>
> I have lost 17 coefficients, and I don't understand why I lost them. What have I done wrong in the program gmmbarg?
>
> Thanks.
>
> Carl Nelson

*
*   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/



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