Skip to content

Commit

Permalink
Fix issue #25
Browse files Browse the repository at this point in the history
Select the threshold more carefully in case we have threshold exactly identical to some of the predictor
  • Loading branch information
xrobin committed May 1, 2018
1 parent 633e734 commit 87fceab
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions R/roc.utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,52 @@ roc.utils.perfs.dens <- function(threshold, x, dens.controls, dens.cases, direct
roc.utils.thresholds <- function(predictor, direction) {
unique.candidates <- sort(unique(predictor))
thresholds <- (c(-Inf, unique.candidates) + c(unique.candidates, +Inf))/2
if (any(o <- outer(thresholds, predictor, `==`))) {
# If we get here, some thresholds are identical to the predictor
# This is caused by near numeric ties that caused the mean to equal
# one of the candidate
# We need to make sure we select the right threshold more carefully
if (direction == '>') {
# We have:
# tp <- sum(cases <= threshold)
# tn <- sum(controls > threshold)
# We need to make sure the selected threshold
# Corresponds to the lowest observation of the predictor
# Identify problematic thresholds
# rows <- which(apply(o, 1, any))
for (row in which(apply(o, 1, any))) {
if (thresholds[row] == unique.candidates[row - 1]) {
# We're already good, nothing to do
}
else if (thresholds[row] == unique.candidates[row]) {
thresholds[row] <- unique.candidates[row - 1]
}
else {
stop("Couldn't fix near ties in thresholds: %s, %s, %s, %s. This is a bug in pROC, please report it to the maintainer.", thresholds[row], unique.candidates[row - 1], unique.candidates[row], direction)
}
}
}
else if (direction == '<') {
# We have:
# tp <- sum(cases >= threshold)
# tn <- sum(controls < threshold)
# We need to make sure the selected threshold
# Corresponds to the highest observation of the predictor
# Identify the problematic thresholds:
# rows <- which(apply(o, 1, any))
for (row in which(apply(o, 1, any))) {
if (thresholds[row] == unique.candidates[row - 1]) {
# Easy to fix: should be unique.candidates[row]
thresholds[row] <- unique.candidates[row]
} else if (thresholds[row] == unique.candidates[row]) {
# We're already good, nothing to do
}
else {
stop("Couldn't fix near ties in thresholds: %s, %s, %s, %s. This is a bug in pROC, please report it to the maintainer.", thresholds[row], unique.candidates[row - 1], unique.candidates[row], direction)
}
}
}
}
return(thresholds)
}

Expand Down

0 comments on commit 87fceab

Please sign in to comment.