# st: correlations by month

 From Christopher F Baum To statalist@hsphsun2.harvard.edu Subject st: correlations by month Date Wed, 20 Oct 2004 21:09:07 -0400

Nick wrote

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'
}
}
}
}

This to me is not the right way to go, as if we change from 5 to 6 variables, we have to do a lot of futzing around, likewise for the dates. And loops nested 4x offends the sensibilities...I do think that mat accum along with a couple of Nick's cute tricks does a lot nicer job. Admittedly it generates a bunch of correlations that are 1 (the diagonal) but the axiom of free disposal applies...

Assume we have a variable ym that is a Stata date variable, counting months, and a bunch of variables to correlate that share a prefix. Then

su ym,meanonly
local min r(min)
forv i=`r(min)'/`r(max)' {
qui mat accum A=mPU* if ym==`i', dev noc
mat C=corr(A)
matvech C rowC
mat rowC = `i',rowC'
mat Rho =nullmat(Rho)\rowC
}
svmat Rho
format Rho1 %tm
list Rho* in 1/3

Gives us

+----------------------------------------------------------------------- ----------+
1. | Rho1 | Rho2 | Rho3 | Rho4 | Rho5 | Rho6 | Rho7 | Rho8 |
| 1947m1 | 1 | .9987901 | .9993901 | .9946929 | -.9789948 | 1 | .9998982 |
|------------------------------------------------------------- +-------------------|
| Rho9 | Rho10 | Rho11 | Rho12 | Rho13 | Rho14 | Rho15 | Rho16 |
| .9884297 | -.9677839 | 1 | .9904932 | -.9712778 | 1 | -.9947766 | 1 |
+----------------------------------------------------------------------- ----------+

+----------------------------------------------------------------------- ----------+
2. | Rho1 | Rho2 | Rho3 | Rho4 | Rho5 | Rho6 | Rho7 | Rho8 |
| 1947m2 | 1 | .999765 | .9999766 | .9998903 | -.9960635 | 1 | .9998899 |
|------------------------------------------------------------- +-------------------|
| Rho9 | Rho10 | Rho11 | Rho12 | Rho13 | Rho14 | Rho15 | Rho16 |
| .9993343 | -.9939078 | 1 | .9997657 | -.9954342 | 1 | -.9972671 | 1 |
+----------------------------------------------------------------------- ----------+

+----------------------------------------------------------------------- ----------+
3. | Rho1 | Rho2 | Rho3 | Rho4 | Rho5 | Rho6 | Rho7 | Rho8 |
| 1947m3 | 1 | .9997288 | .9999117 | .9994376 | -.9961429 | 1 | .99995 |
|------------------------------------------------------------- +-------------------|
| Rho9 | Rho10 | Rho11 | Rho12 | Rho13 | Rho14 | Rho15 | Rho16 |
| .9983857 | -.9938293 | 1 | .9989038 | -.994889 | 1 | -.998525 | 1 |
+----------------------------------------------------------------------- ----------+

Here the 'rho1' is the month for which correlations are calculated, rho3-6 are the first column of the correlation matrix (sub-diagonal), rho 8-10 the second column, rho12-13 the third column, rho15 the fourth column (i.e. the correlation between the 4th and 5th variable which Nick calls de).

Kit Baum, Boston College Economics baum@bc.edu
http://ideas.repec.org/e/pba1.html

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