Creating Bayesian network structures

The graphical structure of a Bayesian network are stored in objects of class bn. They can be created in various ways through three possible representations: the arc set, the adjacency matrix and them model formula. In addition it's possible to create empty and random network structures with the empty.graph and random.graph functions.

Creating an empty network

> library(bnlearn)
> e = empty.graph(LETTERS[1:6])
> e

  Randomly generated Bayesian network

  model:
    [A][B][C][D][E][F] 
  nodes:                                 6 
  arcs:                                  0 
    undirected arcs:                     0 
    directed arcs:                       0 
  average markov blanket size:           0.00 
  average neighbourhood size:            0.00 
  average branching factor:              0.00 

  generation algorithm:                  Empty 

Creating a network structure

with a specific arc set

> arc.set = matrix(c("A", "C", "B", "F", "C", "F"), 
+             ncol = 2, byrow = TRUE)
> arc.set
     [,1] [,2]
[1,] "A"  "C" 
[2,] "B"  "F" 
[3,] "C"  "F" 
> arcs(e) = arc.set
> e

  Randomly generated Bayesian network

  model:
    [A][B][D][E][C|A][F|B:C] 
  nodes:                                 6 
  arcs:                                  3 
    undirected arcs:                     0 
    directed arcs:                       3 
  average markov blanket size:           1.33 
  average neighbourhood size:            1.00 
  average branching factor:              0.50 

  generation algorithm:                  Empty 

with a specific adjacency matrix

> adj = matrix(0L, ncol = 6, nrow = 6, 
+         dimnames = list(LETTERS[1:6], LETTERS[1:6]))
> adj["A", "C"] = 1L
> adj["B", "F"] = 1L
> adj["C", "F"] = 1L
> adj["D", "E"] = 1L
> adj["A", "E"] = 1L
> adj
  A B C D E F
A 0 0 1 0 1 0
B 0 0 0 0 0 1
C 0 0 0 0 0 1
D 0 0 0 0 1 0
E 0 0 0 0 0 0
F 0 0 0 0 0 0
> amat(e) = adj
> e

  Randomly generated Bayesian network

  model:
    [A][B][D][C|A][E|A:D][F|B:C] 
  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 

  generation algorithm:                  Empty 

with a specific model formula

There are two ways to do this:
  1. with the model2network function:
    > model2network("[A][C][B|A][D|C][F|A:B:C][E|F]")
    
      Randomly generated Bayesian network
    
      model:
        [A][C][B|A][D|C][F|A:B:C][E|F] 
      nodes:                                 6 
      arcs:                                  6 
        undirected arcs:                     0 
        directed arcs:                       6 
      average markov blanket size:           2.67 
      average neighbourhood size:            2.00 
      average branching factor:              1.00 
    
      generation algorithm:                  Empty 
    
    
  2. via direct assignment, as for the arc set or the adjacency matrix:
    > modelstring(e) = "[A][C][B|A][D|C][F|A:B:C][E|F]"
    > e
    
      Randomly generated Bayesian network
    
      model:
        [A][C][B|A][D|C][F|A:B:C][E|F] 
      nodes:                                 6 
      arcs:                                  6 
        undirected arcs:                     0 
        directed arcs:                       6 
      average markov blanket size:           2.67 
      average neighbourhood size:            2.00 
      average branching factor:              1.00 
    
      generation algorithm:                  Empty 
    
    

Creating one or more random network structures

with a specified node ordering

> random.graph(LETTERS[1:6], num = 3)
[[1]]

  Randomly generated Bayesian network

  model:
    [A][B|A][C|A][D|B][E|A:C:D][F|B:C:E] 
  nodes:                                 6 
  arcs:                                  9 
    undirected arcs:                     0 
    directed arcs:                       9 
  average markov blanket size:           4.33 
  average neighbourhood size:            3.00 
  average branching factor:              1.50 

  generation algorithm:                  Full Ordering 
  arc sampling probability:              0.4 

[[2]]

  Randomly generated Bayesian network

  model:
    [A][B][C][D|B][E|A][F|A:E] 
  nodes:                                 6 
  arcs:                                  4 
    undirected arcs:                     0 
    directed arcs:                       4 
  average markov blanket size:           1.33 
  average neighbourhood size:            1.33 
  average branching factor:              0.67 

  generation algorithm:                  Full Ordering 
  arc sampling probability:              0.4 

[[3]]

  Randomly generated Bayesian network

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

  generation algorithm:                  Full Ordering 
  arc sampling probability:              0.4 

sampling from the space of connected directed acyclic graphs with uniform probability

> random.graph(LETTERS[1:6], num = 2, method = "ic-dag")
[[1]]

  Randomly generated Bayesian network

  model:
    [E][C|E][B|C:E][A|B:E][F|A:B:C:E][D|A:B:C:F] 
  nodes:                                 6 
  arcs:                                  13 
    undirected arcs:                     0 
    directed arcs:                       13 
  average markov blanket size:           4.67 
  average neighbourhood size:            4.33 
  average branching factor:              2.17 

  generation algorithm:                  Ide & Cozman's Multiconnected DAGs
  burn in length:                        216 
  maximum in-degree:                     Inf 
  maximum out-degree:                    Inf 
  maximum degree:                        Inf 

[[2]]

  Randomly generated Bayesian network

  model:
    [E][B|E][C|E][A|B:E][F|A:B:C:E][D|A:B:C:F] 
  nodes:                                 6 
  arcs:                                  12 
    undirected arcs:                     0 
    directed arcs:                       12 
  average markov blanket size:           4.67 
  average neighbourhood size:            4.00 
  average branching factor:              2.00 

  generation algorithm:                  Ide & Cozman's Multiconnected DAGs
  burn in length:                        216 
  maximum in-degree:                     Inf 
  maximum out-degree:                    Inf 
  maximum degree:                        Inf 

sampling from the space of the directed acyclic graphs with uniform probability

> random.graph(LETTERS[1:6], num = 2, method = "melancon")
[[1]]

  Randomly generated Bayesian network

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

  generation algorithm:                  Melancon's Uniform Probability DAGs
  burn in length:                        216
  maximum in-degree:                     Inf
  maximum out-degree:                    Inf
  maximum degree:                        Inf


[[2]]

  Randomly generated Bayesian network

  model:
    [D][E|D][C|D:E][F|C:E][A|C:D:F][B|A:C:D]
  nodes:                                 6
  arcs:                                  11
    undirected arcs:                     0
    directed arcs:                       11
  average markov blanket size:           4.00
  average neighbourhood size:            3.67
  average branching factor:              1.83

  generation algorithm:                  Melancon's Uniform Probability DAGs
  burn in length:                        216
  maximum in-degree:                     Inf
  maximum out-degree:                    Inf
  maximum degree:                        Inf