I doubt its any faster than using a loop but probably less code is:

library(zoo)
z1 <- zoo(t(mat1)); z2 <- zoo(t(mat2))
idx <- 1:ncol(z1)
out <- rollapply(cbind(z1, z2), 11, by.column = FALSE,
    FUN = function(x) cor(x[,idx],x[,-idx]))

will give you a 10 x 16 multivariate zoo series such that:

out[1, ] is c(cor(t(mat1[,1:11]), t(mat2[,1:11])))
out[2, ] is c(cor(t(mat1[,2:12]), t(mat2[,2:12])))
etc.

and t(out) is a matrix in the orientation you asked for.

Try

library(zoo)
vignette("zoo")

for an intro to zoo.


On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> Hi all,
>
>
>
> Situation:
>
>
>
>  - I have two matrices each w/ 4 rows and 20 columns.
>
>
>
> mat1 <- matrix(sample(1:500,80), ncol = 20,
>
>            dimnames=list(paste("mat1row", 1:4, sep=""),
>
>            paste("mat1col", 1:20, sep="")))
>
>
>
> mat2 <- matrix(sample(501:1000,80), ncol = 20,
>
>            dimnames=list(paste("mat2row", 1:4, sep=""),
>
>            paste("mat2col", 1:20, sep="")))
>
>
>
>  - Each column represents a value in a time series.
>
>
>
> Q: What do I want:
>
>
>
>   Calculate moving average correlations for each row x row pair:
>
>
>
>   For each row x row pair I want 10 values representing moving average
>
>   correlations for 10 sets of time-values:
>
>
>
>   cor(mat1[1,1:10], mat2[1,1:10])
>
>   cor(mat1[1,2:11], mat2[1,2:11])
>
>   ...
>
>   cor(mat1[1,11:20], mat2[1,11:20])
>
>   cor(mat1[1,1:10], mat2[2,1:10])
>
>   ...
>
>   cor(mat1[4,11:20], mat2[4,11:20])
>
>
>
>   Result would be a 16 (rows) x 10 (col) matrix matMA
>
>
>
>      ma1, ma2, ..., ma10 for (mat1 row1) x (mat2 row1)
>
>      ma1, ma2, ..., ma10 for (mat1 row1) x (mat2 row2)
>
>      ...
>
>      ma1, ma2, ..., ma10 for (mat1 row4) x (mat2 row3)
>
>      ma1, ma2, ..., ma10 for (mat1 row4) x (mat2 row4)
>
>
>
>   I would like to be able to do this without using a for loop
>
>   due to the slowness of that method.
>
>
>
>   Is it possible to iterate through subsets w/o using a for loop?
>
>
>
> Thanks,
>
>
>
> - Bruce
>
>
>
>      P
>
>
>
>
> **********************************************************************
> Please be aware that, notwithstanding the fact that the pers...{{dropped}}
>
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to