Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

superClass unable to predict when there is NA in raster data #102

Closed
bappa10085 opened this issue Jan 24, 2024 · 3 comments
Closed

superClass unable to predict when there is NA in raster data #102

bappa10085 opened this issue Jan 24, 2024 · 3 comments
Assignees
Labels

Comments

@bappa10085
Copy link

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)
@KonstiDE
Copy link
Collaborator

KonstiDE commented Jan 24, 2024

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

@KonstiDE KonstiDE added the bug label Jan 24, 2024
@KonstiDE KonstiDE self-assigned this Jan 24, 2024
KonstiDE added a commit that referenced this issue Jan 25, 2024
#102: Fixed a bug that caused the classifier to crash with na values
@bappa10085
Copy link
Author

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

@KonstiDE
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants