簡體   English   中英

如何計算數組內矩陣的相關系數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM