Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Rolling window correlations for all possible pairs of variables


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Rolling window correlations for all possible pairs of variables
Date   Mon, 13 Aug 2012 12:48:44 +0200

On Mon, Aug 13, 2012 at 12:08 PM,  <[email protected]> wrote:
> I have a time series dataset consisting of more than several hundreds of variables, and roughly 1500 time points.
>
> I want to calculate rolling window correlations for each possible pairs of the variables, and store the results either in a new data set or generate new variables for each possible combination (for instance for var 1 and var2, it is rho1_2, for var1 and var3 it is rho1_3 and so on). Doing this manually is quite inefficient and time consuming.

*---------------------------- begin example -------------------------
// get and prepare some example data
webuse lutkepohl2, clear
tsset qtr

// collect the names of all variables except the time variable
ds qtr, not
local vars `r(varlist)'

// count the number of variables
local k_vars : word count `vars'

// store the first variable name in the local macro 1
// the second variable name in the local macro 2, etc.
tokenize `vars'

// I don't want to clutter my harddrive whith such example datasets
tempfile tofill temp orig
save `orig'

forvalues i = 1/`k_vars' {
	forvalues j = `=`i'+1'/`k_vars' {
		// get some informative info while waiting for the loop to finish
		di as txt "rolling correlation between " _c
		di as result "``i''" as txt " and " as result "``j''"
		quietly {
			if `i' == 1 & `j' == 2 {
				// note that the first time round `i' evaluates to 1,
				// which in turn is evaluated to the first variable name
				rolling r(rho), window(10) saving(`tofill'): ///
						corr ``i'' ``j''
				use `tofill', clear
				rename _stat_1 r_``i''_``j''
				save `tofill', replace
			}
			else {
				use `orig', clear
				rolling r(rho), window(10) saving(`temp', replace) : ///
						cor ``i'' ``j''
				use `temp', clear
				rename _stat_1 r_``i''_``j''
				merge 1:1 start using `tofill'
				assert _merge == 3
				drop _merge
				save `tofill', replace
			}
		}
	}
}

use `tofill'
*----------------------------- end example --------------------------
(For more on examples I sent to the Statalist see:
 http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index