compare {bnlearn} R Documentation

Compare two different Bayesian networks

Description

Compare two different Bayesian networks or compute the Structural Hamming Distance (SHD) between them.

Usage

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

shd(learned, true, debug = FALSE)

Arguments

target, learned an object of class bn.
current, true another object of class bn.
... extra arguments from the generic method (currently ignored).
debug a boolean value. If TRUE a lot of debugging output is printed; otherwise the function is completely silent.

Value

compare returns a boolean value (TRUE if the objects describe the same network structure, FALSE otherwise). all.equal returns either TRUE or a character string describing the differences between target and current. shd returns a non-negative integer number.

Note

The main difference between compare and all.equal is that the former is quite slow but provides a detailed description of the differences between target and current (when debug is set to TRUE), while the latter is much faster but provides only generic diagnostic messages.

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)

res = gs(learning.test)
## the arc between A and B has no direction
plot(res, highlight = c("A", "B"))
res2 = set.arc(res, "A", "B")
compare(res, res2, debug = TRUE)
# * children of A in r1 not present in r2:
# character(0)
# * children of A in r2 not present in r1:
# [1] "B"
# * parents of B in r1 not present in r2:
# character(0)
# * parents of B in r2 not present in r1:
# [1] "A"
# * directed arcs in r1 not present in r2:
# character(0)
# * directed arcs in r2 not present in r1:
# [1] "A -> B"
# * undirected arcs in r1 not present in r2:
# [1] "A - B" "B - A"
# * undirected arcs in r2 not present in r1:
# character(0)
# [1] FALSE
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)
# * arcs between A and F do not match.
# * arcs between E and F do not match.
# [1] 2