我在r中有三个栅格
> lpjre
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 : agbipsl
values : 0, 20.86364 (min, max)
> gcre
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, 31.72 (min, max)
和土地覆盖物栅格
> tif4
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 : C:/Users/Halima Usman/Documents/ICIMOD/landusemaskedme.tif
names : landusemaskedme
values : 1, 12 (min, max)
attributes :
ID zn
from: 1 evergreen needleleaf forest
to : 12 croplands
根据不同的土地覆盖类别,我在lpjre
和gcre
之间绘制了一个散点图>
plot(lpjre[tif4==2],gcre[tif4==2])
我想找到如何根据土地覆盖类型计算
lpjre
和gcre
之间的r2值?
我尝试此代码并给出错误:
> cor(values(lpjre)[tif4==1], values(gcre)[tif4==1], use="complete.obs", method = 'pearson') Error in values(gcre)[tif4 == 1] : invalid subscript type 'S4'
可复制栅格:
library(raster)
ras1 <- raster(matrix(c(1,1,1,2,2,2)))
ras2 <- raster(matrix(c(1,1,1,2,2,2)))
#Generating landcover example data
raster2 <- raster(matrix(c(1,1,1,2,2,2,3,3,3),ncol =3))
raster2 <- as.factor(raster2)
rat <- levels(raster2 )[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers")
levels(raster2 ) <- rat
我在r中有三个栅格> lpjre类:RasterLayer维度:2803、5303、14864309(nrow,ncol,ncell)分辨率:0.008333333、0.008333333(x,y)范围:60.85、105.0417、15 ....
您可以使用向量转换。首先是一个可重现的示例: