compare {bnlearn} R Documentation

Compare two or more different Bayesian networks

Description

Compare two different Bayesian networks; compute their Structural Hamming Distance (SHD) or the Hamming distance between their skeletons. Or graphically compare them by plotting them side by side,

Usage

compare(target, current, arcs = FALSE)
## S3 method for class 'bn'
all.equal(target, current, ...)

sid(learned, true, debug = FALSE)
shd(learned, true, wlbl = FALSE, cpdag = TRUE, debug = FALSE)
hamming(learned, true, debug = FALSE)

graphviz.compare(x, ..., groups, layout = "dot", shape = "rectangle",
  fontsize = 12, main = NULL, sub = NULL, diff = "from-first",
  diff.args = list())

Arguments

target, learned

an object of class bn.

current, true

another object of class bn.

...

extra arguments from the generic method (for all.equal(), currently ignored); or a set of one or more objects of class bn (for graphviz.compare).

wlbl

a boolean value. If TRUE, arcs whose directions have been fixed by a whitelist or a blacklist are preserved when constructing the CPDAGs of learned and true.

cpdag

a boolean value. If TRUE, the (CPDAG of) the equivalence class is used instead of the network structures themselves, which is more appropriate if either was learned from data.

debug

a boolean value. If TRUE, a lot of debugging output is printed. Otherwise, the function is completely silent.

arcs

a boolean value. See below.

x

an object of class bn.

groups

a list of character vectors, representing groups of node labels of nodes that should be plotted close to each other.

layout

a character string, the layout argument that will be passed to Rgraphviz. Possible values are "dot", "neato", "twopi", "circo" and "fdp". See Rgraphviz documentation for details.

shape

a character string, the shape of the nodes. Can be "circle", "ellipse" or "rectangle".

fontsize

a positive number, the font size for the node labels.

main

a vector of character strings, one for each network. They are plotted at the top of the corresponding figure(s).

sub

a vector of character strings, the subtitles that are plotted at the bottom of the corresponding figure(s).

diff

a character string, the label of the method used to compare and format the figure(s) created by graphviz.compare(). The default value is "from-first", see below for details.

diff.args

a list of optional arguments to control the formatting of the figure(s) created by graphviz.compare(). See below for details.

Details

graphviz.compare() can visualise differences between graphs in various ways depending on the value of the diff and diff.args arguments:

  • "none": differences are not highlighted.

  • "from-first": the first bn object, x, is taken as the reference network. All the other networks passed via the ... argument are compared to that first network and their true positive, false positive and false negative arcs are highlighted. Colours, line types and line widths for each category of arcs can be specified as the elements of a list via the diff.args argument, with names tp.col, tp.lty, tp.lwd, fp.col, fp.lty, fp.lwd, fn.col, fn.lty, fn.lwd. In addition, it is possible not to plot the reference network at all by setting show.first to FALSE.

Regardless of the visualisation, the nodes are arranged in the same positions across all networks to make comparison easier.

shd() computes the Structural Hamming Distance (SHD). Note that SHD, as defined in the reference, is defined on CPDAGs; therefore cpdag() is called on both learned and true before computing the distance.

sid() computes the Structural Interventional Distance (SID) between two DAGs, between a CPDAG and a DAG, between a DAG and a CPDAG, or between two CPDAGs.

Value

compare() returns a list containing the number of true positives (tp, the number of arcs in current also present in target), of false positives (fp, the number of arcs in current not present in target) and of false negatives (fn, the number of arcs not in current but present in target) if arcs is FALSE; or the corresponding arc sets if arcs is TRUE.

all.equal() returns either TRUE or a character string describing the differences between target and current.

shd() and hamming() return a non-negative integer number.

sid() returns a vector of non-negative integer numbers, with additional attributes describing the distribution over all the evaluated graphs.

graphviz.compare() plots one or more figures and invisibly returns a list containing the graph objects generated from the networks that were passed as arguments (in the same order). They can be further modified using the graph and Rgraphviz packages. Unlike the other functions, graphviz.compare() accepts networks with different node sets as long as they overlap.

Author(s)

Marco Scutari

References

Tsamardinos I, Brown LE, Aliferis CF (2006). "The Max-Min Hill-Climbing Bayesian Network Structure Learning Algorithm." Machine Learning, 65(1):31–78.

Peters J, Buhlmann P (2015). "Structural Intervention Distance for Evaluating Causal Graphs." Neural Computation, 27(3):771–779.

Examples

data(learning.test)

e1 = model2network("[A][B][C|A:B][D|B][E|C][F|A:E]")
e2 = model2network("[A][B][C|A:B][D|B][E|C:F][F|A]")
shd(e2, e1, debug = TRUE)
sid(e2, e1, debug = TRUE)
unlist(compare(e1,e2))
compare(target = e1, current = e2, arcs = TRUE)
## Not run: graphviz.compare(e1, e2, diff = "none")

[Package bnlearn version 5.2-20260704 Index]