naive.bayes(x, training, explanatory)
# S3 method for bn.naive
predict(object, data, prior, ..., prob = FALSE, debug = FALSE)tree.bayes(x, training, explanatory, whitelist = NULL, blacklist = NULL,
mi = NULL, root = NULL, debug = FALSE)
# S3 method for bn.tan
predict(object, data, prior, ..., prob = FALSE, debug = FALSE)
bn.naive
, either fitted or not.object
.mi
(discrete mutual information) and mi-g
(Gaussian mutual
information).TRUE
the posterior probabilities used
for prediction are attached to the predicted values as an attribute called
prob
.TRUE
a lot of debugging output is
printed; otherwise the function is completely silent.naive.bayes
returns an object of class c("bn.naive", "bn")
,
which behaves like a normal bn
object unless passed to predict
.
tree.bayes
returns an object of class c("bn.tan", "bn")
, which
again behaves like a normal bn
object unless passed to predict
. predict
returns a factor with the same levels as the training
variable from data
. If prob = TRUE
, the posterior probabilities
used for prediction are attached to the predicted values as an attribute
called prob
.naive.bayes
functions creates the star-shaped Bayesian network
form of a naive Bayes classifier; the training variable (the one holding the
group each observation belongs to) is at the center of the star, and it has
an outgoing arc for each explanatory variable. If data
is specified, explanatory
will be ignored and the
labels of the explanatory variables will be extracted from the data. predict
performs a supervised classification of the observations by
assigning them to the group with the maximum posterior probability.data(learning.test)
# this is an in-sample prediction with naive Bayes (parameter learning
# is performed implicitly during the prediction).
bn = naive.bayes(learning.test, "A")
pred = predict(bn, learning.test)
table(pred, learning.test[, "A"])
# this is an in-sample prediction with TAN (parameter learning is
# performed explicitly with bn.fit).
tan = tree.bayes(learning.test, "A")
fitted = bn.fit(tan, learning.test, method = "bayes")
pred = predict(fitted, learning.test)
table(pred, learning.test[, "A"])
# this is an out-of-sample prediction, from a training test to a separate
# test set.
training.set = learning.test[1:4000, ]
test.set = learning.test[4001:5000, ]
bn = naive.bayes(training.set, "A")
fitted = bn.fit(bn, training.set)
pred = predict(fitted, test.set)
table(pred, test.set[, "A"])
Run the code above in your browser using DataLab