Sensitivity-specificity curves

**NOTE: this function is now deprecated; please use the optiPair function instead**

The SensSpecCurve function calculates and plots sensitivity (black circles) and specificity (white circles) for all thresholds at a given interval. The plot marks the threshold and the sensitivity value at which sensitivity and specificity are closest to each other. Optionally, you can plot also the difference (minuses), sum (pluses), and/or mean (grey line) of sensitivity and specificity, together with their optimal values and respective thresholds. All these values are also provided with the text output of the function.

SensSpecCurve <- function (obs, pred, interval = 0.01, plot = TRUE, plot.diff = FALSE, plot.sum = FALSE, plot.mean = FALSE, ...) {
# obs: a vector of observed presences (1) and absences (0) or another binary response variable
# pred: a vector with the corresponding predicted values of presence probability, habitat suitability, environmental favourability or alike
# interval: the interval of thresholds at which to calculate sensitivity and specificity
# plot: logical indicating whether or not to plot the curves
# plot.sum and plot.diff will optionally plot the sum (+) and difference (-) between sensitivity and specificity
# ... : arguments to be passed to the "plot" function

  SensSpec <- optiThresh(obs = obs, pred = pred, interval = interval, measures = c("Sensitivity", "Specificity"), plot = FALSE, simplif = TRUE)
  SensSpec$Difference <- abs(SensSpec$Sensitivity - SensSpec$Specificity)
  SensSpec$Sum <- rowSums(SensSpec[,1:2])
  SensSpec$Mean <- rowMeans(SensSpec)
  SensSpec$Threshold <- as.numeric(rownames(SensSpec))

  MinDiff <- min(SensSpec$Difference)
  MaxSum <- max(SensSpec$Sum)
  MaxMean <- max(SensSpec$Mean)

  ThreshDiff <- with(SensSpec, Threshold[which.min(Difference)])
  ThreshSum <- with(SensSpec, Threshold[which.max(Sum)])
  ThreshMean <- with(SensSpec, Threshold[which.max(Mean)])

  if(plot){
    ymax <- ifelse(plot.sum, max(SensSpec$Sum), 1)

    with(SensSpec, plot(Sensitivity ~ Threshold, pch = 20, ylim = c(0, ymax), ylab = "Sensitivity and specificity", ...))
    with(SensSpec, points(Specificity ~ Threshold, pch = 1))
    abline(h = SensSpec[which.min(SensSpec$Difference), "Sensitivity"], col = "grey", lty = 2)
    abline(v = ThreshDiff, col = "grey", lty = 2)

    if(plot.mean) {
      with(SensSpec, lines(Mean ~ Threshold, col = "grey"))
      abline(v = ThreshMean, col = "grey", lty = 2)
      abline(h = MaxMean, col = "grey", lty = 2)
    }  # end if plot mean

    if(plot.sum) {
      with(SensSpec, points(Sum ~ Threshold, pch = '+'))
      abline(v = ThreshSum, col = "grey", lty = 2)
      abline(h = MaxSum, col = "grey", lty = 2)
    }    # end if plot sum

    if(plot.diff) {
      with(SensSpec, points(Difference ~ Threshold, pch = '-', col = "grey"))
      abline(v = ThreshDiff, col = "grey", lty = 2)
      abline(h = MinDiff, col = "grey", lty = 2)
    }  # end if plot diff
  }  # end if plot

  return(list(SensSpec = SensSpec, MinDiff = MinDiff, ThreshDiff = ThreshDiff, MaxSum = MaxSum, ThreshSum = ThreshSum, MaxMean = MaxMean, ThreshMean = ThreshMean))

}  # end SensSpecCurve function

[presented with Pretty R]

This function uses the optiThresh function, which in turn uses threshMeasures and evaluate, so all these must be loaded along with SpensSpecCurve for it to work. Then just load your observed and predicted data (see Beginner’s guide for more information) and type or paste:

with(mydata, SpensSpecCurve(obs = myspecies, pred = myspecies_P))

Advertisements

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s