[R-sig-Geo] Error when predicting a randomForest model to a raster
Robert J. Hijmans
r.hijmans at gmail.com
Wed Jan 14 03:10:43 CET 2015
P.S. you also need to do:
rf.predict <- predict(rgb.rast, rf.mod)
Not
rf.predict <- predict(rf.mod, rgb.rast)
Robert
On Tue, Jan 13, 2015 at 6:09 PM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
> Dave,
>
> The example works if you do
>
> rf.mod <- randomForest(color.frame[, c('red','green','blue')],response)
>
> instead of
>
> rf.mod <- randomForest(color.frame,response)
>
> color.frame has 4 variables, one with column name "NA". Given that you
> use names(color.frame) <- c('red','green','blue') it seems that this
> is not intentional
>
> Robert
>
>
>
>
> On Tue, Jan 13, 2015 at 5:48 PM, Gregovich, Dave P (DFG)
> <dave.gregovich at alaska.gov> wrote:
>> 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]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list