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

    shd(learned, true, wlbl = FALSE, debug = FALSE)
    hamming(learned, true, debug = FALSE)

    graphviz.compare(x, ..., groups, layout = "dot", shape = "circle", 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 by blacklist are preserved when constructing the CPDAGs of `learned` and `true`.
`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 `dots`, `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`.
`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`, se 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 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.

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

`graphviz.compare()` 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

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.

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

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

[Package bnlearn version 4.7.1 Index]