R/labeling_around_zero.R

Defines functions labeling_around_zero

Documented in labeling_around_zero

#' @title Laveling around zero
#'
#' @description Make 3 labels: 1, 0, -1, where 0 are returns closest to zero
#'
#' @param x returns
#'
#' @return factor vector of labels
#'
#' @export
labeling_around_zero <- function(x) {
  x_abs <- abs(x)
  bin <- cut(x_abs, quantile(x_abs, probs = c(0, 0.3333)), labels = 0L, include.lowest = TRUE)
  max_0 <- max(x[bin == 0], na.rm = TRUE)
  min_0 <- min(x[bin == 0], na.rm = TRUE)
  levels(bin) <- c(levels(bin), 1L, -1L)
  bin[x > max_0] <- as.character(1L)
  bin[x < min_0] <- as.factor(-1)
  return(bin)
}
MislavSag/mlfinance documentation built on Sept. 14, 2021, 1:11 p.m.