mirror of
https://github.com/johrpan/geposanui.git
synced 2025-10-26 19:27:24 +01:00
Add optimizer and buttons to disable methods
This commit is contained in:
parent
529f4a553b
commit
8104e9bd8a
3 changed files with 98 additions and 15 deletions
34
optimize.R
Normal file
34
optimize.R
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#' Find the best weights to rank the data.
|
||||
#'
|
||||
#' This function ranks the provided data table based on a weighted score
|
||||
#' computed from the specified `columns`. It tries to find the optimal weights
|
||||
#' that result in a ranking, where the mean rank of the given reference genes
|
||||
#' is as high as possible.
|
||||
#'
|
||||
#' @param data Input data including the columns.
|
||||
#' @param colums Columns containing the separate scores between 0.0 and 1.0.
|
||||
#' @param reference_gene_ids IDs of the reference genes within the input data.
|
||||
#'
|
||||
#' @returns Vector of optimal column weights adding up to 1.0.
|
||||
optimize_weights <- function(data, columns, reference_gene_ids) {
|
||||
#' Compute the mean rank of the reference genes when applying the weights.
|
||||
mean_rank <- function(weights) {
|
||||
data <- copy(data)
|
||||
data[, score := 0.0]
|
||||
|
||||
for (i in seq_along(columns)) {
|
||||
column <- columns[i]
|
||||
weighted <- weights[i] * data[, ..column]
|
||||
data[, score := score + weighted]
|
||||
}
|
||||
|
||||
setorder(data, -score)
|
||||
data[, rank := .I]
|
||||
|
||||
data[gene %chin% reference_gene_ids, mean(rank)]
|
||||
}
|
||||
|
||||
weights <- optim(rep(1.0, length(columns)), mean_rank)$par
|
||||
total_weight <- sum(weights)
|
||||
weights / total_weight
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue