Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: ereturn post after mata


From   Christophe Kolodziejczyk <ck.statalist@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: ereturn post after mata
Date   Thu, 8 Mar 2012 09:11:42 +0100

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 <engel@coll.mpg.de>:
> 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:engel@coll.mpg.de
> 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
> _________________________________________________________________
>
> *
> *   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/



-- 
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index