Commit afb4f400 authored by Philippe Veber's avatar Philippe Veber
Browse files

scripts/recall_precision_curve: fixed computation

parent dfb808e8
......@@ -6,24 +6,15 @@ library("cowplot")
args = commandArgs(trailingOnly=TRUE)
calc_TN_FP_TP_FN = function(scores, labels){
n <- length(scores)
perm <- order(scores)
N <- sum(labels == 0)
P <- sum(labels == 1)
TP <- rep(P, n)
FP <- rep(N, n)
thresholds <- unique(sort(c(0,1,scores)))
n <- length(thresholds)
TP <- rep(0, n)
FP <- rep(0, n)
FN <- rep(0, n)
for(i in 2:n) {
if(labels[perm[i]] == 1) {
TP[perm[i]] <- TP[perm[i - 1]] - 1
FP[perm[i]] <- FP[perm[i - 1]]
FN[perm[i]] <- FN[perm[i - 1]] + 1
}
else {
TP[perm[i]] <- TP[perm[i - 1]]
FP[perm[i]] <- FP[perm[i - 1]] - 1
FN[perm[i]] <- FN[perm[i - 1]]
}
for(i in 1:n) {
TP[i] <- sum(labels == 1 & scores >= thresholds[i])
FP[i] <- sum(labels == 0 & scores >= thresholds[i])
FN[i] <- sum(labels == 1 & scores < thresholds[i])
}
return(data.frame(TP = TP, FP = FP, FN = FN))
}
......@@ -34,17 +25,14 @@ main <- function(fn, dest) {
d <- read.table(fn, sep = '\t', header = T)
labels <- colnames(d)[!(colnames(d) %in% c("Sites", "Oracle"))]
colors = c(c("#984EA3","#4AA947","#377EB8","#E41A1C","#F5BE5B","#90EE90","#8B6914","#BFBFBF","#0000FF","#FFC0CB","#FFA500","#FFFF00")[1:length(labels)], c("#7F7F7F","#ADD8E6"))
recall <- c()
precision <- c()
for(l in labels) {
rp_by_meth <- lapply(labels, function(l) {
r <- calc_TN_FP_TP_FN(d[,c(l)], d$Oracle)
recall <- c(recall, r$TP / (r$TP + r$FN))
precision <- c(precision, r$TP / (r$TP + r$FP))
}
dd <- melt(d, id.vars = "Sites", variable.name = "method")
dd <- dd[dd$method != "Oracle",]
dd$recall <- recall
dd$precision <- precision
recall <- r$TP / (r$TP + r$FN)
precision <- r$TP / (r$TP + r$FP)
precision[is.nan(precision)] <- 1
data.frame(recall = recall, precision = precision, method = rep(l, length(recall)))
})
dd <- do.call("rbind", rp_by_meth)
plot <-
ggplot(dd, aes(x=recall, y=precision, col=method)) +
theme_bw() +
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment