Learn R Programming

bnlearn (version 2.9)

naive.bayes: Discrete naive Bayes classifiers

Description

Create, fit and perform predictions with naive Bayes classifiers.

Usage

naive.bayes(training, explanatory, data)
## S3 method for class 'bn.naive':
predict(object, data, prior, ..., debug = FALSE)

Arguments

training
a character string, the label of the training variable.
explanatory
a vector of character strings, the labels of the explanatory variables.
object
an object of class bn.naive, either fitted or not.
data
a data frame containing the variables in the model.
prior
a numeric vector, the prior distribution for the training variable. It is automatically normalized if not already so.
...
extra arguments from the generic method (currently ignored).
debug
a boolean value. If TRUE a lot of debugging output is printed; otherwise the function is completely silent.

Value

  • naive.bayes returns an object of class c("bn.naive", "bn"), which behaves like a normal bn object unless passed to predict.

    predict returns a factor with the same levels as the training variable from data.

Details

The naive.bayes functions creates the star-shaped Bayesian network form of a naive Bayes classifier; the training variable (the one holding the group each observation belongs to) is at the center of the star, and it has an outgoing arc for each explanatory variable.

If data is specified, explanatory will be ignored and the labels of the explanatory variables will be extracted from the data.

predict performs a supervised classification of the observations by assigning them to the group with the maximum posterior probability.

References

Borgelt C, Kruse R, Steinbrecher M (2009). Graphical Models: Representations for Learning, Reasoning and Data Mining. Wiley, 2nd edition.

Examples

Run this code
data(learning.test)
bn = naive.bayes("A", LETTERS[2:6])
pred = predict(bn, learning.test)
table(pred, learning.test[, "A"])
#
# pred    a    b    c
#    a 1286  310  178
#    b  192  977  203
#    c  190  383 1281
#
fitted = bn.fit(bn, learning.test, method = "bayes")
pred = predict(fitted, learning.test)
table(pred, learning.test[, "A"])
#
# pred    a    b    c
#    a 1286  310  178
#    b  192  977  203
#    c  190  383 1281
#

Run the code above in your browser using DataLab