Reputation: 431
I have two rasters, and I would like to see a corelation between the two, and obtain a r2.
TOTAL2
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : layer
values : 0, 400 (min, max)
> lpjENLF$VegCX2X0.7
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : VegCX2X0.7
values : 1.874989e-05, 350 (min, max)
How can I compute an r2 value between these two rasters? I have tried to convert both of the rasters into dataframes, but both of the dataframes return as NA
. Then I apply, na.rm=T
and try to find the r2 but the length of the dataframes for both rasters become different. The second solution I tried was stacking both of the rasters and applying this code:
layerStats(rasterstack,'pearson')
however I obtain :
$`pearson correlation coefficient`
VegCX2X0.7 layer
VegCX2X0.7 NA NA
layer NA NA
$mean
VegCX2X0.7 layer
NA NA
Upvotes: 0
Views: 243
Reputation: 7689
Option 1: You can use na.rm
in layerStats
layerStats(rasterstack, 'pearson', na.rm=T)
Option 2: You can first extract the values from the raster objects and apply the build in function cor
. With this function you should add the argument use="complete.obs" to get it working with
NA` values.
cor(values(TOTAL2), values(lpjENLF$VegCX2X0.7), use="complete.obs", method = 'pearson')
Upvotes: 2