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]

st: RE: looping in MATA


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: looping in MATA
Date   Thu, 29 Apr 2010 15:08:33 +0100

The key principle here is that local macro references are not part of
Mata. So, they must be interpreted before Mata sees them. Calling Mata
line by line is a little clumsy, but will work, e.g. 

mata: x`y' = st_data(., (" g`y'", " g`y+1'", " g`y+2'"," g`y+3'", "
g`y+4'"), 0)

However, you must get the local macro syntax right too. `y+1', etc.
won't work as you intend. You need `=`y'+1', etc. 

Nick 
n.j.cox@durham.ac.uk 

Olga Gorbachev

I am a beginner in Mata language, but I need to do something, that to
me sounds complicated:

I need to create a rolling loop in stata mata language, I am using
some simple matrix algebra and it is easier to do in Mata than in
matrix functions.

I need to do something like this (within a do file):
*****************************************
mata

foreach y in 1970/2000 {

x`y' = st_data(., (" g`y'", " g`y+1'", " g`y+2'"," g`y+3'", " g`y+4'"),
0)

r=rows(x`y')
vc=x`y'*x`y''
vc=vc/25
var`y'=sqrt(trace(vc)/rows(vc))
tr=vc-diag(vc)
cov`y'=sum(tr)
cov`y'=sqrt(cov`y'/(r^2-r)/2)
}

end
*************************

Ideally I would also like to save these cov`y' and var`y' data into a
metrix.


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