Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

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


From   <A.C.Tuncer@lse.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Rolling window correlations for all possible pairs of variables
Date   Mon, 13 Aug 2012 12:30:46 +0100

Dear Maarten and stata list,
Many thanks for the prompt reply!
This is very helpful and works well, however I receive an error message when a pair of variables do not have any overlapping observations. Say var1 has observations in period 1900-1960, whereas var2 has observations in 1970-2000. Thus as it is not possible to calculate the corr, the calculations simply stop. Is there any way to force the do-file keep calculating?
Thank you very much,
Best,
Coskun

________________________________

From: owner-statalist@hsphsun2.harvard.edu on behalf of Maarten Buis
Sent: Mon 13/08/2012 11:48
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Rolling window correlations for all possible pairs of variables



On Mon, Aug 13, 2012 at 12:08 PM,  <A.C.Tuncer@lse.ac.uk> 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 <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/



Please access the attached hyperlink for an important electronic communications disclaimer: http://lse.ac.uk/emailDisclaimer

<<winmail.dat>>



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index