Home > Software design >  How to calculate correlation(r) between two raster datasets in R?
How to calculate correlation(r) between two raster datasets in R?

Time:05-15

I have two (NPP_BEGA and ENACT_BEGA) stacked raster dataset. They have the same spatial resolution, extent and dimension. I want to run and map the correlation(r) and P-values between these two raster datasets.

NPP_BEGA
class      : RasterBrick 
dimensions : 321, 401, 128721, 37  (nrow, ncol, ncell, nlayers)
resolution : 0.0375, 0.0375  (x, y)
extent     : 32.98125, 48.01875, 2.98125, 15.01875  (xmin, xmax, ymin, ymax)
crs        :  proj=longlat  datum=WGS84  no_defs 
source     : NPP_BEGA_RES.tif 
names      : NPP_BEGA_RES.1, NPP_BEGA_RES.2, NPP_BEGA_RES.3, NPP_BEGA_RES.4, 
NPP_BEGA_RES.5, NPP_BEGA_RES.6, NPP_BEGA_RES.7, NPP_BEGA_RES.8, NPP_BEGA_RES.9, 
NPP_BEGA_RES.10, NPP_BEGA_RES.11, NPP_BEGA_RES.12, NPP_BEGA_RES.13, NPP_BEGA_RES.14, 
NPP_BEGA_RES.15, ... 
min values :              0,              0,              0,              0,                           
max values :          90.21,          87.39,          91.44,          91.77,                    


ENACT_BEGA
class      : RasterBrick 
dimensions : 321, 401, 128721, 37  (nrow, ncol, ncell, nlayers)
resolution : 0.0375, 0.0375  (x, y)
extent     : 32.98125, 48.01875, 2.98125, 15.01875  (xmin, xmax, ymin, ymax)
crs        :  proj=longlat  datum=WGS84  no_defs 
source     : ENACT_BEGA_RES.tif 
names      : ENACT_BEGA_RES.1, ENACT_BEGA_RES.2, ENACT_BEGA_RES.3, ENACT_BEGA_RES.4, 
ENACT_BEGA_RES.5, ENACT_BEGA_RES.6, ENACT_BEGA_RES.7, ENACT_BEGA_RES.8, 
ENACT_BEGA_RES.9, ENACT_BEGA_RES.10, ENACT_BEGA_RES.11, ENACT_BEGA_RES.12, 
ENACT_BEGA_RES.13, ENACT_BEGA_RES.14, ENACT_BEGA_RES.15, ... 
min values :                0,                0,                0,                0,                
max values :            923.8,            923.8,            491.7,            512.1,  

This is what i tried

library(raster)
library(sp)
ENACT_BEGA<-brick("ENACT_BEGA_RES.tif")
NPP_BEGA<-brick("NPP_BEGA_RES.tif")
Cor<-corLocal(ENACT_BEGA,NPP_BEGA, test = T)

and found

There were 50 or more warnings (use warnings() to see the first 50)
1: In cor(x, y) : the standard deviation is zero
2: In cor(x, y) : the standard deviation is zero
50: In cor(x, y) : the standard deviation is zero

What is wrong with the script and how can i correct this? Thank you so much!

CodePudding user response:

There is nothing wrong with the script. You get warnings, not errors. The warnings occur for cells when there is no standard deviation because all focal values are the same for at least one of the layers:

cor(c(0,0,0), c(1,2,3))
#[1] NA
#Warning message:
#In cor(c(0, 0, 0), c(1, 2, 3)) : the standard deviation is zero
  • Related