mirror of
https://github.com/johrpan/geposan.git
synced 2025-10-26 18:57:25 +01:00
ranking: Allow negative weights
This commit is contained in:
parent
47a26da94a
commit
88d6837fee
1 changed files with 16 additions and 6 deletions
22
R/ranking.R
22
R/ranking.R
|
|
@ -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)))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue