Hi
I have to matrices t and y below. I want correlation of columns (a,d) (b,e)
and (c,f). The correlation should be for the first 3 rows of matrix t
against matrix y; and then the last 3 i.e a loop function.
I have a code I created below but its giving me an error, surprisingly the
the individual parts of the code give me the correct answers.
##########My code is
runs=2
Corrs=matrix(0, nrow=2,3)
for (i in 1:2)
+ {
+ index_start = 3*(i-1)+1 #replace 100 with days in a quater
+ index_end = 3*i #replace 100 with days in a quater
+ use_index = index_start:index_end
+ Corrs[i] =diag(cor(t[use_index,],y[use_index,]))
+ }
Warning messages:
1: In Corrs[i] = diag(cor(t[use_index, ], y[use_index, ])) :
number of items to replace is not a multiple of replacement length
2: In Corrs[i] = diag(cor(t[use_index, ], y[use_index, ])) :
number of items to replace is not a multiple of replacement length
Corrs
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 1 0 0
####Expected solution
[,1] [,2] [,3]
[1,] 1 -1 -0.9226129
[2,] 1 -1 -0.8934051
###individual parts of the loop are