# In this first example, a data set is simulated in such a way that the
# classification rule is well-calibrated, i.e., the observed proportion of
# positive cases equates to the simulated probabilities of presence. Since
# the objective is to calculate the uAUC to account for the environmental
# representativeness effect (see Jiménez-Valverde 2022), weights are
# automatically calculated and no w vector is needed.
n <- 1000 # Set the sample size
hs <- rbeta(n, 2, 2) # Simulated probabilities (the classification rule)
random <- runif(n)
sp <- ifelse(random < hs, 1, 0) # Observed presence–absence data
result <- AUCuniform.2(cbind(hs, sp), plot = TRUE, plot.compare = TRUE)
result$AUC # Get the standard AUC
result$uAUC # Get the uniform AUC. Note how it is close to the reference value
# of 0.83 since the probability values (the classification rule)
# are simulated to be well-calibrated (see Jiménez-Valverde 2022)
# In this second set of examples, the objective is not to calculate the
# uniform AUC, but to assign specific weights to certain observations. These
# examples corresponds to some of those provided in Table 1 of
# Jiménez-Valverde (2025).
hs <- seq(1, 0.05, by = -0.05) # Generate the classification rule
sp <- c(0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0) # Observed presence–absence data
wa <- ifelse(sp == 0, 0.2, 1) # The vector of weights for each case
result.a <- AUCuniform.2(cbind(hs, sp), w = wa, plot = TRUE, plot.compare = TRUE)
result.a$AUC # Get the standard AUC
result.a$wAUC # Get the weighted AUC. Since every case within each category of
# sp received the same weight, the weighted AUC value equals the
# standard AUC value
wb <- c(rep(1, 19), 0.2) # The vector of weights for each case
result.b <- AUCuniform.2(cbind(hs, sp), w = wb, plot = TRUE, plot.compare = TRUE)
result.b$wAUC # Get the weighted AUC. Since a low weight is assigned to an
# instance of absence associated with a low probability value,
# the weighted AUC is lower than the standard AUC value.
wc <- c(0.2, rep(1, 19)) # The vector of weights for each case
result.c <- AUCuniform.2(cbind(hs, sp), w = wc, plot = TRUE, plot.compare = TRUE)
result.c$wAUC # Get the weighted AUC. Since a low weight is assigned to an
# instance of absence associated with a high probability value,
# the weighted AUC is higher than the standard AUC value
Run the code above in your browser using DataLab