Skip to contents

Classification based on the Non-Parametric Naive Bayes model.

Usage

# S3 method for 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.

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.

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.

References

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

Author

Michal Majka, michalmajka@hotmail.com

Examples

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"))
#> [1] setosa setosa setosa setosa setosa setosa
#> Levels: setosa versicolor virginica
head(nnb %class% M)
#> [1] setosa setosa setosa setosa setosa setosa
#> Levels: setosa versicolor virginica

### Posterior probabilities
head(predict(nnb, newdata = M, type = "prob"))
#>      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
head(nnb %prob% M)
#>      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