[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Wang,Le" <lew@mail.smu.edu> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Re: RE: loop within loop |

Date |
Mon, 18 Oct 2004 16:58:57 -0700 |

Thanks Nick, 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? Thanks a lot. Le ----- 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. > > Nick > n.j.cox@durham.ac.uk > > Wang,Le > > > I have a question on loop within loop. I have the following data set > > > > year month date a b c > > 88 1 1 .2 .5 .3 > > . 1 2 .3 .6 .4 > > . 1 3 .4 .7 .5 > > . 2 1 .1 .2 .7 > > . 2 2 .4 .5 .9 > > . 2 3 .6 .7 1 > > 89 1 1 .2 .5 .3 > > . 1 2 .3 .6 .4 > > . 1 3 .4 .7 .5 > > . 2 1 .1 .2 .7 > > . 2 2 .4 .5 .9 > > . 2 3 .6 .7 1 > > > > I could program the correlation by year as follows, > > > > g corrab = . > > > > forvalues i = 1988(1)2003{ > > matrix accum R`i' = a b c if year == `i', nocons dev > > matrix R`i' = corr(R`i') > > replace corrab = R`i'[2,1] if year == `i' & corrab == . > > } > > > > However, now I want to calculate the correlation between a > > and b by year > > and month. What should I do?? I tried the following, but it > > doesnot work. > > > > forvalues i = 1988(1)2003 { > > local j = 1 > > while `j' < 13{ > > matrix accum R`i'`j' = a b c d e if year == `i' & > > month == `j', > > nocons dev > > matrix R`i'`j' = corr(R`i'`j') > > replace corrab = R`i'`j'[1,1] if year == `i' & > > month == `j' & > > corrab == . > > local `j' = `j' + 1 > > } > > } > > > > * > * 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/ * * 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/

**References**:**st: RE: loop within loop***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: RE: "no observations" in kernel regression** - Next by Date:
**st: RE: Re: RE: loop within loop** - Previous by thread:
**st: RE: loop within loop** - Next by thread:
**RE: Macoreconometric dynamic panels (was: Re: st: panel autocorrelation)** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |