[R-sig-Geo] Error: NA/NaN/Inf in foreign function call (arg 7) while using Predict function with Raster

aniruddha ghosh aniru123 at gmail.com
Thu Oct 4 15:09:02 CEST 2012


Hello Robert,
I used the development version of 'raster' (version 2.0-20) with
option "inf.rm". It solved the issue!
writeRaster was not able to remove Inf.
Sorry for not posting with self-contained example,

Thanks for the help!

On Thu, Oct 4, 2012 at 6:28 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
> Ani,
>
> The raster::predict function has na.rm=TRUE and removes the NA values, but
> not -Inf/Inf values and that indeed seems to be the problem here (see
> example below). I have added an option "inf.rm" to the predict function of
> the development version of 'raster' (version 2.0-20) you can try to install
> it from R-Forge in 12 hours: install.packages("raster",
> repos="http://R-Forge.R-project.org").
>
> With that version, your code will likely work if you do predict(      ,
> inf.rm=TRUE)
>
> Alternatively, you could write your rasters to a new file using writeRaster,
> because that will likely replace the -Inf/Inf values with NA, and then use a
> RasterBrick made from this new file in the predict version.
>
> When asking questions like this, please try to provide a self-contained
> example like I provide below (mostly copied from ?raster::predict).
>
> library(raster)
> library(caret)
>
> logo <- brick(system.file("external/rlogo.grd", package="raster"))
> #get presence and random-absence points
> p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
>    66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,
>    22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
> a <- cbind(runif(250)*(xmax(logo)-xmin(logo))+xmin(logo),
>            runif(250)*(ymax(logo)-ymin(logo))+ymin(logo))
>
>
> #extract values for points
> xy <- rbind(cbind(1, p), cbind(0, a))
> train <- data.frame(extract(logo, xy[,2:3]))
> class <- as.factor(xy[,1])
>
> f <- class~red+green+blue
> rfFit <- train(as.data.frame(train), class, method = "rf", tuneLength = 5,
> trControl = trainControl(method = "repeatedcv", repeats = 5, verboseIter =
> FALSE), metric = "Kappa")
>
>
>>  logo[2:10,] <- NA
>> r1 <- predict(logo, rfFit)
>>
>> logo[2:10,] <- Inf
>> r2 <- predict(logo, rfFit)
> Error in predict.randomForest(modelFit, newdata) :
>   NA/NaN/Inf in foreign function call (arg 7)
>>
>
>
> Best, Robert
>
> On Wed, Oct 3, 2012 at 7:23 AM, aniruddha ghosh <aniru123 at gmail.com> wrote:
>>
>> Dear List,
>> I'm trying to classify a multiband raster with point shapefiles as
>> training data. I used the following:
>> vec<-readOGR(".","training")
>> raster<-stack("test.tif")   #it contains around 100000 pixels and 240
>> layers
>> outImage<-"classified.tif"
>> #then extract the training values
>> train<-extract(raster,vec)
>> #after that used "caret" package to train the model
>> rfFit <- train(as.data.frame(train),as.factor(vec$class),
>>  method = "rf", tuneLength = 5,
>>  trControl = trainControl(method = "repeatedcv",
>>  repeats = 5,
>>  verboseIter = FALSE),
>>  metric = "Kappa")
>> # then to classify the entire data
>> rfPred <- predict(raster,rfFit, filename=outImage,na.rm=TRUE,
>> progress='text', format='GTiff', datatype='INT1U', overwrite=TRUE)
>>
>> after some progress (more than 20 %) I'm getting this error:
>> "NA/NaN/Inf in foreign function call (arg 7)"
>>
>> I have checked the code for small test image (around 10000 pixels with
>> 125 layers) and it worked fine. I suspect it is due to the "-Inf"
>> present in the large raster. How to deal with this?
>>
>> Thanks,
>> --
>> Ani
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>



-- 
Aniruddha Ghosh
Graduate Student
Department of Natural Resources
TERI University,New Delhi



More information about the R-sig-Geo mailing list