e_log_shift: Log2 nonnegative numbers after offsetting 0 by minimum...

View source: R/e_log_shift.R

e_log_shiftR Documentation

Log2 nonnegative numbers after offsetting 0 by minimum non-zero value.

Description

Default min_add values: If specified, use that. All positive, add 0. Min is 0, add the minimum non-0 value. Min is negative and max is positive, add the minimum non-0 value (so minimum is now the previous non-0 value). Max is negative, add the minimum value plus 1 so log2(x)=0 is the minimum value.

Usage

e_log_shift(x, min_add = NULL, base_log = 2, sw_symmetric = c(FALSE, TRUE)[1])

Arguments

x

numeric vector

min_add

value to add to x before taking log, if not specified and min(x) <= 0, then this is determined automatically

base_log

base for log

sw_symmetric

overrides min_add and chooses constant to give most symmetric distribution after log(x+c)

Value

numeric vector with attributes

Examples


f_print_example <- function(x, x2 = e_log_shift(x)) {
  print(x)
  print(x2)
  par(mfrow = c(1, 2))
  hist(x)
  hist(x2)
  par(mfrow = c(1, 1))
  invisible(NULL)
}


e_log_shift(x = c(0, 1, 2, 3))
e_log_shift(x = c(0, 10, 100, 1000), base = 10)
e_log_shift(x = c(-4, -2, 0, 2, 4))

# Symmetric, positive with right skew
x <- rgamma(100, 1, 1) |> sort()
x_log <- e_log_shift(x, sw_symmetric = TRUE)
  print(x)
  print(x_log)
  par(mfrow = c(1, 3))
  hist(x)
  hist(x_log)
  dat_skew <- tibble::tibble(min_add = seq(0, 1, by = 0.01), skewness = NA)
  for (i_row in seq_len(nrow(dat_skew))) {
    ## i_row = 1
    dat_skew$skewness[i_row] <-
      e_log_shift(x, min_add = dat_skew$min_add[i_row]) |>
      moments::skewness()
  } # i_row
  plot(dat_skew$min_add, dat_skew$skewness, type = "l")
  abline(h = 0, col = "gray50")
  abline(v = attr(x_log, "e_log_shift")["min_add"], col = "gray50")
  par(mfrow = c(1, 1))
  print(c(skew_x = moments::skewness(x), skew_x_log = moments::skewness(x_log)))

# Symmetric, positive with right skew, less than 1
x <- rgamma(100, 1, 1) |> sort()
x <- x / max(x + 1)
x_log <- e_log_shift(x, sw_symmetric = TRUE)
  print(x)
  print(x_log)
  par(mfrow = c(1, 2))
  hist(x)
  hist(x_log)
  par(mfrow = c(1, 1))
  print(c(skew_x = moments::skewness(x), skew_x_log = moments::skewness(x_log)))

# Symmetric, negative with left skew
x <- -rgamma(100, 1, 1) |> sort()
x_log <- e_log_shift(x, sw_symmetric = TRUE)
  print(x)
  print(x_log)
  par(mfrow = c(1, 2))
  hist(x)
  hist(x_log)
  par(mfrow = c(1, 1))
  print(c(skew_x = moments::skewness(x), skew_x_log = moments::skewness(x_log)))

# Symmetric, positive with left skew
x <- -rgamma(100, 1, 1) |> sort()
x <- x + min(x)
x_log <- e_log_shift(x, sw_symmetric = TRUE)
  print(x)
  print(x_log)
  par(mfrow = c(1, 2))
  hist(x)
  hist(x_log)
  par(mfrow = c(1, 1))
  print(c(skew_x = moments::skewness(x), skew_x_log = moments::skewness(x_log)))

erikerhardt/erikmisc documentation built on April 17, 2025, 10:48 a.m.