Search code examples
rmachine-learninggeospatialrasterbiomod2

Error reading in biomod2 modeling object and model projection object from folder


I created some simple model objects using the biomod2 package in R. I saved the model and projection to a folder on my hard drive. I then came back several days later to get my model evaluations, variable importance, and to replot the model projection, but I couldn't load in the model. The biomod2 pdf says to simply use the load() function, but when I tried that, the load() function either errored because I didn't have permission to load that object, or subsequent commands failed. Below is the built-in example from the tutorial, and the resulting errors. Note: For my actual data, I'll only be doing a Random Forest model, with no other ML methods. I'm open to suggestions on other climate envelope modeling packages.

# test the ability to read in biomod2 model object from folder and get model evaluations, variable importance, and plot

# load the library
library(biomod2)

# load our species data
DataSpecies <- read.csv(system.file("external/species/mammals_table.csv",
                                    package="biomod2"))
head(DataSpecies)

# the name of studied species
myRespName <- 'GuloGulo'

# the presence/absences data for our species
myResp <- as.numeric(DataSpecies[,myRespName])

# the XY coordinates of species data
myRespXY <- DataSpecies[,c("X_WGS84","Y_WGS84")]

# load the environmental raster layers (could be .img, ArcGIS
# rasters or any supported format by the raster package)
# Environmental variables extracted from Worldclim (bio_3, bio_4,
# bio_7, bio_11 & bio_12)
myExpl = stack( system.file( "external/bioclim/current/bio3.grd",
                             package="biomod2"),
                system.file( "external/bioclim/current/bio4.grd",
                             package="biomod2"),
                system.file( "external/bioclim/current/bio7.grd",
                             package="biomod2"),
                system.file( "external/bioclim/current/bio11.grd",
                             package="biomod2"),
                system.file( "external/bioclim/current/bio12.grd",
                             package="biomod2"))

myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,
                                     expl.var = myExpl,
                                     resp.xy = myRespXY,
                                     resp.name = myRespName)

# 2. Defining Models Options using default options.
myBiomodOption <- BIOMOD_ModelingOptions()

# 3. Computing the models
myBiomodModelOut <- BIOMOD_Modeling(
  myBiomodData,
  models = c('SRE','CTA','RF','MARS','FDA'),
  models.options = myBiomodOption,
  NbRunEval=3,
  DataSplit=80,
  Prevalence=0.5,
  VarImport=3,
  models.eval.meth = c('TSS','ROC'),
  SaveObj = TRUE,
  rescal.all.models = TRUE,
  do.full.models = FALSE,
  modeling.id = paste(myRespName,"FirstModeling",sep=""))


# get all models evaluation
myBiomodModelEval <- get_evaluations(myBiomodModelOut)
# print the dimnames of this object
dimnames(myBiomodModelEval)

# print variable importances
get_variables_importance(myBiomodModelOut)

# projection over the globe under current conditions
myBiomodProj <- BIOMOD_Projection(
  modeling.output = myBiomodModelOut,
  new.env = myExpl,
  proj.name = 'current',
  selected.models = 'all',
  binary.meth = 'TSS',
  compress = 'xz',
  clamping.mask = F,
  output.format = '.grd')

# summary of crated oject
myBiomodProj

# files created on hard drive
list.files("GuloGulo/proj_current/")

# This line appears in the global environment as "GuloGulo_AllData_RUN1_RF" a "Formal class RF_biomod2_model"
load('C:/Filepath/GuloGulo/models/GuloGuloFirstModeling/GuloGulo_AllData_RUN1_RF')

get_evaluations(GuloGulo_AllData_RUN1_RF)

The last line gives

Error in (function (classes, fdef, mtable)  : 
            unable to find an inherited method for function ‘get_evaluations’ for signature ‘"RF_biomod2_model"

Solution

  • I was indexing too far into the file folders. In the model object on the hard drive (which is the 'GuloGulo' parent folder) there is a spice file (.out) called GuloGuloFirstModeling.models.out before you get to any of the subfolders. Read in this spice file with the load function in R: load("C:/Filepath/GuloGulo/GuloGuloFirstModeling.models.out") and you'll read in all aspects of the model object: all runs, eval metrics, and variable importance.