我必须得到 AUC(ROC 曲线下的面积)来评估我的模型。我正在使用带有 Biomod2 包的 SRE 模型。
Biomod2 通过evaluate()函数给我 AUC 值,这很好。然后我尝试用 ROCR 包检查 AUC,我遇到了一个大问题:evaluate() (Biomod2) 和 ROCR 的 AUC 值不一样。(对于其他一些没有在Biomod2上实现的模型,我必须使用ROCR包,所以我很着急。)
现在我正在尝试使用可重现的示例(来自 Biomod2 包的代码)来查找问题,但出现错误。
首先,来自 Biomod2 包的代码(可重现的示例):
library(biomod2)
library(ROCR)
# species occurrences
DataSpecies <- read.csv(system.file("external/species/mammals_table.csv",
package="biomod2"), row.names = 1)
head(DataSpecies)
# the name of studied species
myRespName <- 'GuloGulo'
# the presence/absences data for our species
myResp <- as.numeric(DataSpecies[,myRespName])
# the XY coordinates of species data
myRespXY <- DataSpecies[,c("X_WGS84","Y_WGS84")]
# Environmental variables extracted from BIOCLIM (bio_3, bio_4, bio_7, bio_11 & bio_12)
myExpl = stack( system.file( "external/bioclim/current/bio3.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio4.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio7.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio11.grd",
package="biomod2"),
system.file( "external/bioclim/current/bio12.grd",
package="biomod2"))
# 1. Formatting Data
myBiomodData <- BIOMOD_FormatingData(resp.var = myResp,
expl.var = myExpl,
resp.xy = myRespXY,
resp.name = myRespName)
# 2. Defining Models Options using default options.
myBiomodOption <- BIOMOD_ModelingOptions()
# 3. Doing Modelisation
myBiomodModelOut <- BIOMOD_Modeling( myBiomodData,
models = c('SRE'),
models.options = myBiomodOption,
NbRunEval=1,
DataSplit=80,
Yweights=NULL,
VarImport=3,
models.eval.meth = c('TSS'),
SaveObj = TRUE,
rescal.all.models = FALSE,
do.full.models = FALSE,
modeling.id='test')
# 4. Evaluate model over another dataset (here the full one)
## creation of suitable dataset
data <- cbind(GuloGulo=get_formal_data(myBiomodModelOut,'resp.var'),
get_formal_data(myBiomodModelOut,'expl.var'))
## evaluation
evaluate(myBiomodModelOut, data=data, stat=c('ROC','TSS'))
然后,从 ROCR 获取 AUC 的部分:
# 4.1 Projection on current environemental conditions
myBiomodProjection <- BIOMOD_Projection(modeling.output = myBiomodModelOut,
new.env = myExpl,
proj.name = 'current',
selected.models = 'all',
binary.meth = 'TSS',
compress = FALSE,
build.clamping.mask = FALSE)
myBiomod_raster <- get_predictions(myBiomodProjection) #, as.data.frame=TRUE
spp_occ <- myResp
sauv_p <- as.vector(as.matrix(myBiomod_raster))
ROC_curve <- data.frame(result=sauv_p, spp=spp_occ) #préparation
ROC_curve <- prediction(ROC_curve$result, ROC_curve$spp) #run
AUC <- performance( ROC_curve, "auc" )@y.values
print(AUC[[1]])
我遇到的问题是我无法从 myResp 和 myBiomod_raster 创建 data.frame,因为length(myResp)
= 2488 并且 myBiomod_raster 的长度是 5640。
错误是:参数暗示不同的行数
你知道如何使用这个 Biomod2 包示例中的数据来获得相同的行数吗?
非常感谢!