Learn R Programming

e1071 (version 1.3-1)

naiveBayes: Naive Bayes Classifier

Description

Computes the conditional a-posterior probabilities of a categorical class variable given independent categorical predictor variables using the Bayes rule.

Usage

naiveBayes(formula, data, ..., subset, na.action = na.pass)

Arguments

formula
A formula of the form class ~ x1 + x2 + .... Interactions are not allowed.
data
Either a data frame of factors or a contingency table.
...
Currently not used.
subset
For data given in a data frame, an index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.)
na.action
A function to specify the action to be taken if NAs are found. The default action is not to count them for the computation of the probability factors. An alternative is na.omit, which leads to rejection of cases

Value

  • An object of class "naiveBayes" including components:
  • aprioriClass distribution for the dependent variable.
  • tablesA list of probability tables, one for each predictor variable, giving, for each attribute level, the conditional probabilities given the predictor classes.

See Also

predict.naiveBayes

Examples

Run this code
data(HouseVotes84)
model <- naiveBayes(Class ~ ., data = HouseVotes84)
predict(model, HouseVotes84[1:10,-1])
predict(model, HouseVotes84[1:10,-1], type = "raw")

pred <- predict(model, HouseVotes84[,-1])
table(pred, HouseVotes84$Class)

data(Titanic)
m <- naiveBayes(Survived ~ ., data = Titanic)
m
predict(m, as.data.frame(Titanic)[,1:3])

Run the code above in your browser using DataLab