简体   繁体   English

如何计算数组内矩阵的相关系数?

[英]How to I calculate the correlation coefficient of matrices inside an array?

I have an relatively large array (242x240x2922). 我有一个相对较大的阵列(242x240x2922)。 The first two dimensions are latitude and longitude, and the third dimension is time (daily satellite images). 前两个维度是纬度和经度,第三个维度是时间(每日卫星图像)。

I need to extract the correlation coefficient for a subset of that array that corresponds to data within a 6° Radius of each one of the (lon, lat) pairs. 我需要提取该数组的子集的相关系数,该子集对应于(lon,lat)对中每对的6°半径内的数据。 First, I created a loop that calculates the circle polygon for each of the lon, lat pairs. 首先,我创建了一个循环,用于计算每个纬度对,纬度对的圆多边形。 Then, I checked which points are inside the circle (with the point.in.polygon function), and extracted the subset of the larger array. 然后,我检查了圆内的哪些点(使用point.in.polygon函数),并提取了较大数组的子集。

I know I could build a second, nested loop that can calculate the correlation of the time series of each lon, lat with the rest of the time series of the "sub array" (those that fall within the circle), but It would take too long... is there any straight forward way to calculate the correlation coefficient of a vector of size "L" with each of the vectors of an array that has NxMxL dimensions? 我知道我可以构建第二个嵌套循环,该循环可以计算每个lon的时间序列与“子数组”的其余时间序列(落入圆内的其余时间)之间的相关性,但这将需要太长了...是否有任何简单的方法来计算大小为“ L”的向量与具有NxMxL维的数组的每个向量的相关系数? For example, in a loop, the first round would calculate cor(myvector, myarray[,,1]). 例如,在循环中,第一轮将计算cor(myvector,myarray [,, 1])。

I tried using apply(myarray, dim=3, cor), but I'm struggling to understand the results. 我尝试使用apply(myarray,dim = 3,cor),但是我在努力理解结果。

Thanks a lot in advance. 非常感谢。

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM