predict.naive_bayes: Predict Method for naive_bayes Objects

predict.naive_bayesR Documentation

Predict Method for naive_bayes Objects


Classification based on Naive Bayes models.


## S3 method for class 'naive_bayes'
predict(object, newdata = NULL, type = c("class","prob"),
  threshold = 0.001, eps = 0, ...)



object of class inheriting from "naive_bayes".


matrix or dataframe with categorical (character/factor/logical) or metric (numeric) predictors.


if "class", new data points are classified according to the highest posterior probabilities. If "prob", the posterior probabilities for each class are returned.


value by which zero probabilities or probabilities within the epsilon-range corresponding to metric variables are replaced (zero probabilities corresponding to categorical variables can be handled with Laplace (additive) smoothing).


value that specifies an epsilon-range to replace zero or close to zero probabilities by threshold. It applies to metric variables.


not used.


Computes conditional posterior probabilities for each class label using the Bayes' rule under the assumption of independence of predictors. If no new data is provided, the data from the object is used. Logical variables are treated as categorical (binary) variables. Predictors with missing values are not included into the computation of posterior probabilities.


predict.naive_bayes returns either a factor with class labels corresponding to the maximal conditional posterior probabilities or a matrix with class label specific conditional posterior probabilities.


Michal Majka,

See Also

naive_bayes, plot.naive_bayes, tables, get_cond_dist, %class%


### Simulate example data
n <- 100
data <- data.frame(class = sample(c("classA", "classB"), n, TRUE),
                   bern = sample(LETTERS[1:2], n, TRUE),
                   cat  = sample(letters[1:3], n, TRUE),
                   logical = sample(c(TRUE,FALSE), n, TRUE),
                   norm = rnorm(n),
                   count = rpois(n, lambda = c(5,15)))
train <- data[1:95, ]
test <- data[96:100, -1]

### Fit the model with default settings
nb <- naive_bayes(class ~ ., train)

# Classification
predict(nb, test, type = "class")
nb %class% test

# Posterior probabilities
predict(nb, test, type = "prob")
nb %prob% test

## Not run: 
vars <- 10
rows <- 1000000
y <- sample(c("a", "b"), rows, TRUE)

# Only categorical variables
X1 <-[5:9], vars * rows, TRUE),
                           ncol = vars))
nb_cat <- naive_bayes(x = X1, y = y)
system.time(pred2 <- predict(nb_cat, X1))

## End(Not run)

naivebayes documentation built on June 25, 2024, 1:16 a.m.