bleutner/RStoolbox

`superClass` unable to predict when there is NA in raster data

bappa10085 opened this issue · 3 comments

I have a multilayer SpatRaster with NA values. When I am trying to use superClass with predict = T and predType = "prob", it returns me following error

Error: [predict] the number of values returned by 'fun' (model predict function) does not match the input. Try na.rm=TRUE?

Here is a minimal, reproducible example

library(terra)
library(RStoolbox)

f <- system.file("ex/elev.tif", package="terra")
elevation <- rast(f)
slope <- terrain(elevation, "slope")
aspect <- terrain(elevation, "aspect")
plot(elevation)
logo <- c(elevation, slope, aspect)

p <- read.table(text = "Longitude Latitude
49.60 6.00
49.65 6.10
49.70 6.15
49.75 6.20
49.80 6.25
49.85 6.27
49.87 5.80
49.90 5.83
50.00 5.85
50.05 5.90", header = T)

a <- p + 0.01  

pb <- c(rep("Yes", nrow(p)), rep("No", nrow(a)))
pts <- cbind(pb, rbind(p, a))

sp.pts <- terra::vect(pts, geom=c("Latitude", "Longitude"), crs=crs(elevation))

v <- terra::extract(logo, sp.pts, xy = T, ID = F, bind=T)

## Fit classifier (splitting training into 70% training data, 30% validation data)
rf_mod <- superClass(logo, trainData = sf::st_as_sf(v), 
                     responseCol = "pb", 
                     model = "rf", tuneLength = 1, trainPartition = 0.7, 
                     predict = T,
                     predType = "prob", #for class probabilities
                     mode = "classification",
                     kfold = 3, na.rm=TRUE)

Yes sorry for the inconvinience, fixed now in dev, I will push to master in a minute. Does this look like an expected end result to you by any chance?

Rplot01

Thank you very much for such a quick response. Now it is working fine using the development version.

No problem, also the fix is available in the main branch since pull-request #103 .
Closing this issue now :)