From c0a1d965d7da080d5a165a0b1cc61d32f4cd70c8 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Thu, 21 Oct 2021 11:42:44 +0200 Subject: [PATCH] Add more optimization targets --- R/ranking.R | 20 +++++++++++++++----- man/optimize_weights.Rd | 5 ++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/R/ranking.R b/R/ranking.R index a4ab21b..bbbfb51 100644 --- a/R/ranking.R +++ b/R/ranking.R @@ -34,11 +34,14 @@ ranking <- function(results, weights) { #' @param results Results from [analyze()] or [ranking()]. #' @param methods Methods to include in the score. #' @param reference_gene_ids IDs of the reference genes. +#' @param target The optimization target. It may be one of "mean", "min" or +#' "max" and results in the respective rank being optimized. #' #' @returns Named list pairing method names with their optimal weights. #' #' @export -optimize_weights <- function(results, methods, reference_gene_ids) { +optimize_weights <- function(results, methods, reference_gene_ids, + target = "mean") { # Create the named list from the factors vector. weights <- function(factors) { result <- NULL @@ -50,13 +53,20 @@ optimize_weights <- function(results, methods, reference_gene_ids) { result } - # Compute the mean rank of the reference genes when applying the weights. - mean_rank <- function(factors) { + # Compute the target rank of the reference genes when applying the weights. + target_rank <- function(factors) { data <- ranking(results, weights(factors)) - data[gene %chin% reference_gene_ids, mean(rank)] + + data[gene %chin% reference_gene_ids, if (target == "min") { + min(rank) + } else if (target == "max") { + max(rank) + } else { + mean(rank) + }] } - factors <- stats::optim(rep(1.0, length(methods)), mean_rank)$par + factors <- stats::optim(rep(1.0, length(methods)), target_rank)$par total_weight <- sum(factors) weights(factors / total_weight) diff --git a/man/optimize_weights.Rd b/man/optimize_weights.Rd index ed21a06..ea03024 100644 --- a/man/optimize_weights.Rd +++ b/man/optimize_weights.Rd @@ -4,7 +4,7 @@ \alias{optimize_weights} \title{Find the best weights to rank the results.} \usage{ -optimize_weights(results, methods, reference_gene_ids) +optimize_weights(results, methods, reference_gene_ids, target = "mean") } \arguments{ \item{results}{Results from \code{\link[=analyze]{analyze()}} or \code{\link[=ranking]{ranking()}}.} @@ -12,6 +12,9 @@ optimize_weights(results, methods, reference_gene_ids) \item{methods}{Methods to include in the score.} \item{reference_gene_ids}{IDs of the reference genes.} + +\item{target}{The optimization target. It may be one of "mean", "min" or +"max" and results in the respective rank being optimized.} } \value{ Named list pairing method names with their optimal weights.