predict.gaussian_naive_bayes: Predict Method for gaussian_naive_bayes Objects

predict.gaussian_naive_bayesR Documentation

Predict Method for gaussian_naive_bayes Objects


Classification based on the Gaussian Naive Bayes model.


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



object of class inheriting from "gaussian_naive_bayes".


matrix with metric predictors (only numeric matrix accepted).


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.


This is a specialized version of the Naive Bayes classifier, in which all features take on real values and class conditional probabilities are modelled with the Gaussian distribution.

Class posterior probabilities are calculated using the Bayes' rule under the assumption of independence of predictors. If no newdata is provided, the data from the object is used.

The Gaussian Naive Bayes is available in both, naive_bayes and gaussian_naive_bayes. The implementation of the specialized Naive Bayes provides more efficient performance though. The speedup comes from the restricting the data input to a numeric matrix and performing the linear algebra as well vectorized operations on it. In other words, the efficiency comes at cost of the flexibility.

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

The gaussian_naive_bayes function is equivalent to the naive_bayes function with the numeric matrix or a data.frame containing only numeric variables.


predict.gaussian_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, gaussian_naive_bayes, plot.gaussian_naive_bayes, tables, get_cond_dist, %class%


y <- iris[[5]]
M <- as.matrix(iris[-5])

### Train the Gaussian Naive Bayes
gnb <- gaussian_naive_bayes(x = M, y = y)

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

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

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