R/get_logratio.R

Defines functions get_logratio

Documented in get_logratio

#' Calculate the log-ratio of the index data.
#'
#' @param data A data frame or vector of index data. Data should be
#' on the normal scale and will be transformed to the log scale.
#'
#' @importFrom utils tail
#' @return A vector of values log-index values.
#'
#' @examples
#' data <- data.frame("first" = rlnorm(10), "second" = rlnorm(10))
#' ratios <- get_logratio(data)
#'
#' @author Kelli F. Johnson
#' @export
#'
get_logratio <- function(data) {
  # Create a data frame from a vector to allow for vectorization
  if (is.vector(data)) data <- as.matrix(data)

  # Calculate the log ratio
  out <- apply(data, 2, function(x) {
    log(utils::tail(x, 1)) - log(x[1])
  })
  return(out)
}
nwfsc-assess/VAST_WestCoast documentation built on July 4, 2023, 5:45 p.m.