Closed EvickMestre closed 2 years ago
Hi Evick,
It looks like the problem is coming from "classification algorithm" such as RF. I'll try to reproduce your error and correct it as soon as possible.
Maya
ok, tks Maya
Hi Maya,
I tried BIOMOD_Modeling() function with the others models one by one (GLM, GBM, GAM, ANN and MARS) and it seems that the problem arises with all of them, not just with RF. The error message simply appears at the end of the last modelling, but all the models are concerned with the invalid class error when factors are ordered.
Evick
Hi Evick,
Could you provide me a reproductible example please ? As I do not manage to reproduce your error.... I tried transforming example data into factor variables, but I still do not get your error.
Initialization :
library(biomod2)
library(raster)
# Load species occurrences (6 species available)
myFile <- system.file('external/species/mammals_table.csv', package = 'biomod2')
DataSpecies <- read.csv(myFile, row.names = 1)
head(DataSpecies)
# Select the name of the studied species
myRespName <- 'GuloGulo'
# Get corresponding presence/absence data
myResp <- as.numeric(DataSpecies[, myRespName])
# Get corresponding XY coordinates
myRespXY <- DataSpecies[, c('X_WGS84', 'Y_WGS84')]
# Load environmental variables extracted from BIOCLIM (bio_3, bio_4, bio_7, bio_11 & bio_12)
myFiles <- paste0('external/bioclim/current/bio', c(3, 4, 7, 11, 12), '.grd')
:heavy_check_mark: This code is running :
myExpl <- raster::stack(system.file(myFiles, package = 'biomod2'))
myExpl$bio3f = myExpl$bio3
myExpl$bio3f[] = cut(myExpl$bio3f[], seq(10, 100, 10))
myExpl$bio3f = as.factor(myExpl$bio3f)
myExpl$bio12f = myExpl$bio12
myExpl$bio12f[] = cut(myExpl$bio12f[], seq(0, 6000, 500))
myExpl$bio12f = as.factor(myExpl$bio12f)
myExpl = myExpl[[c("bio3f", "bio12f")]]
myBiomodModelOut <- BIOMOD_Modeling(bm.format = myBiomodData,
bm.options = myBiomodOptions,
modeling.id = 'AllModels',
nb.rep = 2,
data.split.perc = 80,
var.import = 3,
metric.eval = c('TSS','ROC'),
do.full.models = FALSE)
:interrobang: This transformation, not sure it works (the plot shows empty map, and values seem to be all NA) :
myExpl <- raster::stack(system.file(myFiles, package = 'biomod2'))
myExpl$bio3f = myExpl$bio3
myExpl$bio3f[] = cut(myExpl$bio3f[], seq(10, 100, 10))
myExpl$bio3f = factor(myExpl$bio3f, ordered = TRUE, levels = 1:9)
:interrobang: This transformation provides me an error :
myExpl <- raster::stack(system.file(myFiles, package = 'biomod2'))
myExpl$bio3f = myExpl$bio3
myExpl$bio3f[] = cut(myExpl$bio3f[], seq(10, 100, 10))
myExpl$bio3f = as.ordered(myExpl$bio3f)
Error in unique.default(x, nmax = nmax) : unique() applies only to vector
Hi Maya,
here are my script and the csv file containing all the variables
Evick
Hello Evick,
Thank you for the script and data.
I fixed the problem for the ordered factors (was not really an error, just that class()
returns a vector with 2 elements : "ordered", "factor"
, which was messing with the one value expected).
You will probably encounter another error, specifying for some models that some factor levels are missing in the original fit.
It must come from the sampling of factor values (not all factor levels are represented within the calib.lines
selected).
I'll try to fix that too.
Maya
I just checked : no error in the bm_SampleFactorLevels
function.
It's coming that some of your categorical variables (SITUAPENTE
for example) have levels referenced that have no values. You should drop them.
Maya
Hi Maya,
it works great now with orderd factors and without levels with no values. Thanks a lot !
Evick
Hi,
is it possible to use ordered factors for exploratory variables in BioMod2 ?
When i use "as.factor()" to convert a variable into a factorial one followed by factor(x, levels=c(...)) , there is no problem. But when I try order the levels using "factor(x, ordered = TRUE, levels=c(....))" or "as.ordered(X)", I get the following error message when I run "BIOMOD_Modeling()" in the stable version (3.5.1) of Biomod2 :
invalid class “GLM_biomod2_model” object: invalid object for slot "expl_var_type" in class "GLM_biomod2_model": got class "list", should be or extend class "character"
Is there a way to order factors compatible with "BIOMOD_Modeling()" ?
According to the recommendation of Maya, I tried with the Development version (4.0). This solved the issue for GLM, GBM, GAM, ANN and MARS models, but I still get the same error message with the RF model.
Here is my code and the output of Biomod2 functions. Script_Rforgetxt.txt
Thank you Evick