predict.bernoulli_naive_bayes {naivebayes} | R Documentation |
Predict Method for bernoulli_naive_bayes Objects
Description
Classification based on the Bernoulli Naive Bayes model.
Usage
## S3 method for class 'bernoulli_naive_bayes'
predict(object, newdata = NULL, type = c("class","prob"), ...)
Arguments
object |
object of class inheriting from |
newdata |
matrix with numeric 0-1 predictors. |
type |
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. |
Details
This is a specialized version of the Naive Bayes classifier, in which all features take on numeric 0-1 values and class conditional probabilities are modelled with the Bernoulli 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 Bernoulli Naive Bayes is available in both, naive_bayes
and bernoulli_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 0-1 matrix and performing the linear algebra as well as 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 bernoulli_naive_bayes
function is equivalent to the naive_bayes
function with the numeric 0-1 matrix being coerced, for instance, to the "0"-"1" character matrix.
Value
predict.bernoulli_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.
Author(s)
Michal Majka, michalmajka@hotmail.com
See Also
naive_bayes
, bernoulli_naive_bayes
, plot.bernoulli_naive_bayes
, tables
, get_cond_dist
, %class%
Examples
cols <- 10 ; rows <- 100 ; probs <- c("0" = 0.4, "1" = 0.1)
M <- matrix(sample(0:1, rows * cols, TRUE, probs), 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 <- 0.5
### Train the Bernoulli Naive Bayes
bnb <- bernoulli_naive_bayes(x = M, y = y, laplace = laplace)
### Classification
head(predict(bnb, newdata = M, type = "class"))
head(bnb %class% M)
### Posterior probabilities
head(predict(bnb, newdata = M, type = "prob"))
head(bnb %prob% M)