Skip to content

[Bug]: problem producing maxent models and their predictions #49

@mguerreiro24

Description

@mguerreiro24

Describe the bug

I am getting this error in one of my species (code given bellow):
Error in data.frame(th = rownames, val = ths, fpa = fpa, or = or_train, :
arguments imply differing number of rows: 5, 3
3.
stop(gettextf("arguments imply differing number of rows: %s",
paste(unique(nrows), collapse = ", ")), domain = NA)
2.
data.frame(th = rownames, val = ths, fpa = fpa, or = or_train,
stringsAsFactors = FALSE)
1.
thresholds(model, type = "cloglog", test = test)

And when I calculate predictions rasters from these models, there are only 0's and one value of presence (e.g.: 0,6 or 0,64).

Steps to reproduce the bug

library(SDMtune)
Sys.setenv(JAVA_HOME = "C:/Program Files/Java/jre-1.8/")
setwd('D:/PostDoc/')

species <- 'PheronemaCarpenteri'
#species <- 'DendrophylliaCornigera'
presence_locations <- read.csv(paste(species,'/EnvironmentalData.csv',sep=''))

current <- sqrt(presence_locations$horizontalXCurrents^2+presence_locations$horizontalYCurrents^2)
presence_locations <- cbind(presence_locations,current)
presence_locations <- subset(presence_locations,current<1)
presence_locations <- presence_locations[,c(1:7,9,10,13)]#selecting variables
variables <- variable.names(presence_locations)[3:10]
  
background <- read.csv('backgroundEnvironment.csv')
background <- na.omit(background)
current <- sqrt(background$horizontalXCurrents^2+background$horizontalYCurrents^2)
background <- cbind(background,current)
background <- subset(background,current<1)
background <- background[,c(1:7,9,10,13)]#selecting variables

proportion <- dim(presence_locations)[1]*10



lengthOfVariables <- length(variables)
# "ANN", "BRT", "Maxent", "Maxnet" or "RF"

models <- c('maxent',
            #'BRT',
            'RF',
            'NN'
            )
runs <- 1:5
modelsList <- list()
evaluationList <- list()
TSSs <- c()
AUCs <- c()
threshs <- c()
responseCurves <- list()

for (i in 1:(lengthOfVariables-1)){
for (ii in (i+1):lengthOfVariables){
for (modelName in models){
for (run in runs){
  background <- background[sample(nrow(background),proportion),]
  data_species <- rbind(presence_locations,background)
  df_coords<-data_species[,1:2]#coordinates: presence & absence
  df_data<-data_species[,c(i+2,ii+2)]#environmental data: presence & absence
  pa<- c(rep(1,dim(presence_locations)[1]),rep(0,dim(background)[1]))#vectors of 1 and 0: presence & absence
  data <- SWD(species = species,
              coords = df_coords,
              data = df_data,
              pa = pa)
  # Split presence locations in training (75%) and testing (25%) datasets
  datasets <- trainValTest(data, test = 0.25, only_presence = TRUE, seed = run)
  train <- datasets[[1]]
  test <- datasets[[2]]
  if (modelName=='maxent'){model <- train(method = "Maxent", data=train)}
  if (modelName=='RF'){model <- train(method = "ANN", data=train, size = 99, maxit = 10000)}
  if (modelName=='NN'){model <- train(method = "RF", data=train)}
  lModelList <- length(modelsList)
  modelsList[[lModelList+1]] <- model
  AUCs <- c(AUCs, auc(model,test))
  TSSs <- c(TSSs, tss(model,test))
  if (modelName=='maxent'){
    modelT <-thresholds(model,type = "cloglog",test=test)#<-------------------------------------------Error here
    threshs <- c(threshs,modelT[2,3])
  } else{
    modelT <- thresholds(model,test=test)
    threshs <- c(threshs,modelT[2,5])
  }
  evaluationList[[lModelList+1]] <- varImp(model,permut = 25)
  temp <-list()
  if (modelName=='maxent'){
    my <- plotResponse(model,
                 var = variables[i],
                 type = "cloglog")
    temp[[variables[i]]] <- my@data
    my <- plotResponse(model,
                       var = variables[ii],
                       type = "cloglog")
    temp[[variables[ii]]] <- my@data
    responseCurves[[lModelList+1]] <- temp
    }else {
    my <- plotResponse(model,var = variables[i])
    temp[[variables[i]]] <- my@data
    my <- plotResponse(model,var = variables[ii])
    temp[[variables[ii]]] <- my@data
    responseCurves[[lModelList+1]] <- temp
  }
}}}}

Session information

R version 4.5.1 (2025-06-13 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default
  LAPACK version 3.12.1

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
[1] LC_COLLATE=Portuguese_Portugal.utf8  LC_CTYPE=Portuguese_Portugal.utf8   
[3] LC_MONETARY=Portuguese_Portugal.utf8 LC_NUMERIC=C                        
[5] LC_TIME=Portuguese_Portugal.utf8    

time zone: Europe/Berlin
tzcode source: internal

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

other attached packages:
[1] SDMtune_1.3.3

loaded via a namespace (and not attached):
 [1] terra_1.8-70       vctrs_0.6.5        cli_3.6.5          rlang_1.1.6       
 [5] stringi_1.8.7      generics_0.1.4     S7_0.2.0           rJava_1.0-11      
 [9] glue_1.8.0         sp_2.2-0           scales_1.4.0       grid_4.5.1        
[13] tibble_3.3.0       lifecycle_1.0.4    stringr_1.5.2      compiler_4.5.1    
[17] dplyr_1.1.4        codetools_0.2-20   RColorBrewer_1.1-3 Rcpp_1.1.0        
[21] pkgconfig_2.0.3    rstudioapi_0.17.1  farver_2.1.2       lattice_0.22-7    
[25] R6_2.6.1           tidyselect_1.2.1   pillar_1.11.1      magrittr_2.0.4    
[29] tools_4.5.1        gtable_0.3.6       dismo_1.3-16       raster_3.6-32     
[33] ggplot2_4.0.0

Additional information

I am running a small sample (n=11; train=8; test=3) species distribution model ensemble.
I believe that the low number may be one of the causes.

Reproducible example

  • I have done my best to provide the steps to reproduce the bug

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions