#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.