Interfacing with the igraph R package

The igraph package (link) is the R interface to the igraph library (link) for network analysis. It implements an extensive selection of algorithms for creating and generating directed and undirected graphs, manipulating nodes and arcs, and it provides highly customizable plotting facilities. For this reason, bnlearn implements functions to import and export network structures to igraph's native objects, which are themselves of class igraph.

Exporting a network structure to igraph

Exporting objects is achieved with the conversion method as.igraph().

> library(bnlearn)
> 
> dag.bnlearn = random.graph(LETTERS[1:10])
> dag.bnlearn

  Random/Generated Bayesian network

  model:
   [A][B][D][G][C|B][F|B:D][H|G][E|C][I|B:C][J|B:E] 
  nodes:                                 10 
  arcs:                                  9 
    undirected arcs:                     0 
    directed arcs:                       9 
  average markov blanket size:           2.20 
  average neighbourhood size:            1.80 
  average branching factor:              0.90 

  generation algorithm:                  Full Ordering 
  arc sampling probability:              0.2222222
> dag.igraph = bnlearn::as.igraph(dag.bnlearn)
> dag.igraph = as.igraph(dag.bnlearn)
> dag.igraph
IGRAPH e4b139b DN-- 10 9 -- 
+ attr: name (v/c)
+ edges from e4b139b (vertex names):
[1] B->C B->F B->I B->J C->E C->I D->F E->J G->H

as.igraph() accepts objects of class bn.fit as well as of class bn, and it implitly call bn.net() (link) on the former.

> dag.test = hc(learning.test)
> dag.test

  Bayesian network learned via Score-based methods

  model:
   [A][C][F][B|A][D|A:C][E|B:F] 
  nodes:                                 6 
  arcs:                                  5 
    undirected arcs:                     0 
    directed arcs:                       5 
  average markov blanket size:           2.33 
  average neighbourhood size:            1.67 
  average branching factor:              0.83 

  learning algorithm:                    Hill-Climbing 
  score:                                 BIC (disc.) 
  penalization coefficient:              4.258597 
  tests used in the learning procedure:  40 
  optimized:                             TRUE
> fitted = bn.fit(dag.test, learning.test)
> as.igraph(fitted)
IGRAPH 47a0ed8 DN-- 6 5 -- 
+ attr: name (v/c)
+ edges from 47a0ed8 (vertex names):
[1] A->B A->D B->E C->D F->E

Undirected arcs are exported as a pair of directed arcs, that is, Xi — Xj becomes {Xi → Xj, Xi ← Xj}.

> as.igraph(skeleton(dag.test))
IGRAPH 40fc327 DN-- 6 10 -- 
+ attr: name (v/c)
+ edges from 40fc327 (vertex names):
 [1] A->B A->D B->A B->E C->D D->A D->C E->B E->F F->E

Importing a network structure from igraph

Importing network structures works in the same way, with a conversion method as.bn().

> as.bn(dag.igraph)

  Random/Generated Bayesian network

  model:
   [A][B][D][G][C|B][F|B:D][H|G][E|C][I|B:C][J|B:E] 
  nodes:                                 10 
  arcs:                                  9 
    undirected arcs:                     0 
    directed arcs:                       9 
  average markov blanket size:           2.20 
  average neighbourhood size:            1.80 
  average branching factor:              0.90 

  generation algorithm:                  Empty

Note that as graphs are imported with as.bn() they are checked to be acyclic, unless the user specifies check.cycles = FALSE.

> library(igraph)
> 
> cyclic.graph = make_graph(~ A-+B-+C-+D-+A)
> cyclic.graph
IGRAPH 68d5247 DN-- 4 4 -- 
+ attr: name (v/c)
+ edges from 68d5247 (vertex names):
[1] A->B B->C C->D D->A
> as.bn(cyclic.graph)
## Error: the igraph object contains directed cycles.
> as.bn(cyclic.graph, check.cycles = FALSE)

  Random/Generated Bayesian network

  model:
   [A|D][B|A][C|B][D|C] 
  nodes:                                 4 
  arcs:                                  4 
    undirected arcs:                     0 
    directed arcs:                       4 
  average markov blanket size:           2.00 
  average neighbourhood size:            2.00 
  average branching factor:              1.00 

  generation algorithm:                  Empty
Last updated on Mon Feb 17 16:47:00 2020 with bnlearn 4.6-20190124 and R version 3.6.2 (2019-12-12).