compare: Compare two or more different Bayesian networks

View source: R/frontend-graph.R

compareR Documentation

Compare two or more different Bayesian networks


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,


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

shd(learned, true, wlbl = FALSE, debug = FALSE)
hamming(learned, true, debug = FALSE), ..., groups, layout = "dot", shape = "rectangle",
  fontsize = 12, main = NULL, sub = NULL, diff = "from-first",
  diff.args = list())


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


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


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


a boolean value. See below.


an object of class bn.


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


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


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


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


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


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


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


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

Details can visualize differences between graphs in various way 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, false negative arcs relative to that first network 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, tp.lwd. In addition, it is possible not to plot the reference network at all by setting show.first to FALSE.

Regardless of the visualization, the nodes are arranged to be in the same position for all the networks to make it easier to compare them.


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. plots one or more figures and returns invisibly 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.


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.


Marco Scutari


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



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)
compare(target = e1, current = e2, arcs = TRUE)
## Not run:, e2, diff = "none")

bnlearn documentation built on May 29, 2024, 5:07 a.m.