I have a data frame with daily data in R (148 columns by 6230 rows). I want to find the correlations coefficients using sliding windows with length of 600 (days) with windows displacement of 5 (days) and trying to generate 1220 correlation matrices (approx.). All the examples that I saw used only one information vector. There exist an easy way to find those correlation matrices using sliding window? I'll appreciate any suggestion.
Asked
Active
Viewed 1,910 times
0
-
Welcome to Stack Overflow. Please read [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic) before posting. Questions asking for code should show some effort ("attempted solutions, why they didn't work, and the expected results") otherwise your question will downvoted and closed as off-topic. – zero323 Oct 28 '13 at 03:20
1 Answers
2
If M
is the input matrix then each row of out
is one correlation matrix strung out column by column:
library(zoo)
out <- rollapply(M, 600, by = 5, function(x) c(cor(x)), by.column = FALSE)
They could be reshaped into a list of correlation matrices, if need be:
L <- lapply(1:nrow(out), function(i) matrix(out[i, ], ncol(M)))
or as an array:
simplify2array(L)

G. Grothendieck
- 254,981
- 17
- 203
- 341