Learn R Programming

bnclassify

Implements algorithms for learning discrete Bayesian network classifiers from data, as well as functions for using these classifiers for prediction, assessing their predictive performance, and inspecting and analyzing their properties.

Example

Load a data set and learn a one-dependence estimator by maximizing Akaike’s information criterion (AIC) score.

library(bnclassify)
data(car)
tn <- tan_cl('class', car, score = 'aic')
tn
#> 
#>   Bayesian network classifier (only structure, no parameters)
#> 
#>   class variable:        class 
#>   num. features:   6 
#>   num. arcs:   9 
#>   learning algorithm:    tan_cl
plot(tn)

After learning the network’s parameters, you can use it to classify data.

tn <- lp(tn, car, smooth = 0.01)
p <- predict(tn, car, prob = TRUE)
head(p)
#>      unacc          acc         good        vgood
#> [1,]     1 3.963694e-09 5.682130e-09 4.269700e-09
#> [2,]     1 1.752769e-09 3.310473e-12 3.236335e-09
#> [3,]     1 3.730170e-09 1.090296e-08 1.800719e-12
#> [4,]     1 3.963694e-09 5.682130e-09 4.269700e-09
#> [5,]     1 4.562294e-09 6.965323e-09 4.536532e-09
#> [6,]     1 4.281155e-09 5.366306e-09 5.168828e-09
p <- predict(tn, car, prob = FALSE)
head(p)
#> [1] unacc unacc unacc unacc unacc unacc
#> Levels: unacc acc good vgood

Estimate predictive accuracy with cross validation.

cv(tn, car, k = 10)
#> [1] 0.9386641

Or compute the log-likelihood

logLik(tn, car)
#> 'log Lik.' -13280.39 (df=131)

Install

Make sure you have at least version 3.2.0 of R. You can install bnclassify from CRAN:

install.packages('bnclassify')

Or get the current development version from Github:

# install.packages('devtools')
devtools::install_github('bmihaljevic/bnclassify')
# devtools::install_github('bmihaljevic/bnclassify', build_vignettes = TRUE)

Ideally, you would use the build_vignettes = TRUE version, and thus get the vignettes, but it requires programs such as texi2dvi to be installed on your side.

Overview

See an overview of the package and examples of usage:

vignette('overview', package = 'bnclassify')
#> Warning: vignette 'overview' not found

See the list of main functionalities.

?bnclassify

Use the usage vignette for more details on the functions.

vignette('usage', package = 'bnclassify')

Then have a look at the remaining vignettes.

browseVignettes("bnclassify")

Copy Link

Version

Install

install.packages('bnclassify')

Monthly Downloads

497

Version

0.4.8

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Last Published

March 13th, 2024

Functions in bnclassify (0.4.8)

check_mlr_attached

Checks if mlr attached.
bnc_aode_bns

Fits an AODE model.
dag

Get underlying graph. This should be exported.
compute_ll

Computes log-likelihood of the model on the provided data.
graph_union

Merges multiple disjoint graphs into a single one.
grain_and_graph

Convert to igraph and gRain.
learn_params

Learn the parameters of a Bayesian network structure.
greedy_wrapper

Learn Bayesian network classifiers in a a greedy wrapper fashion.
loglik

Compute (penalized) log-likelihood.
learn_unprunned_tree

Learns a unpruned rpart recursive partition.
graph_subgraph

Subgraph. Only for a directed graph?
get_log_leaf_entries

Assuming that the cpt is a leaf, returns 1 instead of a CPT entry when value missing
makeRLearner.bnc

makeRLearner. Auxiliary mlr function.
fast_equal

Compares all elements in a to b
graph_named_edge_matrix

Returns an edge matrix with node names (instead of node indices).
get_last

Return last element of x.
forget

Forget a memoized function.
complete_graph

Returns a complete unweighted graph with the given nodes.
direct_forest

Direct an undirected graph.
compute_cll

Computes the conditional log-likelihood of the model on the provided data.
get_null_safe

Get i-th element of x.
nb_dag

Returns a naive Bayes structure
compute_wanbia_weights

graph_get_adjacent

Finds adjacent nodes. Has not been tested much
cpt_vars_values

Get just form first dimension in their own cpt, not checking for consistency in others.
trainLearner.bnc

trainLearner. Auxiliary mlr function.
tan_chowliu

Learns a one-dependence estimator using Chow-Liu's algorithm.
new_cache

Make a new cache.
bootstrap_ss

Return a bootstrap sub-sample.
skip_testing

Skip while testing to isolate errors
spode

Returns a Superparent one-dependence estimator.
graph_is_adjacent

Checks whether nodes are adjacent
local_ode_score_contrib

Returns pairwise component of ODE (penalized) log-likelihood scores. In natural logarithms.
graph_add_edges

Add edges Does not allow edges among adjacent nodes
direct_tree

Direct an undirected graph.
extract_ctgt

Returns a contingency table over the variables.
max_weight_forest

Returns the undirected augmenting forest.
log_normalize

Normalize log probabilities.
plot.bnc_dag

Plot the structure.
map

Assigns instances to the most likely class.
order_acyclic

Provide an acyclic ordering (i.e., a topological sort).
cv

Estimate predictive accuracy with stratified cross validation.
get_ancestors

Based on gRbase::ancestors()
print.bnc_base

Print basic information about a classifier.
skip_assert

Whether to do checks or not. Set TRUE to speed up debugging or building.
inspect_bnc_dag

Inspect a Bayesian network classifier structure.
make_cll_gradient

Returns a function to compute the gradient of negative conditional log-likelihood with respect to feature weights
voting

Congress Voting Data Set.
inspect_bnc_bn

Inspect a Bayesian network classifier (with structure and parameters).
make_cll

Returns a function to compute negative conditional log-likelihood given feature weights
get_but_last

Return all but last element of x.
nb

Learn a naive Bayes network structure.
predict.bnc_fit

Predicts class labels or class posterior probability distributions.
is.memoised

Is it memoized?
identify_min_testing_depths

Identifies the lowest (closest to root) depths at which the features of a classification tree are tested.
memoise_char

Memoise a function.
graph_connected_components

connected_components
is_aode

Is it en AODE?
identify_all_testing_depths

Identifies all depths at which the features of a classification tree are tested.
superparent_children

Return nodes which can be superparents along with their possible children.
subset_by_colnames

Subset a 2D structure by a vector of column names.
predictLearner.bnc

predictLearner. Auxiliary mlr function.
augment_ode_arcs

Returns augmenting arcs that do not invalidate the ODE.
bnc

Learn network structure and parameters.
as_mlr

Convert to mlr.
aode

Learn an AODE ensemble.
accuracy

Compute predictive accuracy.
augment_kdb

Arcs that do not invalidate the k-DB structure
are_factors

Checks if all columns in a data frame are factors.
augment_kdb_arcs

Returns augmenting arcs that do not invalidate the k-DB.
are_pdists

Returns TRUE is x is a valid probability distribution.
augment_ode

Arcs that do not invalidate the tree-like structure
car

Car Evaluation Data Set.
bnc_bn

Bayesian network classifier with structure and parameters.
bnclassify

Learn discrete Bayesian network classifiers from data.
bnc_aode

Returns a c("bnc_aode", "bnc") object.
bnc_dag

Bayesian network classifier structure.
cmi

Compute the (conditional) mutual information between two variables.
cmi_table

Returns the conditional mutual information three variables.