0

I have an relatively large array (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. 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.

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? For example, in a loop, the first round would calculate cor(myvector, myarray[,,1]).

I tried using apply(myarray, dim=3, cor), but I'm struggling to understand the results.

Thanks a lot in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MilloMarinE
  • 132
  • 8
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. We don't need your actual data, just something to test with. – MrFlick May 22 '18 at 21:36
  • myarray <- array(data = rnorm(10000), dim=c(10,10,10)) coef.df <- data.frame(corrcoef=rep(NA,10)) for(i in 1:dim(myarray)[1]){ coef.df$corrcoef[i] <- cor(myarray[1,1, ], myarray[i,i,]) } – MilloMarinE May 22 '18 at 21:50
  • That's the code for the example. I need to calculate the correlation coefficient for each vector of the third dimension of the array. Thank you. – MilloMarinE May 22 '18 at 21:51

1 Answers1

1
#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
Katia
  • 3,784
  • 1
  • 14
  • 27