R/s.BART.R

Defines functions s.BART

Documented in s.BART

# s.BART.R
# ::rtemis::
# 2016 Efstathios D. Gennatas egenn.github.io

#' Bayesian Additive Regression Trees [C, R]
#'
#' Trains a Bayesian Additive Regression Tree (BART) model using package \code{bartMachine} and validates it
#'
#' If you are having trouble with rJava in Rstudio on macOS, see my solution here:
#' https://support.rstudio.com/hc/en-us/community/posts/203663956/comments/249073727
#' \code{bartMachine} does not support case weights
#' @inheritParams s.GLM
#' @param save.mod Logical: if TRUE, sets \code{bartMachine}'s \code{serialize} to TRUE and saves model to \code{outdir}
#' @param ... Additional arguments to be passed to \code{bartMachine::bartMachine}
#' @return Object of class \pkg{rtemis}
#' @author Efstathios D. Gennatas
#' @seealso \link{elevate} for external cross-validation
#' @family Supervised Learning
#' @family Tree-based methods
#' @export

s.BART <- function(x, y = NULL,
                   x.test = NULL, y.test = NULL,
                   x.name = NULL, y.name = NULL,
                   n.trees = c(100, 200),
                   k_cvs = c(2, 3),
                   nu_q_cvs = list(c(3, 0.9), c(10, 0.75)),
                   k_folds = 5,
                   n.burnin = 250,
                   n.iter = 1000,
                   n.cores = rtCores,
                   upsample = FALSE,
                   downsample = FALSE,
                   resample.seed = NULL,
                   print.plot = TRUE,
                   plot.fitted = NULL,
                   plot.predicted = NULL,
                   plot.theme = getOption("rt.fit.theme", "lightgrid"),
                   question = NULL,
                   rtclass = NULL,
                   verbose = TRUE,
                   trace = 0,
                   outdir = NULL,
                   save.mod = ifelse(!is.null(outdir), TRUE, FALSE),
                   java.mem.size = 12, ...) {

  # [ INTRO ] ====
  if (missing(x)) { print(args(s.RF)); return(invisible(9)) }
  if (!is.null(outdir)) outdir <- normalizePath(outdir, mustWork = FALSE)
  logFile <- if (!is.null(outdir)) {
    paste0(outdir, "/", sys.calls()[[1]][[1]], ".", format(Sys.time(), "%Y%m%d.%H%M%S"), ".log")
  } else {
    NULL
  }
  start.time <- intro(verbose = verbose, logFile = logFile)
  mod.name <- "BART"

  # [ DEPENDENCIES ] ====
  if (!depCheck("bartMachine", verbose = FALSE)) {
    cat("\n"); stop("Please install dependencies and try again")
  }

  # [ ARGUMENTS ] ====
  if (missing(x)) { print(args(s.BART)); stop("x is missing") }
  if (is.null(y) & NCOL(x) < 2) { print(args(s.BART)); stop("y is missing") }
  if (is.null(x.name)) x.name <- getName(x, "x")
  if (is.null(y.name)) y.name <- getName(y, "y")
  if (!verbose) print.plot <- FALSE
  verbose <- verbose | !is.null(logFile)
  if (save.mod & is.null(outdir)) outdir <- paste0("./s.", mod.name)
  if (!is.null(outdir)) outdir <- paste0(normalizePath(outdir, mustWork = FALSE), "/")

  # [ DATA ] ====
  dt <- dataPrepare(x, y,
                    x.test, y.test,
                    upsample = upsample,
                    downsample = downsample,
                    resample.seed = resample.seed,
                    verbose = verbose)
  x <- dt$x
  y <- dt$y
  x.test <- dt$x.test
  y.test <- dt$y.test
  xnames <- dt$xnames
  type <- dt$type
  if (verbose) dataSummary(x, y, x.test, y.test, type)
  if (print.plot) {
    if (is.null(plot.fitted)) plot.fitted <- if (is.null(y.test)) TRUE else FALSE
    if (is.null(plot.predicted)) plot.predicted <- if (!is.null(y.test)) TRUE else FALSE
  } else {
    plot.fitted <- plot.predicted <- FALSE
  }
  # For multinomial classification, must provide integer
  if (type == "Classification") {
    if (length(levels(y)) > 2) {
      y.train <- as.integer(y)
    } else {
      # bartMachine considers first level negative, second positive
      y.train <- factor(y, levels = rev(levels(y)))
      nu_q_cvs <- NULL
    }
  } else {
    y.train <- y
  }

  # [ BART ] ====
  java.mem <- paste0("-Xmx", java.mem.size, "g")
  options(java.parameters = java.mem)
  bartMachine::set_bart_machine_num_cores(n.cores)
  if (verbose) msg("Training Bayesian Additive Regression Trees...", newline.pre = TRUE)
  mod <- bartMachine::bartMachineCV(x, y.train,
                                    num_tree_cvs = n.trees,
                                    k_cvs = k_cvs,
                                    nu_q_cvs = nu_q_cvs,
                                    k_folds = k_folds,
                                    num_burn_in = n.burnin,
                                    num_iterations_after_burn_in = n.iter,
                                    serialize = save.mod,
                                    verbose = trace > 0, ...)
  if (trace > 0) summary(mod)

  # [ FITTED ] ====
  if (type == "Classification") {
    fitted.prob <- predict(mod, x, type = "prob")
    fitted <- factor(levels(y)[round(fitted.prob) + 1], levels = levels(y))
  } else {
    fitted.prob <- NULL
    fitted <- as.numeric(predict(mod, x))
  }
  error.train <- modError(y, fitted)
  if (verbose) errorSummary(error.train, mod.name)

  # [ PREDICTED ] ====
  if (!is.null(x.test) & !is.null(y.test)) {
    if (type == "Classification") {
      predicted.prob <- predict(mod, x.test, type = "prob")
      predicted <- factor(levels(y)[round(predicted.prob) + 1], levels = levels(y))
    } else {
      predicted.prob <- NULL
      predicted <- as.numeric(predict(mod, x.test))
    }
    error.test <- modError(y.test, predicted)
    if (verbose) errorSummary(error.test, mod.name)
  } else {
    predicted <- error.test <- NULL
  }

  # [ OUTRO ] ====
  rt <- rtModSet(rtclass = rtclass,
                 mod = mod,
                 mod.name = mod.name,
                 type = type,
                 y.train = y,
                 y.test = y.test,
                 x.name = x.name,
                 y.name = y.name,
                 xnames = xnames,
                 fitted = fitted,
                 fitted.prob = fitted.prob,
                 se.fit = NULL,
                 error.train = error.train,
                 predicted = predicted,
                 predicted.prob = predicted.prob,
                 se.prediction = NULL,
                 error.test = error.test,
                 question = question)

  rtMod.out(rt,
            print.plot,
            plot.fitted,
            plot.predicted,
            y.test,
            mod.name,
            outdir,
            save.mod,
            verbose,
            plot.theme)

  outro(start.time, verbose = verbose, sinkOff = ifelse(is.null(logFile), FALSE, TRUE))
  rt

} # rtemis::s.BART
egenn/rtemis documentation built on March 25, 2020, 3:28 p.m.