# st: RE: correlations by month

 From "Nick Cox" <[email protected]> To <[email protected]> Subject st: RE: correlations by month Date Thu, 21 Oct 2004 10:54:39 +0100

```Note that for this to work you would also
need to install -matvech- from the STB-56 files.

Nick
[email protected]

Christopher F Baum

> 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

< snip >

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

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