ConsBiol-unibern / SDMtune

Performs Variables selection and model tuning for Species Distribution Models (SDMs). It provides also several utilities to display results.
https://consbiol-unibern.github.io/SDMtune/
Other
25 stars 8 forks source link

[Bug]: function auc() report error when used alone #35

Open PetiteTong opened 9 months ago

PetiteTong commented 9 months ago

Describe the bug

function auc() report error when used alone

Steps to reproduce the bug

library(SDMtune)
library(sp)
library(raster)
library(dismo)
library(rgdal)
library(rJava)
library(dplyr)
library(magrittr)
library(ade4)
library(ape)
library(gbm)
#library(ecospat)
library(sf)
library(doSNOW)
library(ENMeval)
library(rasterVis)
library(magrittr)
library(SDMtune)
library(zeallot)
library(ggplot2)   
library(maps)       
library(lattice)
library(plotROC)
library(kableExtra)
library(pROC)
library(terra)
library(ISwR)
library(PMCMRplus)

# Set a random seed in order to be able to reproduce this analysis.
set.seed(0)

occs <- read.table("D:/SXT/bamrepanda/panda_range/PANDA.csv", header=TRUE, sep=',')

occs <- occs[!duplicated(occs),]

#occs <- na.omit(occs)   

slopsin<-raster('D:/SXT/clipmap/else/right/slopesin1.tif')

elev=raster('D:/SXT/newdata/maps/bufferareaDEM/bufdem30mutm.tif')

landcover <- raster('D:/SXT/bamrepanda/ouyangNEE_model/land_cover_re30m.tif')

#bio因子

bio1<-raster('D:/SXT/DS-cor-maps/current/bio/bio1.tif')
# bio11<-resample(bio1,a)
# bio1<-bio11

bio5<-raster('D:/SXT/DS-cor-maps/current/bio/bio5.tif')
bio6<-raster('D:/SXT/DS-cor-maps/current/bio/bio6.tif')
bio12<-raster('D:/SXT/DS-cor-maps/current/bio/bio12.tif')

#bamboo<-raster('D:/SXT/bam_mod/mod1km_map/diversity/diversity_cur.tif')

envs_stack = stack(slopsin,elev,landcover,bio1,bio5,bio6,bio12)
names(envs_stack) <- c('slopsin','elev','landcover','bio1','bio5','bio6','bio12')
envs_stack$landcover <- raster::as.factor(envs_stack$landcover)

occs.cells <- raster::extract(envs_stack, occs, cellnumbers = TRUE)
occs.cellDups <- duplicated(occs.cells[,1])
occs <- occs[!occs.cellDups,]

bg <- dismo::randomPoints(envs_stack,10000) %>% as.data.frame()
colnames(bg) <- colnames(occs)

bg.cells <- raster::extract(envs_stack, bg, cellnumbers = TRUE)
bg.cellDups <- duplicated(bg.cells[,1])
bg <- bg[!bg.cellDups,]

data <- prepareSWD(species = "panda", p = occs, a =bg,
                   env = envs_stack, categorical = "landcover")

require(ENMeval)

## Checkerboard1 partition using the ENMeval package
cb_folds <- get.checkerboard2(occ = data@coords[data@pa == 1,], bg = data@coords[data@pa == 0,],
                              env =envs_stack, aggregation.factor = 4 )

model <- train(method = "Maxent", data = data, fc = c('lqpht'), reg = 1,
               folds = cb_folds)
saveRDS(model,"D:/SXT/bamrepanda/panda_bamboo/modelA/modelA.rda")

#获得变量重要性

vi<-maxentVarImp(model)
vi

vi1<-varImp(model,permut = 10)
plotVarImp(vi1)

jkvi<-doJk(model,metric = 'auc')

# plotJk(jkvi,
#        type = 'train',
#        ref = auc(model))
#
# plotJk(jkvi,
#        type = 'test',
#        ref = auc(model))

plotResponse(model,
             var='bio12',
             type = 'logistic',
             only_presence = TRUE,
             marginal = FALSE,
             rug = TRUE)

#AUC
auc=auc(model)
auc
#ROC
#plotROC(model)

#TSS
tss=tss(model)
tss

Session information

> summary(modelA)
    Length      Class       Mode
         1 SDMmodelCV         S4
> modelA
Object of class SDMmodelCV
Method: Maxent

Species: panda
Replicates: 4
Presence locations: 822
Absence locations: 9999

Model configurations:
--------------------
fc: lqpht
reg: 1
iter: 500

Variables:
---------
Continuous: slopsin elev bio1 bio5 bio6 bio12
Categorical: landcover
> #AUC
> auc=auc(modelA)
Error in roc.default(response, predictor, auc = TRUE, ...) :
  No valid data provided.
> auc
function (...)
{
    UseMethod("auc")
}
<bytecode: 0x000002a6d27c2ef0>
<environment: namespace:pROC>
> #ROC
> #plotROC(model)
>
> #TSS
> tss=tss(modelA)
> tss
[1] 0.6508162
>

Additional information

No response

Reproducible example

PetiteTong commented 9 months ago

Dear Vignali,

Hello, and thank you very much for creating the initial SDMtune package and continuously updating its features; it's truly invaluable. However, there's an unexpected issue that I would like your assistance with. When I was computing the maxent model in 2022, I utilized the chessboard grid data splitting method from the ENMenval package to increase data availability and reduce spatial autocorrelation. I saved the model at that time. Now, I intend to use this model to generate variable importance plots and calculate AUC TSS data. However, some functionalities are currently incompatible.

Could you please help resolve this issue?

Thank you for your attention to this matter.

Best regards, Xiaotong