predict.nonparametric_naive_bayes: Predict Method for nonparametric_naive_bayes Objects

predict.nonparametric_naive_bayesR Documentation

Predict Method for nonparametric_naive_bayes Objects


Classification based on the Non-Parametric Naive Bayes model.


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



object of class inheriting from "nonparametric_naive_bayes".


matrix with metric predictors (only numeric matrix accepted).


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


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


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


not used.


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.


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.


Michal Majka,


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%


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)

naivebayes documentation built on June 25, 2024, 1:16 a.m.