BnViewer — An R package for Interactive Visualization of Bayesian Networks

--

In this article I will present the bnviewer, my first package in R (it was published in CRAN on 2018–07–31). BnViewer lets you visualize interactive Bayesian networks so the scientists can better analyze the causal relationships between nodes in very large Bayesian networks through custom layouts, color and node shape customization, zoom and drag-and-drop operations.

Bayesian networks are a type of probabilistic graphical model that uses Bayesian inference for probability computations. Bayesian networks aim to model conditional dependence, and therefore causation, by representing conditional dependence by edges in a directed graph. Through these relationships, one can efficiently conduct inference on the random variables in the graph through the use of factors.

An example of a bayesian network. Source: Aalto course CS-E4820: Advanced probabilistic methods

bnViewer is an R package for interactive visualization of Bayesian Networks based on bnlearn, through visNetwork. The bnviewer package reads various structure learning algorithms provided by the bnlearn package, such as:

Constraint-based structure learning algorithms:

  • PC (the stable version);
  • Grow-Shrink (GS);
  • Incremental Association Markov Blanket (IAMB);
  • Fast Incremental Association (Fast-IAMB);
  • Interleaved Incremental Association (Inter-IAMB);
  • Max-Min Parents & Children (MMPC);
  • Semi-Interleaved Hiton-PC (SI-HITON-PC);

Score-based structure learning algorithms:

  • Hill Climbing (HC);
  • Tabu Search (Tabu);

Hybrid structure learning algorithms:

  • Max-Min Hill Climbing (MMHC);
  • General 2-Phase Restricted Maximization (RSMAX2);

Local discovery algorithms:

  • Chow-Liu;
  • ARACNE;

Installation

You can install the stable version of bnviewer from CRAN:

install.packages("bnviewer")

How to use

Import the bnlearn and bnviewer packages

library(bnlearn)
library(bnviewer)

Import the desired dataset and apply a structure learning algorithm. Example (Hill-Climbing (HC)).

data("alarm")
bn.learn.hc = hc(alarm)

Call the viewer function of the bnviewer package with the desired parameters.

viewer(bn.learn.hc,
bayesianNetwork.width = "100%",
bayesianNetwork.height = "80vh",
bayesianNetwork.layout = "layout_with_sugiyama",
bayesianNetwork.title="Discrete Bayesian Network - Alarm",
bayesianNetwork.subtitle = "Monitoring of emergency care patients",
bayesianNetwork.footer = "Fig. 1 - Layout with Sugiyama"
)

Example of Bayesian network visualization with custom nodes and grid layout.

viewer(bn.learn.hc,
bayesianNetwork.width = "100%",
bayesianNetwork.height = "80vh",
bayesianNetwork.layout = "layout_on_grid",
bayesianNetwork.title="Discrete Bayesian Network - Alarm",
bayesianNetwork.subtitle = "Monitoring of emergency care patients",
bayesianNetwork.footer = "Fig. 1 - Layout on grid",

node.colors = list(background = "#f4bafd",
border = "#2b7ce9",
highlight = list(background = "#97c2fc",
border = "#2b7ce9"))

)

Example of Bayesian network visualization with custom nodes and circle layout.

viewer(bn.learn.hc,
bayesianNetwork.width = "100%",
bayesianNetwork.height = "80vh",
bayesianNetwork.layout = "layout_in_circle",
bayesianNetwork.title="Discrete Bayesian Network - Alarm",
bayesianNetwork.subtitle = "Monitoring of emergency care patients",
bayesianNetwork.footer = "Fig. 1 - Layout in circle",

node.colors = list(background = "red",
border = "black",
highlight = list(background = "black",
border = "red"))

)

To the next…

I hope this approach can contribute to those who are starting in the area of Data Science, whether Statistics, Mathematicians, Computer Scientists or students who have an interest in the subject.

--

--