R/calc.AUC.R

# Following the Tutorial on MaxEnt by Steven Phillips
# Combine all in this R file into one function soon!

# Loading required packages
require(ROCR)
require(vcd)
require(boot)

# Reqading in and organizing the data
presenceIN <- read.csv("YOUR_SPECIES_samplePredictions.csv")
backgroundIN <- read.csv("YOUR_SPECIES_backgroundPredictions.csv")
pp <- presence$Logistic.prediction                   # get the column of predictions
testpp <- pp[presence$Test.or.train=="test"]         # select only test points
trainpp <- pp[presence$Test.or.train=="train"]       # select only test points
bb <- background$logistic

# Preparing input for ROCR
combined <- c(testpp, bb)                            # combine into a single vector
label <- c(rep(1,length(testpp)),rep(0,length(bb)))  # labels: 1=present, 0=random
pred <- prediction(combined, label)                  # labeled predictions, if there is an error make sure test % is not 0.
perf <- performance(pred, "tpr", "fpr")              # True / false positives, for ROC curve
plot(perf, colorize=FALSE)                            # Show the ROC curve
performance(pred, "auc")@y.values[[1]]               # Calculate the AUC
 
# Bootstrap to generate standard error and confidence interval for the AUC
# Function AUC
AUC <- function(p,ind) {
  pres <- p[ind]
  combined <- c(pres, bb)
  label <- c(rep(1,length(pres)),rep(0,length(bb)))
  predic <- prediction(combined, label)
  return(performance(predic, "auc")@y.values[[1]])
}

# Running the bootstrap with 10 AUC calculations
b1 <- boot(testpp, AUC, 10)
b1                                                   # gives estimates of standard error and bias of AUC
boot.ci(b1, conf = 0.95, type = "basic")             # confidence interval of AUC
jinyung/SDMbase documentation built on May 19, 2019, 10:36 a.m.