Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

r - Model prediction using raster output issue

I am trying to predict an outcome over a spatial extent using a raster, similar to what is described in the section on model prediction here

The model is structured as follows

m1 <- glm(wb_bin ~ data1$evi_sc, family="binomial", data=data1)

My raster stack for the predictor variable:

evi_sc1 <- raster("prediction_rasters/evi_sc.tif")
predras <- stack(evi_sc1)
plot(predras)

the raster I'm using to predict

However, when I predict as follows, I receive a numeric prediction instead of my intended prediction map, and the error which follows.

p <- predict(predras, m1)
Error in p[-naind, ] <- predv : 
  number of items to replace is not a multiple of replacement length
In addition: Warning message:
'newdata' had 854 rows but variables found have 516 rows 

str(p)
num [1:516] 1 0 0 1 0 1 1 0 0 0 ...

I have double checked and mydata variables all contain 516 observations, so I am unsure where the 854 rows error is coming from.

Am I missing a step where I should be inputting the spatial extent of my model, so that the raster prediction and the model can be linked?

DATA: 
dput(data1$wb_bin)
c(1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 
0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 
0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 
1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 
0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 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62, 
0.51, 0.45, 0.6, 0.49, 0.59, 0.58, 0.57, 0.57, 0.64, 0.58, 0.48, 
0.64, 0.62, 0.61, 0.53, 0.73, 0.73, 0.73, 0.69, 0.68, 0.68, 0.68, 
0.68, 0.63, 0.64, 0.59, 0.73, 0.63, 0.63, 0.63, 0.63, 0.54, 0.54, 
0.56, 0.57, 0.63, 0.63, 0.63, 0.63, 0.71, 0.57, 0.55, 0.49, 0.54, 
0.7, 0.67, 0.68, 0.68, 0.66, 0.66, 0.65, 0.55, 0.67, 0.55, 0.57, 
0.57, 0.62, 0.62, 0.62, 0.61, 0.61, 0.61, 0.6, 0.62, 0.62, 0.6, 
0.53, 0.53, 0.5, 0.53, 0.53, 0.54, 0.54, 0.55, 0.55, 0.54, 0.53, 
0.53, 0.53, 0.54, 0.51, 0.51, 0.53, 0.5, 0.5, 0.53, 0.53, 0.53, 
0.52, 0.56, 0.56, 0.56, 0.56, 0.56, 0.55, 0.55, 0.55, 0.55, 0.52, 
0.52, 0.54, 0.53, 0.55, 0.52, 0.55, 0.55, 0.55, 0.56, 0.56, 0.56, 
0.56, 0.52, 0.52, 0.52, 0.52, 0.5, 0.5, 0.54, 0.51, 0.51, 0.5, 
0.5, 0.5, 0.53, 0.53, 0.53, 0.53, 0.53, 0.51, 0.51, 0.51, 0.52, 
0.56, 0.52, 0.52, 0.48, 0.48, 0.5, 0.52, 0.52, 0.52, 0.5, 0.5, 
0.5, 0.51, 0.53, 0.54, 0.47, 0.5, 0.54, 0.55, 0.5, 0.5, 0.65, 
0.65, 0.65, 0.65, 0.65, 0.66, 0.66, 0.66, 0.66, 0.65, 0.65, 0.65, 
0.65, 0.66, 0.66, 0.65, 0.65, 0.71, 0.59, 0.66, 0.66, 0.53, 0.58, 
0.65, 0.65, 0.66, 0.66, 0.55, 0.55, 0.62, 0.62, 0.58, 0.58, 0.53, 
0.57, 0.6, 0.58, 0.56, 0.64, 0.59, 0.56, 0.56, 0.56, 0.61, 0.61, 
0.61, 0.64, 0.6, 0.6, 0.57, 0.57, 0.61, 0.61, 0.61, 0.57, 0.57, 
0.54, 0.54, 0.54, 0.58, 0.58, 0.58, 0.59, 0.59, 0.6, 0.6, 0.6, 
0.57, 0.61, 0.56, 0.56, 0.51, 0.53, 0.53, 0.55, 0.55, 0.55, 0.55, 
0.57, 0.59, 0.57, 0.58, 0.58, 0.58, 0.57, 0.55, 0.6, 0.56, 0.61, 
0.61, 0.57, 0.61, 0.62, 0.62, 0.59, 0.6, 0.61, 0.58, 0.58, 0.63, 
0.63, 0.69, 0.69, 0.69, 0.6, 0.6, 0.6, 0.6, 0.66, 0.66, 0.66, 
0.64, 0.64, 0.64, 0.64, 0.65, 0.65, 0.65, 0.56, 0.56, 0.63, 0.63, 
0.63, 0.67, 0.68, 0.6, 0.66, 0.66, 0.66, 0.67, 0.67, 0.65, 0.67, 
0.6, 0.62, 0.62, 0.66, 0.62, 0.6, 0.5, 0.5, 0.5, 0.55, 0.55, 
0.55, 0.55, 0.55, 0.55, 0.5, 0.5, 0.51, 0.51, 0.52, 0.52, 0.52, 
0.52, 0.53, 0.53, 0.52, 0.52, 0.55, 0.55, 0.55, 0.55, 0.52, 0.52, 
0.52, 0.57, 0.57, 0.5, 0.53, 0.53, 0.52, 0.52, 0.52, 0.51, 0.49, 
0.52, 0.52, 0.52, 0.49, 0.55, 0.55, 0.56, 0.56, 0.55, 0.55, 0.53, 
0.59, 0.55, 0.58, 0.52, 0.51, 0.51, 0.51, 0.49, 0.49, 0.5, 0.49, 
0.49, 0.53, 0.53, 0.49, 0.49, 0.47, 0.47, 0.45, 0.45, 0.45, 0.5, 
0.6, 0.6, 0.6, 0.52, 0.63, 0.64, 0.64, 0.64, 0.64, 0.6, 0.49, 
0.5, 0.53, 0.56, 0.5, 0.49, 0.5, 0.57, 0.57, 0.54, 0.63, 0.55, 
0.47, 0.47, 0.67, 0.49, 0.48, 0.52, 0.61, 0.54, 0.54, 0.54, 0.55, 
0.66, 0.46, 0.46, 0.5, 0.5, 0.54, 0.54, 0.54, 0.52, 0.54, 0.53, 
0.53, 0.54, 0.56, 0.56, 0.55, 0.55, 0.52, 0.5, 0.52, 0.55, 0.44, 
0.56, 0.58, 0.54, 0.55, 0.53, 0.55, 0.52, 0.55, 0.59, 0.55, 0.55
)
question from:https://stackoverflow.com/questions/65928680/model-prediction-using-raster-output-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...