sjevelazco/flexsdm

error in occfilt_env

peterpothmann opened this issue · 1 comments

I'm encountering an error when running the function occfilt_env in my script. This might be related to Issue #313.

library(geodata)
library(terra)
library(flexsdm)
library(tibble)
library(sf)

# get predictor data
tmin <- worldclim_country("germany", var=c("tmin"), path=tempdir())
tmax <- worldclim_country("germany", var=c("tmax"), path=tempdir())
e <- elevation_30s("germany", path=tempdir())
e <- ifel(e < 2000, 0, e) # set most of the pixel values to 0
e <- resample(e, tmax)

# combine
predictor <- c(tmin, tmax)
predictorError <- c(tmin, tmax, e)

# get occurrence data
set.seed(1)
occ <- spatSample(x = predictor, size = 1000, as.points = TRUE, values = FALSE)

occ <- occ |> 
  st_as_sf() |> 
  st_coordinates() |> 
  as_tibble() |> 
  rowid_to_column() 

# now the function
#

# this works
envFilter <- occfilt_env(
  data = occ,
  x = "X",
  y = "Y",
  id = "rowid",
  env_layer = predictor, # without e 
  nbins = 5
)

# error with the SpatRaster where most of the values are 0
envFilterError <- occfilt_env(
  data = occ,
  x = "X",
  y = "Y",
  id = "rowid",
  env_layer = predictorError, # with SpatRaster e
  nbins = 5
)

# Error in seq.default(ext1[1], ext1[2], by = res[i]) : 
#  invalid '(to - from)/by'


Here is my sessionInfo:

R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.utf8 LC_CTYPE=German_Germany.utf8
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C
[5] LC_TIME=German_Germany.utf8

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] tibble_3.2.1 flexsdm_1.3.4 geodata_0.5-9 terra_1.7-78

loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 lattice_0.22-6
[4] spThin_0.2.0 magrittr_2.0.3 grid_4.3.3
[7] timechange_0.3.0 iterators_1.0.14 gbm_2.1.9
[10] maps_3.4.2 foreach_1.5.2 doParallel_1.0.17
[13] Matrix_1.6-5 nnet_7.3-19 survival_3.5-8
[16] mgcv_1.9-1 spam_2.10-0 fansi_1.0.6
[19] kernlab_0.9-32 viridisLite_0.4.2 scales_1.3.0
[22] codetools_0.2-20 cli_3.6.2 rlang_1.1.3
[25] munsell_0.5.1 splines_4.3.3 tools_4.3.3
[28] raster_3.6-26 parallel_4.3.3 dplyr_1.1.4
[31] colorspace_2.1-0 ggplot2_3.5.0 vctrs_0.6.5
[34] R6_2.5.1 lifecycle_1.0.4 lubridate_1.9.3
[37] randomForest_4.7-1.1 pkgconfig_2.0.3 pillar_1.9.0
[40] gtable_0.3.5 glue_1.7.0 dismo_1.3-14
[43] Rcpp_1.0.12 fields_15.2 xfun_0.45
[46] tidyselect_1.2.1 knitr_1.47 rstudioapi_0.16.0
[49] nlme_3.1-164 patchwork_1.2.0.9000 dotCall64_1.1-1
[52] maxnet_0.1.4 compiler_4.3.3 SDMtune_1.3.1
[55] sp_2.1-3

Any help would be greatly appreciated!