Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: RE: loop within loop


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: RE: loop within loop
Date   Mon, 18 Oct 2004 23:29:31 +0100

I am less clear now on what your real problem is. 

But being interested in several variables doesn't 
make the issues any different. If you 
want to put correlations into variables, you 
must still operate with one correlation from 
each pair of variables. -matrix accum- looks to me 
like a poor choice of method. You end up 
putting stuff into a matrix and then need 
to get it out again, a bit like going to Houston 
from Dallas via San Antonio, perhaps instructive 
but nevertheless indirect. As you are cycling 
over years and months, you need to work with 
several matrices, which makes the problem yet
more complicated. 

If I had a varlist a b c d e, there are 
ten distinct correlations which we can denote
ab ac ad ae bc bd be cd ce de. 

So I can go 

foreach v in ab ac ad ae bc bd be cd ce de { 
	gen `v' = . 
} 

and then some code to get all the correlations 
from all pairs of variables, all years and months
might look like this, but this isn't tested: 

tokenize "a b c d e"
qui forval i = 1/4 { 
	forval j = 2/5 { 
		forval y = 1988/2003 { 
			forval m = 1/12 { 
				corr ``i'' ``j'' if year == `y' & month == `m' 
				replace ``i''``j'' = r(rho) if year == `y' & month == `m' 
			}
		}
	}
} 
	
As you say nothing about what went wrong
as far as you are concerned in your previous 
code, I am not sure what you want me to explain. 
I can't see an obvious syntax error. 

As the FAQ advises, "Say exactly what you typed and exactly what 
Stata typed (or did) in response." 

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

Wang,Le
 
> I am sorry that I didn't describe my problem properly. I just 
> made up the data set.
> There are actually five variables "a, b, c, d, e" from Jan 
> 1988 to Dec 2003.
> Although I am aware that there is a -egen corr- availabe, I 
> just want to pracitise
> it a little bit since I am not quite familiar with matrix operation in
> STATA. Woud you
> please tell me where I am wrong when I am using matrix form 
> in my second post?
 
> ----- Original Message ----- 
> From: "Nick Cox" <n.j.cox@durham.ac.uk>
> To: <statalist@hsphsun2.harvard.edu>
> Sent: Monday, October 18, 2004 3:10 AM
> Subject: st: RE: loop within loop
> 
> 
> > If you want the correlation between a and b, what
> > c d e are doing is unnecessary information. A more
> > direct approach is
> >
> > gen corrab = .
> > qui forval i = 1988(1)2003 {
> >      forval j = 1/12
> > corr a b if year == `i' & month == `j'
> > replace corrab = r(rho) if year == `i' &  month == `j'
> >      }
> > }
> >
> > In your example, each correlation is based on just 3 data points.
> > Perhaps you are obliged to do that, but I wouldn't want to.
> >
> > Also, note Nick Winter's -egen, corr()- within -egenmore- on SSC.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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