spsanderson / healthyR.ai

healthyR.ai - AI package for the healthyverse
http://www.spsanderson.com/healthyR.ai/
Other
16 stars 6 forks source link

Add vector functions for Kurtosis and Skewness #132

Closed spsanderson closed 2 years ago

spsanderson commented 2 years ago
hai_kurtosis_vec <- function(.x){

  # Tidyeval ----
  x_term <- .x

  # Checks ----
  if(!is.numeric(x_term)){
    stop(call. = FALSE, ".x must be a numeric vector.")
  }

  # Calculate
  n              <- length(x_term)
  mu             <- mean(x_term, na.rm = TRUE)
  n_diff         <- (x_term - mu)^4
  nu             <- (1/n * sum(n_diff))
  d_diff         <- (x_term - mu)^2
  de             <- (1/n * sum(d_diff))^2
  k              <- nu/de
  return(k)
  print(k)
}

hai_skewness_vec <- function(.x){

  # Tidyeval ----
  x_term <- .x

  # Checks ----
  if(!is.numeric(x_term)){
    stop(call. = FALSE, ".x must be a numeric vector.")
  }

  # Calculate
  n      <- length(x_term)
  mu     <- mean(x_term, na.rm = TRUE)
  n_diff <- (x_term - mu)^3
  nu     <- (1/n * sum(n_diff))
  d_diff <- (x_term - mu)^2
  de     <- (1/n * sum(d_diff))^(3/2)
  s      <- nu/de
  return(s)
  print(s)
}
spsanderson commented 2 years ago

Use this link for the formula

https://www.geeksforgeeks.org/skewness-and-kurtosis-in-r-programming/