R/getHcObj.R

Defines functions getHclustObj

Documented in getHclustObj

#' Extract Hclust object.
#'
#' @param badrate A vector of bad rate value for each band. Can be generated by getBadRate().
#' @param distanceMethod A character value, check ?dist.
#' @param clustMethod A character value, check ?hclust.
#' @return An object of hclust class.
#' @examples
#' data(lendclub)
#' badrate <- getBadRate(lendclub$loan_status, lendclub$home_ownership)
#' hclust <- getHclustObj(badrate)
#' plot(hclust)
#' @export
#' @importFrom stats cutree
#' @importFrom stats dist
#' @importFrom stats hclust


getHclustObj <- function(badrate, distanceMethod = "euclidean",
                         clustMethod = "ward.D2"){
  # checking --------------
  if(any(is.na(badrate))){
    stop("NA detected in badrate. Variables must cover all factor levels.")
  }
  if(any(!is.numeric(badrate))){
    stop("wrong badrate")
  }

  # calculation -----------
  d  <- dist(badrate, method = distanceMethod)
  hclust(dist(d), clustMethod)
}
wojciechoblak/varbinq documentation built on May 4, 2019, 9:46 a.m.