使用栅格堆栈/砖块进行预测-返回“对象不是矩阵”

时间:2019-07-20 12:02:04

标签: r

我已经创建了栅格堆栈,并希望将其用于基于GLM模型预测要素。

#creating the raster /raster stack
Waterdis <- raster('waterdist.tif')
Shoredis <- raster('Shoredist.tif')
IceConcRaster<- raster('Iceconc.tif')
Bathy <- raster('bathtry.tif')

crs(Bathy) <- "+init=EPSG:4326"
plot(Bathy)

#Projecting to right coordinate system
Bathy_Proj <- projectRaster(Bathy, crs="+proj=stere +lat_0=90 +lat_ts=45 +lon_0=50 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
plot(Bathy_Proj)

#resampling to bathymetry raster
#Shore distance
ShoreRaster <- resample(Shoredis,Bathy_Proj, method="ngb")
#water distance
WaterRaster <- resample(Waterdis,Bathy_Proj, method="ngb")
#ice concentration 
IceConcRasterres <- resample(IceConcRaster,Bathy_Proj, method="ngb")

#stacking predictors 
predictors2pre <- stack(list(WaterRaster=WaterRaster, IceConcRasterres=IceConcRasterres, ShoreRaster=ShoreRaster,Bathy_Proj=Bathy_Proj))
plot(predictors2pre)

该模型是二进制逻辑回归。我设法在同一模型上通过另一个栅格堆栈使用了预测函数,但没有在“ predictors2pre”堆栈上使用

#the raster stack in question -   predictors2pre         
class      : RasterStack 
dimensions : 2856, 2466, 7042896, 4  (nrow, ncol, ncell, nlayers)
resolution : 353, 473  (x, y)
extent     : -397756.4, 472741.6, -5536805, -4185917  (xmin, xmax, ymin, ymax)
crs        : +proj=stere +lat_0=90 +lat_ts=45 +lon_0=50 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : /private/var/folders/qd/785_ds8j4fn0xphk7604q4f80000gn/T/Rtmpy9oiyR/raster/r_tmp_2019-07-20_123308_1328_18877.grd 
names      : WaterRaster, IceConcRasterres, ShoreRaster,  Bathy_Proj 
min values :   2209.7087,           0.0000,    179.3362, -13175.5049 
max values :  284091.562,          255.000,  184013.016,    2164.948



#Predicting features
M2ThickIpred.ALL <- predict(predictors2pre, M2ThickIce.ALL.Years, type='response')
The output returns this error  - 

#"Error in model.frame.default(Terms, newdata, na.action = na.action,
# xlev = object$xlevels) :
# 
#   object is not a matrix" 

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是一个可复制的示例,可以正常工作。您可以通过紧跟您所做的事情来打破它吗?并证明使用这些数据?

library(raster)
logo <- brick(system.file("external/rlogo.grd", package="raster"))
logo
#class      : RasterBrick 
#dimensions : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
#resolution : 1, 1  (x, y)
#extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#crs        : +proj=merc +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#source     : C:/soft/R/R-3.6.1/library/raster/external/rlogo.grd 
#names      : red, green, blue 
#min values :   0,     0,    0 
#max values : 255,   255,  255 

# presence and absence points
p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 
   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 
   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
   99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
   37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)

xy <- rbind(cbind(1, p), cbind(0, a))
v <- data.frame(cbind(pa=xy[,1], extract(logo, xy[,2:3])))

model <- glm(formula=pa~., data=v, family=binomial(link="logit"))
model

#Call:  glm(formula = pa ~ ., family = binomial(link = "logit"), data = v)
#Coefficients:
#(Intercept)          red        green         blue  
#   283.5023      -6.5323       4.5612       0.6848  
#Degrees of Freedom: 39 Total (i.e. Null);  36 Residual
#Null Deviance:      55.45 
#Residual Deviance: 2.711e-09    AIC: 8

r <- predict(logo, model, type="response")