[R-sig-Geo] Error when predicting a randomForest model to a raster

Gregovich, Dave P (DFG) dave.gregovich at alaska.gov
Wed Jan 14 02:48:36 CET 2015


Hello,
Thanks kindly aforehand for any help you can lend. I am trying to perform a simple classification of coarse vegetation types. I am getting an error when trying to predict a randomForest model to a raster:
#Error in if (any(is.na(x))) stop("missing values in newdata") :
#  argument is not interpretable as logical

Here is some reproducible code if anyone happens to have the time to give it a shot:
_________________________________________________________________
library(raster)
library(randomForest)

#create random color band rasters and fill with values
a<-b<-d<-raster(nrows=10,ncols=10)
a[]<-sample(0:255,ncell(a))
b[]<-sample(0:255,ncell(a))
d[]<-sample(0:255,ncell(a))
rgb.rast<-stack(a,b,d)
names(rgb.rast)<-c('red','green','blue')


#create mock random forest covariate data
color.offsets<-c(0,20,40)
color.means<-c(40,80,120,160)

color.mat<-sapply(1:length(color.means),function(x){
                                                sapply(1:length(color.offsets),function(y){
                                                                round(rnorm(30,color.means[x]+color.offsets[y],sd=1))
                                                })
                                })
color.frame<-as.data.frame(color.mat)
names(color.frame)<-c('red','green','blue')


#create mock response data
response<-as.factor(rep(1:3,each=30))

#run random forest model
rf.mod<-randomForest(color.frame,response)

#attempt prediction
rf.predict<-predict(rf.mod,rgb.rast)
#error message---
#Error in if (any(is.na(x))) stop("missing values in newdata") :
#  argument is not interpretable as logical
_____________________________________________
> sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] randomForest_4.6-10 rgdal_0.9-1         raster_2.2-31       sp_1.0-15

loaded via a namespace (and not attached):
[1] grid_3.0.3      lattice_0.20-27 tools_3.0.3


Thanks a ton folks for any help you can lend!
__________________________________
Dave Gregovich
Research Analyst
Alaska Department of Fish and Game
Wildlife Conservation Division
Douglas, AK 99821
(907) 465-4291
dave.gregovich at alaska.gov
__________________________________


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list