簡體   English   中英

使用光柵堆棧/磚塊進行預測 - 返回“對象不是矩陣”

[英]Predicting using raster stack/brick - returns "object is not a matrix"

我創建了一個柵格堆棧,並希望使用它來預測基於 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)

該模型是二元邏輯回歸。 我已經設法在同一模型上使用另一個柵格堆棧來使用 predict 函數,但沒有使用“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" 

在此先感謝您的幫助。

這是一個有效的可重現示例。 你能通過更密切地關注你所做的事情來打破它嗎? 並表明使用這些數據?

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM