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][D][F][E|D][G|A:B:C][H|A:B:F][I|C:F][J|B:C:D:E:H] nodes: 10 arcs: 14 undirected arcs: 0 directed arcs: 14 average markov blanket size: 5.40 average neighbourhood size: 2.80 average branching factor: 1.40 generation algorithm: Full Ordering arc sampling probability: 0.2222222
> dag.igraph = as.igraph(dag.bnlearn) > dag.igraph
IGRAPH 7c4c330 DN-- 10 14 -- + attr: name (v/c) + edges from 7c4c330 (vertex names): [1] A->G A->H B->G B->H B->J C->G C->I C->J D->E D->J E->J F->H F->I H->J
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 252455e DN-- 6 5 -- + attr: name (v/c) + edges from 252455e (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 493b870 DN-- 6 10 -- + attr: name (v/c) + edges from 493b870 (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][D][F][E|D][G|A:B:C][H|A:B:F][I|C:F][J|B:C:D:E:H] nodes: 10 arcs: 14 undirected arcs: 0 directed arcs: 14 average markov blanket size: 5.40 average neighbourhood size: 2.80 average branching factor: 1.40 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 d48551d DN-- 4 4 -- + attr: name (v/c) + edges from d48551d (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
Sat Feb 17 23:30:01 2024
with bnlearn
5.0-20240208
and R version 4.3.2 (2023-10-31)
.