R/internal_helpers.R

Defines functions .onUnload class_proportions MODE DANN_distance

Documented in class_proportions DANN_distance MODE

###################################
# Helper functions
###################################
#' Computes the distance between x0 and x1 using the DANN metric
#'
#' @param x0 A numeric matrix with training predictors as columns.
#' @param x1 A numeric matrix with training predictors as columns.
#' @param sigma A numeric matrix defined in Hastie's DANN publication.
#' @keywords internal
DANN_distance <- function(x0, x1, sigma) {
  difference <- x0 - x1
  distance <- difference %*% sigma %*% t(difference)
  return(distance)
}

#' Computes mode.
#' Code found at [Stack Overflow](https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode)
#'
#' @param x A numeric vector.
#' @keywords internal
MODE <- function(x) {
  ux <- sort(unique(x))
  return(ux[which.max(tabulate(match(x, ux)))])
}

#' Computes class proportions
#'
#' @param x A numeric vector.
#' @param possibleValues A vector of all possible values x can contain
#' @keywords internal
class_proportions <- function(x, possibleValues) {
  counts <- purrr::map_dbl(possibleValues, function(i) {
    temp <- x[which(x == i)]
    length(temp)
  })

  out <- counts / sum(counts)
  return(out)
}

#' @keywords internal
.onUnload <- function(libpath) {
  library.dynam.unload("dann", libpath)
}

Try the dann package in your browser

Any scripts or data that you put into this service are public.

dann documentation built on Sept. 23, 2023, 5:06 p.m.