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][C][F][G][D|C][H|C:F][E|D][I|A:F:H][J|E] nodes: 10 arcs: 8 undirected arcs: 0 directed arcs: 8 average markov blanket size: 2.20 average neighbourhood size: 1.60 average branching factor: 0.80 generation algorithm: Full Ordering arc sampling probability: 0.2222222
> dag.igraph = as.igraph(dag.bnlearn) > dag.igraph
IGRAPH 29a5c2a DN-- 10 8 -- + attr: name (v/c) + edges from 29a5c2a (vertex names): [1] A->I C->D C->H D->E E->J F->H F->I H->I
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 4db4381 DN-- 6 5 -- + attr: name (v/c) + edges from 4db4381 (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 e8944fa DN-- 6 10 -- + attr: name (v/c) + edges from e8944fa (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][C][F][G][D|C][H|C:F][E|D][I|A:F:H][J|E] nodes: 10 arcs: 8 undirected arcs: 0 directed arcs: 8 average markov blanket size: 2.20 average neighbourhood size: 1.60 average branching factor: 0.80 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 31384b1 DN-- 4 4 -- + attr: name (v/c) + edges from 31384b1 (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
Fri Aug 1 23:00:15 2025
with bnlearn
5.1
and R version 4.5.0 (2025-04-11)
.