R/auxiliary.R

Defines functions check_counts check_alpha

## auxiliary functions for 'T4entropy' package
#   (1) check_counts : one of 'vector', 'table', or 'histogram'
#   (2) check_alpha  : all should be positive values of fixed length



# (1) check_counts --------------------------------------------------------
#' @keywords internal
check_counts <- function(input, fnname){
  if (inherits(input, 'histogram')){
    return(round(input$counts))
  } else if (inherits(input, 'table')){
    return(round(as.double(input)))
  } else if (is.vector(input)){
    xx = round(input)
    if ((any(is.na(xx)))||(any(is.infinite(xx)))||(any(xx<0))){
      stop(paste0("* ",fnname," : the input object should contain no NAs, Infs, or negative numbers."))
    }
    return(xx)
  } else {
    stop(paste0("* ",fnname," : the input object should be an instance of following objects; 'histogram','table', or 'vector'."))
  }
}

# (2) check_alpha ---------------------------------------------------------
#' @keywords internal
check_alpha <- function(alpha, K, fnname){
  if (length(alpha)==1){
    alpha = rep(alpha, K)
  }
  if (length(alpha)!=K){
    stop(paste0("* ",fnname," : 'alpha' should be a vector of length ",K," in this case."))
  }
  if (any(alpha<=0)){
    stop(paste0("* ",fnname," : 'alpha' should have positive real numbers only."))
  }
  return(alpha)
}
kyoustat/T4entropy documentation built on March 6, 2020, 12:56 a.m.