mirror of
https://github.com/johrpan/geposan.git
synced 2025-10-26 10:47:25 +01:00
distance: Switch from median to density estimate
This commit is contained in:
parent
98ee52b122
commit
aee77e8bb5
5 changed files with 27 additions and 27 deletions
24
R/utils.R
24
R/utils.R
|
|
@ -40,6 +40,30 @@ num <- function(number, digits) {
|
|||
format(round(number, digits = digits), nsmall = digits)
|
||||
}
|
||||
|
||||
#' Find the densest value in the data.
|
||||
#'
|
||||
#' This function assumes that data represents a continuous variable and finds
|
||||
#' a single value with the highest estimated density. This can be used to
|
||||
#' estimate the mode of the data. If there is only one value that value is
|
||||
#' returned. If multiple density maxima with the same density exist, their mean
|
||||
#' is returned.
|
||||
#'
|
||||
#' @param data The input data.
|
||||
#'
|
||||
#' @return The densest value of data.
|
||||
#'
|
||||
#' @export
|
||||
densest <- function(data) {
|
||||
as.numeric(if (length(data) <= 0) {
|
||||
NULL
|
||||
} else if (length(data) == 1) {
|
||||
data
|
||||
} else {
|
||||
density <- stats::density(data)
|
||||
mean(density$x[density$y == max(density$y)])
|
||||
})
|
||||
}
|
||||
|
||||
# This is needed to make data.table's symbols available within the package.
|
||||
#' @import data.table
|
||||
NULL
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue