[R-sig-Geo] Error in image classification (raster stack) with random forest (ranger) and GLM (glmnet)

Hugo Costa hugoagcosta at gmail.com
Tue Sep 26 22:48:52 CEST 2017


Dear list

I'm fitting some models to classify a raster image. The prediction function
produces an error for RF (package ranger) and a GLM (package glmnet).

The error given by RF is:
Error in v[cells, ] <- predv : incorrect number of subscripts on matrix
(see reproducible example at the end)

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 error given by GLM is:
Error in cbind2(1, newx) %*% (nbeta[[i]]) :
  not-yet-implemented method for <data.frame> %*% <dgCMatrix>

I'm interested in using these specific packages due to specific
circumstances of my work. Any solution to solve this issue would be highly
appreciated.
Thanks
Hugo

library(raster)
library(ranger)
library(glmnet)
library(nnet)
data(iris)

# put iris data into a raster stack
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]

# ranger
m1<-ranger(Species ~ ., data=iris)
p1<-predict(r,m1)

# glmnet
m2 <- glmnet(as.matrix(iris[,1:4]),iris[,5],family=c("multinomial"))
p2<-predict(r,m2,type="class",s=0)

# multinom (this works)
m3 <- multinom(Species ~ ., data=iris, trace=F)
p3<-predict(r,m3)
plot(p3)

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list