Interfacing with the deal R package
The deal package (link) is one of the oldest R packages for structure and parameter learning; notably, it supports conditional linear Gaussian networks. It implements a single option for learning: hill climbing with a posterior score followed by posterior estimates of the parameters.
Exporting a network structure to deal
It is possible to export a network structure learned from data to deal; since
deal does not have separate classes for network structures and fitted networks
(e.g. like bn
vs bn.fit
objects in bnlearn),
it is not possible to export a network structure that does not have an associated data set, such
as one created with modelstring()
. Furthermore, deal does not support
partially directed graphs, so the network structure must be completely directed.
Since the two packages use the same model string representation, that is the easiest way of
exporting the network structure. Both packages have functions called modelstring()
that take network models and return their string representation; and as.network()
in
deal does the opposite, much like model2network()
in
bnlearn.
> library(bnlearn) > data(learning.test) > res = hc(learning.test) > bnlearn::modelstring(res)
[1] "[A][C][F][B|A][D|A:C][E|B:F]"
> library(deal) > bnlearn::node.ordering(res)
[1] "A" "C" "F" "B" "D" "E"
> net = deal::network(learning.test[, node.ordering(res)]) > net = deal::as.network(bnlearn::modelstring(res), net) > deal::modelstring(net)
[1] "[A][C][F][B|A][D|A:C][E|F:B]"
> bnlearn::modelstring(res)
[1] "[A][C][F][B|A][D|A:C][E|B:F]"
Note that deal requires the columns of the data set to follow the topological ordering of the nodes in the graph.
Importing a network structure from deal
The same technique can be used to import a network structure from deal.
> res2 = bnlearn::model2network(deal::modelstring(net))
Sat Feb 17 23:32:15 2024
with bnlearn
5.0-20240208
and R version 4.3.2 (2023-10-31)
.