thresholdWeighted: Weighted thresholds for predictions

View source: R/thresholdWeighted.r

thresholdWeightedR Documentation

Weighted thresholds for predictions

Description

This function is similar to the threshold function in the dismo package, which calculates thresholds to create binary predictions from continuous values. However, unlike that function, it allows the user to specify weights for presences and absence/background predictions. The output will thus be the threshold that best matches the specified criterion taking into account the relative weights of the input values.

Usage

thresholdWeighted(
  pres,
  contrast,
  presWeight = rep(1, length(pres)),
  contrastWeight = rep(1, length(contrast)),
  at = c("msss", "mdss", "minPres", "orss", "sedi", "prevalence", "sensitivity"),
  sensitivity = 0.9,
  thresholds = seq(0, 1, by = 0.01),
  delta = 0.001,
  na.rm = FALSE,
  bg = NULL,
  bgWeight = NULL,
  ...
)

Arguments

pres

Numeric vector. Predicted values at test presences.

contrast

Numeric vector. Predicted values at background/absence sites.

presWeight

Numeric vector same length as pres. Relative weights of presence sites. The default is to assign each presence a weight of 1.

contrastWeight

Numeric vector same length as contrast. Relative weights of background sites. The default is to assign each presence a weight of 1.

at

Character or character vector, name(s) of threshold(s) to calculate. The default is to calculate them all.

  • 'msss': Threshold that the maximizes the sum of sensitivity and specificity.

  • 'mdss': Threshold that minimizes the difference between sensitivity and specificity.

  • 'minPres': Minimum prediction across presences. This threshold is not weighted.

  • 'orss': Threshold that maximizes the odds ratio skill score (Wunderlich et al. 2019).

  • 'sedi': Threshold that maximizes the symmetrical extremal dependence index (Wunderlich et al. 2019).

  • 'prevalence': Prevalence of presences (sum(presence weights) / sum(presence weights + background weights))'

  • 'sensitivity': Threshold that most closely returns the sensitivity specified by sensitivity.

sensitivity

Value of specificity to match (used only if at contains 'sensitivity').

thresholds

Numeric vector. Thresholds at which to calculate the sum of sensitivity and specificity. The default evaluates all values from 0 to 1 in steps of 0.01.

delta

Positive numeric >0 in the range [0, 1] and usually very small. This value is used only if calculating the SEDI threshold when any true positive rate or false negative rate is 0 or the false negative rate is 1. Since SEDI uses log(x) and log(1 - x), values of 0 and 1 will produce NAs. To obviate this, a small amount can be added to rates that equal 0 and subtracted from rates that equal 1.

na.rm

Logical. If TRUE then remove any presences and associated weights and background predictions and associated weights with NAs.

bg

Same as contrast. Included for backwards compatibility. Ignored if contrast is not NULL.

bgWeight

Same as contrastWeight. Included for backwards compatibility. Ignored if contrastWeight is not NULL.

...

Other arguments (unused).

Value

Named numeric vector.

References

Fielding, A.H. and J.F. Bell. 1997. A review of methods for the assessment of prediction errors in conservation presence/absence models. Environmental Conservation 24:38-49.

Wunderlich, R.F., Lin, P-Y., Anthony, J., and Petway, J.R. 2019. Two alternative evaluation metrics to replace the true skill statistic in the assessment of species distribution models. Nature Conservation 35:97-116.

See Also

threshold, evaluate

Examples

set.seed(123)

# set of bad and good predictions at presences
bad <- runif(100)^2
good <- runif(100)^0.1
hist(good, breaks=seq(0, 1, by=0.1), border='green', main='Presences')
hist(bad, breaks=seq(0, 1, by=0.1), border='red', add=TRUE)
pres <- c(bad, good)
contrast <- runif(1000)
thresholdWeighted(pres, contrast)

# upweight bad predictions
presWeight <- c(rep(1, 100), rep(0.1, 100))
thresholdWeighted(pres, contrast, presWeight=presWeight)

# upweight good predictions
presWeight <- c(rep(0.1, 100), rep(1, 100))
thresholdWeighted(pres, contrast, presWeight=presWeight)

adamlilith/enmSdm documentation built on Jan. 6, 2023, 11 a.m.