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

st: RE: 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: RE: loop within loop
Date   Tue, 19 Oct 2004 09:09:46 +0100

This code does more work than is necessary. 
A better version is below. (Still not tested.) 

tokenize "a b c d e"
qui forval i = 1/4 { 
	local I = `i' + 1 
 	forval j = `I'/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' 
 			}
 		}
 	}
} 

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

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
> Sent: 18 October 2004 23:30
> To: statalist@hsphsun2.harvard.edu
> Subject: st: RE: Re: RE: loop within loop
> 
> 
> 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' 
> 			}
> 		}
> 	}
> } 
> 	

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