ranking: Use S3 classes and rename optimize method

This commit is contained in:
Elias Projahn 2021-11-05 14:47:33 +01:00
parent 4792bbaefd
commit 4992bb2930
5 changed files with 55 additions and 31 deletions

View file

@ -2,7 +2,7 @@
S3method(print,geposan_preset) S3method(print,geposan_preset)
export(analyze) export(analyze)
export(optimize_weights) export(optimal_weights)
export(preset) export(preset)
export(ranking) export(ranking)
import(data.table) import(data.table)

View file

@ -1,29 +1,40 @@
#' Rank the results by computing a score. #' Rank the results by computing a score.
#' #'
#' This function takes the result from [analyze()] and creates a score by #' This function takes the result of [analyze()] and creates a score by
#' computing a weighted mean across the different methods' results. #' computing a weighted mean across the different methods' results.
#' #'
#' @param results Results from [analyze()]. #' @param analysis Analysis object resulting from [analyze()].
#' @param weights Named list pairing method names with weighting factors. #' @param weights Named list pairing method names with weighting factors. Only
#' methods that are contained within this list will be included.
#' #'
#' @result The input data with an additional column containing the score and #' @returns A ranking object. The object extends the analysis with additional
#' another column containing the rank. #' columns containing the `score` and the `rank` of each gene. It will be
#' ordered by rank.
#' #'
#' @export #' @export
ranking <- function(results, weights) { ranking <- function(analysis, weights) {
results <- copy(results) if (!"geposan_analysis" %chin% class(analysis)) {
results[, score := 0.0] stop("Invalid analyis. Use geposan::analyze().")
}
ranking <- copy(analysis)
ranking[, score := 0.0]
for (method in names(weights)) { for (method in names(weights)) {
weighted <- weights[[method]] * results[, ..method] weighted <- weights[[method]] * ranking[, ..method]
results[, score := score + weighted] ranking[, score := score + weighted]
} }
# Normalize scores to be between 0.0 and 1.0. # Normalize scores to be between 0.0 and 1.0.
results[, score := score / sum(unlist(weights))] ranking[, score := score / sum(unlist(weights))]
setorder(results, -score) setorder(ranking, -score)
results[, rank := .I] ranking[, rank := .I]
structure(
ranking,
class = c("geposan_ranking", "geposan_analysis", class(ranking))
)
} }
#' Find the best weights to rank the results. #' Find the best weights to rank the results.
@ -31,17 +42,22 @@ ranking <- function(results, weights) {
#' This function finds the optimal parameters to [ranking()] that result in the #' This function finds the optimal parameters to [ranking()] that result in the
#' reference genes ranking particulary high. #' reference genes ranking particulary high.
#' #'
#' @param results Results from [analyze()] or [ranking()]. #' @param analysis Results from [analyze()] or [ranking()].
#' @param methods Methods to include in the score. #' @param methods Methods to include in the score.
#' @param reference_gene_ids IDs of the reference genes. #' @param reference_gene_ids IDs of the reference genes.
#' @param target The optimization target. It may be one of "mean", "min" or #' @param target The optimization target. It may be one of "mean", "min" or
#' "max" and results in the respective rank being optimized. #' "max" and results in the respective rank being optimized.
#' #'
#' @returns Named list pairing method names with their optimal weights. #' @returns Named list pairing method names with their optimal weights. This
#' can be used as an argument to [ranking()].
#' #'
#' @export #' @export
optimize_weights <- function(results, methods, reference_gene_ids, optimal_weights <- function(analysis, methods, reference_gene_ids,
target = "mean") { target = "mean") {
if (!"geposan_analysis" %chin% class(analysis)) {
stop("Invalid analyis. Use geposan::analyze().")
}
# Create the named list from the factors vector. # Create the named list from the factors vector.
weights <- function(factors) { weights <- function(factors) {
result <- NULL result <- NULL
@ -55,7 +71,7 @@ optimize_weights <- function(results, methods, reference_gene_ids,
# Compute the target rank of the reference genes when applying the weights. # Compute the target rank of the reference genes when applying the weights.
target_rank <- function(factors) { target_rank <- function(factors) {
data <- ranking(results, weights(factors)) data <- ranking(analysis, weights(factors))
data[gene %chin% reference_gene_ids, if (target == "min") { data[gene %chin% reference_gene_ids, if (target == "min") {
min(rank) min(rank)

View file

@ -14,9 +14,10 @@ function should accept a number between 0.0 and 1.0 for the current
progress.} progress.}
} }
\value{ \value{
A \link{data.table} with one row for each gene identified by it's ID An object containing the results of the analysis. It contains a
(\code{gene} column). The additional columns contain the resulting scores per \link{data.table} with one row for each gene identified by it's ID (\code{gene}
method and are named after the method IDs. column). The additional columns contain the resulting scores per method
and are named after the method IDs.
} }
\description{ \description{
Analyze by applying the specified preset. Analyze by applying the specified preset.

View file

@ -1,13 +1,13 @@
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ranking.R % Please edit documentation in R/ranking.R
\name{optimize_weights} \name{optimal_weights}
\alias{optimize_weights} \alias{optimal_weights}
\title{Find the best weights to rank the results.} \title{Find the best weights to rank the results.}
\usage{ \usage{
optimize_weights(results, methods, reference_gene_ids, target = "mean") optimal_weights(analysis, methods, reference_gene_ids, target = "mean")
} }
\arguments{ \arguments{
\item{results}{Results from \code{\link[=analyze]{analyze()}} or \code{\link[=ranking]{ranking()}}.} \item{analysis}{Results from \code{\link[=analyze]{analyze()}} or \code{\link[=ranking]{ranking()}}.}
\item{methods}{Methods to include in the score.} \item{methods}{Methods to include in the score.}
@ -17,7 +17,8 @@ optimize_weights(results, methods, reference_gene_ids, target = "mean")
"max" and results in the respective rank being optimized.} "max" and results in the respective rank being optimized.}
} }
\value{ \value{
Named list pairing method names with their optimal weights. Named list pairing method names with their optimal weights. This
can be used as an argument to \code{\link[=ranking]{ranking()}}.
} }
\description{ \description{
This function finds the optimal parameters to \code{\link[=ranking]{ranking()}} that result in the This function finds the optimal parameters to \code{\link[=ranking]{ranking()}} that result in the

View file

@ -4,14 +4,20 @@
\alias{ranking} \alias{ranking}
\title{Rank the results by computing a score.} \title{Rank the results by computing a score.}
\usage{ \usage{
ranking(results, weights) ranking(analysis, weights)
} }
\arguments{ \arguments{
\item{results}{Results from \code{\link[=analyze]{analyze()}}.} \item{analysis}{Analysis object resulting from \code{\link[=analyze]{analyze()}}.}
\item{weights}{Named list pairing method names with weighting factors.} \item{weights}{Named list pairing method names with weighting factors. Only
methods that are contained within this list will be included.}
}
\value{
A ranking object. The object extends the analysis with additional
columns containing the \code{score} and the \code{rank} of each gene. It will be
ordered by rank.
} }
\description{ \description{
This function takes the result from \code{\link[=analyze]{analyze()}} and creates a score by This function takes the result of \code{\link[=analyze]{analyze()}} and creates a score by
computing a weighted mean across the different methods' results. computing a weighted mean across the different methods' results.
} }