Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
PetiteTong opened this issue Mar 6, 2024 · 1 comment
Open
1 task done

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

PetiteTong opened this issue Mar 6, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@PetiteTong
Copy link

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

  • I have done my best to provide the steps to reproduce the bug
@PetiteTong PetiteTong added the bug Something isn't working label Mar 6, 2024
@PetiteTong
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants