I have problems to parallely use R packages bnlearn and sna. The following example is straightforward:
library(bnlearn)
data("asia")
# build network
a <- hc(asia)
# output
a
The output is as expected:
Bayesian network learned via Score-based methods
model:
[A][S][T][L|S][B|S][E|T:L][X|E][D|B:E]
nodes: 8
arcs: 7
undirected arcs: 0
directed arcs: 7
average markov blanket size: 2.25
average neighbourhood size: 1.75
average branching factor: 0.88
learning algorithm: Hill-Climbing
score: BIC (disc.)
penalization coefficient: 4.258597
tests used in the learning procedure: 77
optimized: TRUE
Once I load the sna package, I receive something completely different:
library(sna)
#output
a
I get:
Biased Net Model
Parameters:
Error in matrix(c(x$d, x$pi, x$sigma, x$rho), ncol = 1) :
'data' must be of a vector type, was 'NULL'
As I don't really call any functions (just want to get the output of a), I don't think that using the :: operator can help.
I wonder if the problem is masking of an internal function that I can't really influence. Any help would be great!
This is somewhat similar to other q & a's except in this case there is an implicit call to
print, rather than an explicit function call. It is thisprintfunction that is getting masked.To print
a, you can either typeain the terminal, or be explicit and typeprint(a). To get the nice print layout of thebnobject, the author has written aprintmethod, and this is what is dispatched when typing eitheraorprint(a). (To see it without this specific printing you can useprint.default(a)). After noting that theclass(a) == "bn", you can look for theprintmethod, by usingmethods("print")or typingbnlearn:::printand then<tab>to see available functions: this leads to a (non-exported) functionbnlearn:::print.bn.So long story short, the
snapackage also has aprint.bnmethod, for objects ofclass"bn"(biased net), and it is this function that masks the one frombnlearn.So if you load
snaafterbnlearn, you can still get the nice printing by either explicitly usingbnlearn:::print.bn(a), or redefine theprintmethodprint.bn <- bnlearn:::print.bn, and it should print as expected.