Skip to content
Prev 8100 / 15274 Next

Rolling Correlation Matrixes

On Sun, 19 Jun 2011, tonyp wrote:

            
It's not that messy but it may be more than you expected. Your 200x5 
series is transformed into a 196x25. The 196 is due to "losing" four 
observations with a five observation window. The 25 is due to the 5x5 
correlation matrix that you compute. But it would be sufficient to use 
something like

   mycor <- function(x) {
     rval <- cor(x)
     rval[lower.tri(rval)]
   }

and then to use FUN = mycor in your rollapply() call. Maybe you can also 
improve the labeling somewhat.
Maybe something like this?

   apply(pw, 2, function(x) mean(x <= tail(x, 1)))

hth,
Z