predict.nonparametric_naive_bayes: Predict Method for nonparametric_naive_bayes Objects

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

Classification based on the Non-Parametric Naive Bayes model.

Usage

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

Arguments

object

object of class inheriting from "nonparametric_naive_bayes".

newdata

matrix with metric predictors (only numeric matrix accepted).

type

if "class", new data points are classified according to the highest posterior probabilities. If "prob", the posterior probabilities for each class are returned.

threshold

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).

eps

value that specifies an epsilon-range to replace zero or close to zero probabilities by threshold. It applies to metric variables.

...

not used.

Details

This is a specialized version of the Naive Bayes classifier, in which all features take on real values (numeric/integer) and class conditional probabilities are non-parametrically estimated with kernel density estimator. By default Gaussian kernel is used and the smoothing bandwidth is selected according to the Silverman's 'rule of thumb'. For more details, please see the references and the documentation of density and bw.nrd0.

The Non-Parametric Naive Bayes is available in both, naive_bayes() and nonparametric_naive_bayes(). This specialized implementation of the Naive Bayes does not provide a substantial speed-up over the general naive_bayes() function but it should be more transparent and user friendly.

The nonparametric_naive_bayes function is equivalent to naive_bayes() when the numeric matrix or a data.frame contains only numeric variables and usekernel = TRUE.

The missing values (NAs) are omitted during the parameter estimation. The NAs in the newdata in predict.nonparametric_naive_bayes() are not included into the calculation of posterior probabilities; and if present an informative warning is given.

Value

predict.nonparametric_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

References

Silverman, B. W. (1986). Density Estimation for Statistics and Data Analysis. Chapman & Hall.

See Also

naive_bayes, nonparametric_naive_bayes, plot.nonparametric_naive_bayes, tables, get_cond_dist, naive_bayes, %class%

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
data(iris)
y <- iris[[5]]
M <- as.matrix(iris[-5])

### Train the Non-Parametric Naive Bayes
nnb <- nonparametric_naive_bayes(x = M, y = y, bw = "SJ")

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

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

Example output

naivebayes 0.9.7 loaded
[1] setosa setosa setosa setosa setosa setosa
Levels: setosa versicolor virginica
[1] setosa setosa setosa setosa setosa setosa
Levels: setosa versicolor virginica
     setosa   versicolor    virginica
[1,]      1 6.116221e-10 2.047042e-11
[2,]      1 7.759481e-09 1.852369e-10
[3,]      1 3.286125e-09 1.637165e-10
[4,]      1 2.327491e-09 1.151089e-10
[5,]      1 1.910768e-10 1.597211e-11
[6,]      1 5.298859e-10 1.044005e-09
     setosa   versicolor    virginica
[1,]      1 6.116221e-10 2.047042e-11
[2,]      1 7.759481e-09 1.852369e-10
[3,]      1 3.286125e-09 1.637165e-10
[4,]      1 2.327491e-09 1.151089e-10
[5,]      1 1.910768e-10 1.597211e-11
[6,]      1 5.298859e-10 1.044005e-09

naivebayes documentation built on March 13, 2020, 1:31 a.m.