R/runAll.R

Defines functions runAll

Documented in runAll

#' Run doClsutering() for all variables in data.frame and save plots.
#'
#' @param data A data.frame containing the analysed variables. All variables
#' must be factors or logical.
#' @param bad  A character name of the column of independent variable of data.
#' @param distanceMethod A character, check ?dist.
#' @param clustMethod A character, check ?hclust.
#' @param dir A directory where function saves the images.
#' @param eps A number from 1 to 2 which manage how many cluster function returns.
#' @param sen A number of sensitive what is the less value of observations in a band. If the number of observation is lower than sen then function returns warning.
#' @return A data.frame with summaries of Clusterrr objects for all variables. Side-effect are plots of Clusterrr save in dir.
#' @examples
#' data(lendclub)
#' #runAll(lendclub, "loan_status", dir = getwd())
#' @export
#' @importFrom grDevices dev.list
#' @importFrom grDevices dev.off
#' @importFrom grDevices png


runAll <- function(data, bad, distanceMethod = "euclidean",
                   clustMethod = "ward.D2",
                   dir = getwd(), eps = 1.05, sen = 1000){

  on.exit(if(is.null(dev.list()) == F){ dev.off()}, add=T)

  if(!is.data.frame(data)){
    stop("data must be data.frame")
  }
  if(!is.logical(data[, bad])){
    stop("yVar must be logical")
  }

  m <- ncol(data)
  result <- as.data.frame(matrix(0, nrow = m, ncol = 3))
  colnames(result) <- c("iv","k","warning")
  for(i in 1:m){ #for each variable in data.frame
    name = colnames(data)[i]
    tryCatch({
      x <- doClustering(data, name, bad, distanceMethod, clustMethod,
                        eps, sen)
      png(paste0(dir,"/", name, ".png", sep=""), height=1500, width = 1500,
          pointsize = 27)
      plot(x)
      result[i,] <- summary(x)
      rownames(result)[i] <- name
    }, error = function(e){
      message(paste0(name, "-failed:\n",e))
    }, finally = {
      try(dev.off(), silent=T)
    })
  }
  result
}
wojciechoblak/varbinq documentation built on May 4, 2019, 9:46 a.m.