Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: ereturn post after mata


From   Christophe Kolodziejczyk <[email protected]>
To   [email protected]
Subject   Re: st: ereturn post after mata
Date   Sat, 10 Mar 2012 17:34:57 +0100

Dear Christoph,
Another approach to handle missing values is to create an indicator
called typically touse and use it to select the observations in your
view (see help st_view() or st_data()).

gen touse = keep if dv < . & iv < .

mata:

...

st_view(X= ., ., tokens("`xlist'"),"touse")

...
end

Bill Gould had a presentation in 2005 on Mata and is a good tutorial
for Mata in general and st_view() in particular.

It is not necessary to create the cons variable, you can directly
specify it when you use the cross() function like

cross(X,1,X,1)

Stata will compute the cross product with X and the constant.

See the help on cross() for more details and helpful examples.

You should also check whether your mails are sent in plain text, it
may explain why you cannot reply to statalist.

Best
Christophe



2012/3/9 Christoph Engel <[email protected]>:
> Dear Christophe,
>
> For some reason I do not understand, the list does not let me post this
> response to you, which is why I send them in private.
>
> Once more: thank you very much for your help!
>
> Christoph
>
>
>
>
> Dear Christophe,
>
> Sorry for not having found your response earlier. I must have missed it in
> the flurry of postings.
>
> Thank you very much for having tried out my code on the auto dataset. I now
> have had a few more trials and eventually found out two things mata
> requires. Maybe later users find it useful to know, which is why I respond
> here:
>
> it seems mata does not automatically find / define a constant. Rather,
> following Cameron/Trivedi's code, one should code
>
> gen cons = 1
> local xlist iv cons  // which is the new auxiliary variable
>
> the reason why my trial dataset had not worked well were indeed two missing
> values on the dependent variable. It worked, once I coded
>
> preserve
> keep if dv < . & iv < .
> [the previous code]
> restore
>
> Best
>
> Christoph Engel
>
> Am 3/8/2012 9:11 AM, schrieb Christophe Kolodziejczyk:
>
> Dear Christoph
> I've tried your code with the auto dataset had no problem getting the
> correct results (see the log file).
> So I would check the data first and look for the elements produced by
> your program which have missing values.
> If you get error code r(504) it is because b or V has missing values.
> Best
> Christophe
>
> ----------------------------------------------------------------------------------------------------------------------
>       name:  <unnamed>
>        log:  C:\Users\CK\Documents\try_reg_mata.log
>   log type:  text
>  opened on:   8 Mar 2012, 09:07:21
>
> .
> .
> . sysuse auto , clear
> (1978 Automobile Data)
>
> .
> . gen cons=1
>
> .
> . local y price
>
> . local xlist mpg cons
>
> . mata
> ------------------------------------------------- mata (type end to
> exit) --------------------------------------------
> :     st_view(y= ., ., "`y'")
>
> :     st_view(X= ., ., tokens("`xlist'"))
>
> :     XXinv = cholinv(cross(X,X))
>
> :     b = XXinv*cross(X,y)
>
> :     e = y - X*b
>
> :     n = rows(X)
>
> :     k = cols(X)
>
> :     s2 = (e'e)/(n-k)
>
> :     vdef = s2*XXinv
>
> :         b,vdef
>                   1              2              3
>     +----------------------------------------------+
>   1 |  -238.8943456     2817.13472   -59997.35566  |
>   2 |   11253.06066   -59997.35566    1370802.527  |
>     +----------------------------------------------+
>
> :     st_matrix("b", b')
>
> :     st_matrix("V", vdef)
>
> : end
> ----------------------------------------------------------------------------------------------------------------------
>
> .
> . matrix colnames b = `xlist'
>
> . matrix colnames V = `xlist'
>
> . matrix rownames V = `xlist'
>
> .
> . mat list b
>
> b[1,2]
>            mpg        cons
> r1  -238.89435   11253.061
>
> . mat list V
>
> symmetric V[2,2]
>              mpg        cons
>  mpg   2817.1347
> cons  -59997.356   1370802.5
>
> . ereturn post b V
>
> . ereturn display
> ------------------------------------------------------------------------------
>              |      Coef.   Std. Err.      z    P>|z|     [95% Conf.
> Interval]
> -------------+----------------------------------------------------------------
>          mpg |  -238.8943   53.07669    -4.50   0.000    -342.9227
> -134.866
>         cons |   11253.06   1170.813     9.61   0.000      8958.31
> 13547.81
> ------------------------------------------------------------------------------
>
> .
> .
> . reg price mpg
>
>       Source |       SS       df       MS              Number of obs =
> 74
> -------------+------------------------------           F(  1,    72) =
> 20.26
>        Model |   139449474     1   139449474           Prob > F      =
> 0.0000
>     Residual |   495615923    72  6883554.48           R-squared     =
> 0.2196
> -------------+------------------------------           Adj R-squared =
> 0.2087
>        Total |   635065396    73  8699525.97           Root MSE      =
> 2623.7
>
> ------------------------------------------------------------------------------
>        price |      Coef.   Std. Err.      t    P>|t|     [95% Conf.
> Interval]
> -------------+----------------------------------------------------------------
>          mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008
> -133.0879
>        _cons |   11253.06   1170.813     9.61   0.000     8919.088
> 13587.03
> ------------------------------------------------------------------------------
>
> .
> . log close
>       name:  <unnamed>
>        log:  C:\Users\CK\Documents\try_reg_mata.log
>   log type:  text
>  closed on:   8 Mar 2012, 09:07:22
> ----------------------------------------------------------------------------------------------------------------------
>
>
> .
> . matrix colnames b = `xlist'
>
> . matrix colnames V = `xlist'
>
> . matrix rownames V = `xlist'
>
> . ereturn post b V
>
> . ereturn display
> ------------------------------------------------------------------------------
>              |      Coef.   Std. Err.      z    P>|z|     [95% Conf.
> Interval]
> -------------+----------------------------------------------------------------
>          mpg |  -238.8943   53.07669    -4.50   0.000    -342.9227
> -134.866
>         cons |   11253.06   1170.813     9.61   0.000      8958.31
> 13547.81
> ------------------------------------------------------------------------------
>
> .
> .
> . reg price mpg
>
>       Source |       SS       df       MS              Number of obs =
> 74
> -------------+------------------------------           F(  1,    72) =
> 20.26
>        Model |   139449474     1   139449474           Prob > F      =
> 0.0000
>     Residual |   495615923    72  6883554.48           R-squared     =
> 0.2196
> -------------+------------------------------           Adj R-squared =
> 0.2087
>        Total |   635065396    73  8699525.97           Root MSE      =
> 2623.7
>
> ------------------------------------------------------------------------------
>        price |      Coef.   Std. Err.      t    P>|t|     [95% Conf.
> Interval]
> -------------+----------------------------------------------------------------
>          mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008
> -133.0879
>        _cons |   11253.06   1170.813     9.61   0.000     8919.088
> 13587.03
> ------------------------------------------------------------------------------
>
> .
> . log close
>       name:  <unnamed>
>        log:  C:\Users\CK\Documents\try_reg_mata.log
>   log type:  text
>  closed on:   8 Mar 2012, 09:04:07
> ----------------------------------------------------------------------------------------------------------------------
>
>
> 2012/3/6 Christoph Engel <[email protected]>:
>
> Dear readers of Stata list,
>
> Cameron Trivedi: Microeconometrics Using Stata 2009, p. 116 report the code
> for running OLS in mata, and posting the results in stata.
>
> If I simplify the code and estimate a model with only one regressor and no
> constant, it works just fine. But if I add the constant, I still get
> coefficients and a variance/covariance matrix that are the same as the ones
> generated by reg. But I can no longer display the results conveniently. The
> following step does not work
>
> ereturn post b V
>
> I get the error code: r(504): matrix has missing values
>
> The link to r(504) says that this should no longer be an issue. Actually, I
> can use matrix list e(V) and get the same VCE as with reg. Am I making a
> mistake, or is this a bug that Stata Corp should fix?
>
> Here is my code which, I think, is exactly what Cameron Trivedi propose:
>
> local y dv
> local xlist iv cons
> mata
>    st_view(y= ., ., "`y'")
>    st_view(X= ., ., tokens("`xlist'"))
>    XXinv = cholinv(cross(X,X))
>    b = XXinv*cross(X,y)
>    e = y - X*b
>    n = rows(X)
>    k = cols(X)
>    s2 = (e'e)/(n-k)
>    vdef = s2*XXinv
>    st_matrix("b", b')
>    st_matrix("V", vdef)
> end
>
> matrix colnames b = `xlist'
> matrix colnames V = `xlist'
> matrix rownames V = `xlist'
> ereturn post b V
> ereturn display
>
>
> Thank you for any help
>
> Christoph Engel
>
> --
>
>
>
> --
> _________________________________________________________________
> Prof. Dr. Christoph Engel
> Max-Planck-Institut zur
> Erforschung von Gemeinschaftsgütern
> Max Planck Institute for Research on Collective Goods
> Kurt-Schumacher-Strasse 10
> D 53113 Bonn
> Tel. +49/228/91416-10
> Fax +49/228/91416-11
> e-mail:[email protected]
> http://www.coll.mpg.de
> http://www.coll.mpg.de/engel.html
> http://ideas.repec.org/e/pen22.html
> http://papers.ssrn.com/sol3/cf_dev/AbsByAuth.cfm?per_id=251559
> _________________________________________________________________



-- 
Christophe Kolodziejczyk
Research Fellow

AKF, Anvendt KommunalForskning
Danish Institute of Governmental Research
Købmagergade 22
DK-1150 København K

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index