naiveBayes

0th

Percentile

Naive Bayes Classifier

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

Keywords
classif, category
Usage
## S3 method for class 'formula':
naiveBayes(formula, data, laplace = 0, ..., subset, na.action = na.pass)
## S3 method for class 'default':
naiveBayes(x, y, laplace = 0, ...)

## S3 method for class 'naiveBayes': predict(object, newdata, type = c("class", "raw"), threshold = 0.001, ...)

Arguments
x
A numeric matrix, or a data frame of categorical and/or numeric variables.
y
Class vector.
formula
A formula of the form class ~ x1 + x2 + .... Interactions are not allowed.
data
Either a data frame of predictors (categorical and/or numeric) or a contingency table.
laplace
positive double controlling Laplace smoothing. The default (0) disables Laplace smoothing.
...
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
object
An object of class "naiveBayes".
newdata
A dataframe with new predictors (with possibly fewer columns than the training data). Note that the column names of newdata are matched against the training data ones.
type
If "raw", the conditional a-posterior probabilities for each class are returned, and the class with maximal probability else.
threshold
Value replacing cells with 0 probabilities.
Details

The standard naive Bayes classifier (at least this implementation) assumes independence of the predictor variables, and Gaussian distribution (given the target class) of metric predictors. For attributes with missing values, the corresponding table entries are omitted for prediction.

Value

  • An object of class "naiveBayes" including components:
  • aprioriClass distribution for the dependent variable.
  • tablesA list of tables, one for each predictor variable. For each categorical variable a table giving, for each attribute level, the conditional probabilities given the target class. For each numeric variable, a table giving, for each target class, mean and standard deviation of the (sub-)variable.

Aliases
  • naiveBayes
  • naiveBayes.default
  • naiveBayes.formula
  • print.naiveBayes
  • predict.naiveBayes
Examples
## Categorical data only:
data(HouseVotes84, package="mlbench")
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)

## using laplace smoothing:
model <- naiveBayes(Class ~ ., data = HouseVotes84, laplace = 3)
pred <- predict(model, HouseVotes84[,-1])
table(pred, HouseVotes84$Class)


## Example of using a contingency table:
data(Titanic)
m <- naiveBayes(Survived ~ ., data = Titanic)
m
predict(m, as.data.frame(Titanic)[,1:3])

## Example with metric predictors:
data(iris)
m <- naiveBayes(Species ~ ., data = iris)
## alternatively:
m <- naiveBayes(iris[,-5], iris[,5])
m
table(predict(m, iris[,-5]), iris[,5])
Documentation reproduced from package e1071, version 1.5-20, License: GPL-2

Community examples

Looks like there are no examples yet.