The bnlearn package helps you learn Bayesian networks from data. It has handy functions for plotting performance of Bayesian network structure learning.

Below I demonstrate some network learnings performance visualizations on the ALARM network, a commonly used benchmark:


alarm.model.string <- paste("[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF]",
  "[CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]", sep = "") <- alarm %>%
  names %>%
  empty.graph %>%
  `modelstring<-`(value = alarm.model.string)
graphviz.plot(, main = "ALARM Network")

plot of chunk alarm

Using model averaging and the strength.plot function, I can get a visualization of the proportion of times each arc in the true network appeared in the model averaging bootstrap simulation. This is shown as edge thickness.

model.averaging <- boot.strength(data = alarm, 
                                 R = 50, m = 100,
                                 algorithm = "hc", 
                                 algorithm.args = list(score = "bde", iss = 10))
strength.plot(, model.averaging)

plot of chunk stength-plot1

This reflects how well the network inference learned the true arcs – the thicker the arc, the more evidence the learning algorithm found in the data.

Similarly, I get a consensus network - a learned network created from the results of the model averaging, and visualize the evidence for each arc in the consensus network. The function provides a consensus network from the averaging results, the threshold value is a cutoff – the arc must have appeared more than this percentage of times in the averaging samples. I set it very low so we can get lots of weak edges in the visualization. <-, threshold = .01)
strength.plot(, model.averaging)

plot of chunk strength-plot2

Since the cutoff is so low, the network is thick with weak-evidence arcs.

ROC Curves

ROC curves are a classic visualization of learning performance. However, they are tricky in this case because they require a false positive rate. False positives are easy to get with the compare function:


## $tp
## [1] 22
## $fp
## [1] 390
## $fn
## [1] 24

However, the false positive rate is (FP / N), where N here is defined as “wrong arcs”, i.e. all the arcs that are not in the ALARM network. That means counting all the possible directed edges in graph. Since it is a directed acyclic graph, the presence of an edge depends on the presence of others, so this is a non-trivial problem.

An alternative is to convert the graph to a moral graph, where undirected edges represent conditional dependencies.


plot of chunk moral-plot

In this case, N is just the number of conditional independencies, which we can count – it is just the number of possible undirected edges ( n(n-1)/ 2, n = # nodes) minus the number of undirected edges in moral( The compare function still works with moral graphs.

compare(moral(, moral(

## $tp
## [1] 65
## $fp
## [1] 573
## $fn
## [1] 0

So we can get an ROC curve that reflects how well we recover not arcs, but the conditional dependences that the arcs determine.

I wrote a function that gives me true positive rate and false positive rate, and plotted an ROC curve. The main argument is the cutoff, which is passed as the threshold value in With different values of cutoff, I get different pairs of rates, giving me values to plot for the ROC curve.

getROCStats <- function(cutoff, true, model.averaging){ <-, threshold = cutoff) %>%
  pos <- narcs(moral(
  neg <- nnodes( %>%
    {.*(. - 1) / 2 - pos}
  compare(moral(, moral( %>%
    unlist %>%
    {c(.["tp"] / pos, .["fp"]/ neg)} %>%
    `names<-`(c("tpr", "fpr"))
adply(seq(.05, 1, by = .05), 1, getROCStats, true =, model.averaging = model.averaging) %>%
  select(fpr, tpr) %>%
  plot(main = "Recovery of Conditional Dependencies", type = "l")

plot of chunk bnroc