I'm fitting a random forest using the R package ranger to classify a raster image. The prediction function produces an error and hereafter I provide a reproducible example.
library(raster)
library(nnet)
library(ranger)
data(iris)
# put iris data into raster
r<-list()
for(i in 1:4){
r[[i]]<-raster(nrows=10, ncols=15)
r[[i]][]<-iris[,i]
}
r<-stack(r)
names(r)<-names(iris)[1:4]
# multinom (an example that works)
nn.model <- multinom(Species ~ ., data=iris, trace=F)
nn.pred<-predict(r,nn.model)
# ranger (doesn't work)
ranger.model<-ranger(Species ~ ., data=iris)
ranger.pred<-predict(r,ranger.model)
The error given is
Error in v[cells, ] <- predv : incorrect number of subscripts on matrix
although the error with my real data is
Error in p[-naind, ] <- predv : number of items to replace is not a multiple of replacement length
The only thing that crosses my mind is that the ranger.prediction object includes several elements other than the predictions of interest. Anyway, how ranger could be used to predict on a raster stack?