[英]How to slice and calculate the pearson correlation coefficient between one big and small array with "overlapping" windows arrays
[英]How to I calculate the correlation coefficient of matrices inside an array?
我有一个相对较大的阵列(242x240x2922)。 前两个维度是纬度和经度,第三个维度是时间(每日卫星图像)。
我需要提取该数组的子集的相关系数,该子集对应于(lon,lat)对中每对的6°半径内的数据。 首先,我创建了一个循环,用于计算每个纬度对,纬度对的圆多边形。 然后,我检查了圆内的哪些点(使用point.in.polygon函数),并提取了较大数组的子集。
我知道我可以构建第二个嵌套循环,该循环可以计算每个lon的时间序列与“子数组”的其余时间序列(落入圆内的其余时间)之间的相关性,但这将需要太长了...是否有任何简单的方法来计算大小为“ L”的向量与具有NxMxL维的数组的每个向量的相关系数? 例如,在循环中,第一轮将计算cor(myvector,myarray [,, 1])。
我尝试使用apply(myarray,dim = 3,cor),但是我在努力理解结果。
非常感谢。
#define dimensions
M = 3; N = 4; L = 5
myarray <- array(data = rnorm(M*N*L), dim=c(M,N,L))
myvector <- myarray[1,1, ]
# Use apply function to cycle through all the vectors in 3rd dimension:
result <- apply(myarray, c(1,2), FUN=function(x)cor(myvector,x))
result
# [,1] [,2] [,3] [,4]
#[1,] 1.00000000 0.73804476 0.7356366 -0.1583484
#[2,] 0.03820936 -0.07797187 0.3798744 -0.4925700
#[3,] -0.52827708 -0.09036006 0.1895361 -0.2860481
# For testing compare with the loop result (which will be much slower for larger arrays):
for (i in 1:dim(myarray)[1])
for (j in 1:dim(myarray)[2])
print( cor(myvector,myarray[i,j,]))
# [1] 1
# [1] 0.7380448
# [1] 0.7356366
# [1] -0.1583484
# [1] 0.03820936
# [1] -0.07797187
# [1] 0.3798744
# [1] -0.49257
# [1] -0.5282771
# [1] -0.09036006
# [1] 0.1895361
# [1] -0.2860481
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.