The naive solution is really very slow (at least a few minutes - I'm not patient enough):
library(zoo) n <- 2e5 k <- 30 z <- rnorm(n) x <- rnorm(k)
You can calculate the correlation manually, starting from the first moments and comments, but it will take several minutes.
y <- zoo(rnorm(n), 1:n) x <- rnorm(k) exy <- exx <- eyy <- ex <- ey <- zoo( rep(0,n), 1:n ) for(i in 1:k) { cat(i, "\n") exy <- exy + lag(y,i-1) * x[i] ey <- ey + lag(y,i-1) eyy <- eyy + lag(y,i-1)^2 ex <- ex + x[i]
Once you have the time series of correlations, it is easy to extract the position of the top 300.
i <- order(corxy, decreasing=TRUE)[1:300] corxy[i]
Vincent zoonekynd
source share