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
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).