predict.multinomial_naive_bayes: Predict Method for multinomial_naive_bayes Objects

predict.multinomial_naive_bayesR Documentation

Predict Method for multinomial_naive_bayes Objects


Classification based on the Multinomial Naive Bayes model.


## S3 method for class 'multinomial_naive_bayes'
predict(object, newdata = NULL, type = c("class","prob"), ...)



object of class inheriting from "multinomial_naive_bayes".


matrix with non-negative integer predictors (only numeric matrix is accepted).


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


not used.


This is a specialized version of the Naive Bayes classifier, where the features represent the frequencies with which events have been generated by a multinomial distribution.

The Multinomial Naive Bayes is not available through the naive_bayes function.

The NAs in the newdata are not included into the calculation of posterior probabilities; and if present an informative warning is given.


predict.multinomial_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,


McCallum, Andrew; Nigam, Kamal (1998). A comparison of event models for Naive Bayes text classification (PDF). AAAI-98 workshop on learning for text categorization. 752.

See Also

multinomial_naive_bayes, tables, get_cond_dist, %class%, coef.multinomial_naive_bayes


### Simulate the data:
cols <- 10 ; rows <- 100
M <- matrix(sample(0:5, rows * cols,  TRUE), nrow = rows, ncol = cols)
y <- factor(sample(paste0("class", LETTERS[1:2]), rows, TRUE, prob = c(0.3,0.7)))
colnames(M) <- paste0("V", seq_len(ncol(M)))
laplace <- 1

### Train the Multinomial Naive Bayes
mnb <- multinomial_naive_bayes(x = M, y = y, laplace = laplace)

# Classification
head(predict(mnb, newdata = M, type = "class"))
head(mnb %class% M)

# Posterior probabilities
head(predict(mnb, newdata = M, type = "prob"))
head(mnb %prob% M)

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