Closed ManuelSpinola closed 4 years ago
Thanks for reporting the error, I will check it today or tomorrow.
The error happens because the function "predict" in the randomForest package expects to have a factor for the "biome" variable, but in the raster object "biome" is a numeric variable.
To avoid this error you can pass an additional argument to the predict function. The predict function for raster dataset implemented in SDMtune uses the predict function implemented in the raster package https://rdrr.io/cran/raster/man/predict.html. You can use the factors argument to pass the levels of the "biome" factor. Here is the code:
f <- list(biome = levels(data@data$biome))
map <- predict(default_model, data = predictors, factors = f)
Note that the factors argument is a named list. You have to match the name with the name of the raster layer, in this case biome. Be aware that you might have to handle cases in which there are more levels in the raster layer compared to the training dataset (google it and you will find some examples).
It works. Thank you very much Sergio.
El mar., 26 may. 2020 a las 1:20, Sergio Vignali (notifications@github.com) escribió:
The error happens because the function "predict" in the randomForest package expects to have a factor for the "biome" variable, but in the raster object "biome" is a numeric variable.
To avoid this error you can pass an additional argument to the predict function. The predict function for raster dataset implemented in SDMtune uses the predict function implemented in the raster package https://rdrr.io/cran/raster/man/predict.html. You can use the factors argument to pass the levels of the "biome" factor. Here is the code:
f <- list(biome = levels(data@data$biome)) map <- predict(default_model, data = predictors, factors = f)
Note that the factors argument is a named list. You have to match the name with the name of the raster layer, in this case biome. Be aware that you might have to handle cases in which there are more levels in the raster layer compared to the training dataset (google it and you will find some examples).
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ConsBiol-unibern/SDMtune/issues/8#issuecomment-633854882, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFI3FB3GNTZCO4ZTLXUBRBLRTNUVRANCNFSM4NIUWEUA .
-- Manuel Spínola, Ph.D. Instituto Internacional en Conservación y Manejo de Vida Silvestre Universidad Nacional Apartado 1350-3000 Heredia COSTA RICA mspinola@una.cr mspinola@una.ac.cr mspinola10@gmail.com Teléfono: (506) 8706 - 4662 Personal website: Lobito de río https://sites.google.com/site/lobitoderio/ Institutional website: ICOMVIS http://www.icomvis.una.ac.cr/
This workaround no longer seems to be effective, potentially due to the migration to terra. The following code was adapted from the original comment:
library(SDMtune)
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"), pattern = "grd", full.names = TRUE)
predictors <- raster::stack(files)
help(virtualSp)
p_coords <- virtualSp$presence
bg_coords <- virtualSp$background
data <- prepareSWD(species = "Virtual species", p = p_coords, a = bg_coords,
env = terra::rast(predictors),
categorical = "biome")
default_model <- train(method = "RF", data = data)
f <- list(biome = levels(data@data$biome))
map <- predict(default_model, data = terra::rast(predictors), factors = f)
And returns the following error: Error in .local(object, ...) : unused argument (factors = list(c("1", "2", "3", "4", "5", "7", "8", "9", "10", "11", "12", "13", "14")))
It's worth noting that the predict function in terra doesn't take a factors argument like the predict function in the raster package did, which might cause this issue.
Confirmed that going back to CRAN version 1.1.6 fixes this problem.
I got an error when working with a Random Forest model and specify a categorical variable as a predictor
Error in predict.randomForest(object@model, data, type = "prob") : Type of predictors in new data do not match that of the training data.