rbiomod2

broken function BIOMOD_FormatingData from biomod2 package?


I have the following variables I am inputting into biomod model:

myResp=
    structure(list(occ = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, 1, 1, 1, 
    NA_real_, 1, 1, 1, 1, 1, 1, 
    N1, 1)), row.names = c(NA, 20L), class = "data.frame")
ENV<-
    structure(list(SST = c(7.59180453767379, 7.73509759058555, 7.84841126371175, 
    7.93174555705239, 8.07323373736193, 8.27287580464035, 8.60783228824536, 
    9.07810318817695, 9.55298856149117, 10.032488408188, 10.7857288212205, 
    11.8127098005886, 12.6318128572156, 13.2430379911015, 13.5948196913426, 
    13.6871579579388, 13.9124653160572, 14.2707417656978, 14.9238792363554, 
    15.8718777280301), SSS = c(34.0450506702065, 34.0508728072047, 
    34.0571919595202, 34.0640081271529, 34.0731531754136, 34.0846271043022, 
    34.0977436259389, 34.1125027403235, 34.132830452919, 34.1587267637253, 
    34.2435973445574, 34.3874421954155, 34.5183364008864, 34.6362799609701, 
    34.6962110688289, 34.6981297244628, 34.7175911421577, 34.7545953219136, 
    34.8712614620725, 35.0675895626346), SO2 = c(0.299824322961892, 
    0.299422515447562, 0.299219067511149, 0.299213979152652, 0.298692794353701, 
    0.297655513114296, 0.2957078315046, 0.292849749524612, 0.289952534728218, 
    0.287016187115417, 0.282462434495877, 0.276291276869597, 0.271238694060594, 
    0.267304686068868, 0.265136273610794, 0.264733456686372, 0.263699139289868, 
    0.262033321421283, 0.258719810688247, 0.253758607090761), NPP = c(3.85156376257745e-07, 
    3.96088458405608e-07, 4.11107894580006e-07, 4.30214684780938e-07, 
    4.4703424908014e-07, 4.61566587477609e-07, 4.77750944139858e-07, 
    4.95587319066885e-07, 5.13265392578418e-07, 5.30785164674456e-07, 
    5.50156834773034e-07, 5.71380402874152e-07, 5.84950195207471e-07, 
    5.90866211772993e-07, 5.98413147197737e-07, 6.07591001481704e-07, 
    6.22490969298673e-07, 6.43113050648643e-07, 6.34808436525414e-07, 
    5.97577126928983e-07), MLD = c(108.645480087648, 104.108556013554, 
    98.3433291949332, 92.578102376312, 86.8128755576909, 81.6714904400887, 
    77.1539470235055, 72.6364036069223, 68.1188601903391, 65.1494404875645, 
    63.7281444985984, 62.3068485096323, 60.8855525206661, 59.9668972487703, 
    59.5508826939446, 59.1348681391189, 58.7188535842932, 59.1232465262892, 
    60.3480469651069, 61.5728474039246), SIC = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
    20L), class = "data.frame")
 resp.xy=
    structure(list(X = c(-179.75, -179.75, -179.75, -179.75, -179.75, 
    -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, 
    -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, 
    -179.75), Y = c(-52.25, -51.75, -51.25, -50.75, -50.25, -49.75, 
    -49.25, -48.75, -48.25, -47.75, -47.25, -46.75, -46.25, -45.75, 
    -45.25, -44.75, -44.25, -43.75, -43.25, -42.75)), row.names = c(NA, 
    20L), class = "data.frame")

But I get the following error

library(biomod2)
BIOMOD_FormatingData(
  resp.var = SpatialPointsDataFrame(resp.xy, myResp, proj4string = CRS("+proj=longlat +datum=WGS84")),
  expl.var = SpatialPointsDataFrame(resp.xy, ENV,  proj4string = CRS("+proj=longlat +datum=WGS84")),
  resp.name = 'Species',
  eval.resp.var = NULL,
  eval.expl.var = NULL,
  eval.resp.xy = NULL,
  PA.nb.rep = 1,
  PA.nb.absences = 3,
  PA.strategy = "random",
  PA.dist.min = 0,
  PA.dist.max = NULL,
  PA.sre.quant = 0.025,
  PA.user.table = NULL,
  na.rm = TRUE
)

I get the error: rror in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘BIOMOD.formated.data’ for signature ‘"list", "data.frame"’ which I do not understand , I provide no lists, only SpatialPointsDataFrame object as listed in the documentation

my biomod2 version 4.0 ( but I tried previous versions too), sp version is 1.4.7 ( I am on Windows 10) and using R version 4.2.0 ( but the same error was on previous version)


Solution

  • for version bimod 3.5.1 - there is a bug- so the only way to make it work is convert env variables to ratser :

    stack_raster <- function(ENV, resp.xy ) {
      r <- stack()
      for (col in 1:ncol(ENV)) {
        ENV_col <- rasterFromXYZ(cbind(resp.xy, ENV[, col]), digits = 5 )
        r <- stack(r , ENV_col)
        
      }
      names(r) = colnames(ENV)
      return(r)
    }
    
    r <- stack_raster(ENV, resp.xy)
    r 
    
    myBiomodData <- BIOMOD_FormatingData(
      resp.var = myResp, 
      expl.var = r,   
      resp.xy = resp.xy , 
      resp.name = 'Swordfish',
      eval.resp.var = NULL,
      eval.expl.var = NULL,
      eval.resp.xy = NULL,
      PA.nb.rep = 1,
      PA.nb.absences = 100,
      PA.strategy = "random",
      PA.dist.min = 0,
      PA.dist.max = NULL,
      PA.sre.quant = 0.025,
      na.rm = TRUE
    )
    

    If updated to version 4.0 ( but you will need to update all function names and argument names , the following code will work :

    myBiomodData <- BIOMOD_FormatingData(
      resp.var = SpatialPointsDataFrame(resp.xy, myResp,, proj4string = CRS("+proj=longlat +datum=WGS84")),
      expl.var = SpatialPointsDataFrame(resp.xy, ENV,  proj4string = CRS("+proj=longlat +datum=WGS84")),
      resp.name = as.character(gsub("[()]", "", metainfo$AcceptedSciName)),
      PA.nb.rep = 2,
      PA.nb.absences = nb,
      PA.strategy = 'random'
    )