R/ML_NaiveBayesModel.R

Defines functions NaiveBayesModel

Documented in NaiveBayesModel

#' Naive Bayes Classifier Model
#'
#' Computes the conditional a-posterior probabilities of a categorical class
#' variable given independent predictor variables using Bayes rule.
#'
#' @param laplace positive numeric controlling Laplace smoothing.
#'
#' @details
#' \describe{
#'   \item{Response types:}{\code{factor}}
#' }
#'
#' Further model details can be found in the source link below.
#'
#' @return \code{MLModel} class object.
#'
#' @seealso \code{\link[e1071]{naiveBayes}}, \code{\link{fit}},
#' \code{\link{resample}}
#'
#' @examples
#' \donttest{
#' ## Requires prior installation of suggested package e1071 to run
#'
#' fit(Species ~ ., data = iris, model = NaiveBayesModel)
#' }
#'
NaiveBayesModel <- function(laplace = 0) {

  MLModel(

    name = "NaiveBayesModel",
    label = "Naive Bayes Classifier",
    packages = "e1071",
    response_types = "factor",
    predictor_encoding = "model.frame",
    na.rm = FALSE,
    params = new_params(environment()),

    fit = function(formula, data, weights, ...) {
      eval_fit(
        data,
        formula = e1071::naiveBayes(
          formula, data = data, na.action = na.pass, ...
        ),
        matrix = e1071::naiveBayes(x, y, ...)
      )
    },

    predict = function(object, newdata, ...) {
      newdata <- as.data.frame(newdata)
      predict(object, newdata = newdata, type = "raw")
    }

  )

}

MLModelFunction(NaiveBayesModel) <- NULL

Try the MachineShop package in your browser

Any scripts or data that you put into this service are public.

MachineShop documentation built on Sept. 18, 2023, 5:06 p.m.