Create some simple test rasters in R:
> m=matrix(1:9,3,3)
> m2 = matrix(c(9,2,3,4,1,5,6,8,7),3,3)
Then we can trivially compute the covariance between these matrices:
> cov(c(m),c(m2))
[1] 2.125
and I would wager doing the computation by hand would get the same result. What does layerStats
do?
> D = stack(raster(m),raster(m2))
> layerStats(D, "cov", na.rm=TRUE)
$covariance
layer.1 layer.2
layer.1 7.500 2.125
layer.2 2.125 7.500
There's the same 2.125 in the cross-correlation.
Now try with an NA in there:
> m[2,2]=NA
> cov(c(m),c(m2),use="complete.obs")
[1] 2.428571
> D = stack(raster(m),raster(m2))
> layerStats(D, "cov", na.rm=TRUE)
$covariance
layer.1 layer.2
layer.1 8.571429 2.428571
layer.2 2.428571 7.500000
Again, cov
agrees with layerStats
if we remove the NA
value.
So what does ArcGIS do in each of these situations? Save the D
, import to ArcGIS, and find out...
EDIT
The results from ArcGIS:
Raster m and m2:

Raster with an NA and m2:

[Now, the question is - Which way of calculating covariance between rasters is correct?]
R
?na.rm=TRUE
,N
is equal to(n - cellStats(r, stat = "countNA") - asSample)
. How ArcGIS manages the missing values?