ranking: Allow negative weights

This commit is contained in:
Elias Projahn 2021-11-16 16:22:36 +01:00
parent 47a26da94a
commit 88d6837fee

View file

@ -41,7 +41,10 @@ ranking <- function(analysis, weights, min_n_species = 10) {
} }
# Normalize scores to be between 0.0 and 1.0. # Normalize scores to be between 0.0 and 1.0.
ranking[, score := score / sum(unlist(weights))] min_score <- ranking[, min(score)]
max_score <- ranking[, max(score)]
score_range <- max_score - min_score
ranking[, score := (score - min_score) / score_range]
setorder(ranking, -score) setorder(ranking, -score)
ranking[, rank := .I] ranking[, rank := .I]
@ -95,18 +98,25 @@ optimal_weights <- function(analysis, methods, reference_gene_ids,
min_n_species = min_n_species min_n_species = min_n_species
) )
data[gene %chin% reference_gene_ids, if (target == "min") { result <- data[gene %chin% reference_gene_ids, if (target == "min") {
min(rank) min(rank)
} else if (target == "max") { } else if (target == "max") {
max(rank) max(rank)
} else { } else {
mean(rank) mean(rank)
}] }]
if (result > 0) {
result
} else {
Inf
}
} }
factors <- stats::optim(rep(1.0, length(methods)), target_rank)$par factors <- stats::optim(
factors[factors < 0.0] <- 0.0 rep(0.0, length(methods)),
total_weight <- sum(factors) target_rank
)$par
weights(factors / total_weight) weights(factors / max(abs(factors)))
} }