Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop node from a bayesian network with bnlearn package

I built a network with bnlearn, but there are some nodes without edges to another node, so I would like to remove them. Is there a command to remove a specific node from a bn object?

like image 259
user2626597 Avatar asked Jul 26 '26 16:07

user2626597


2 Answers

bnlearn has built-in arc operations (docs also here) made for just this. These functions also have the benefit of checking for cycles in your graph, because Bayesian Networks need to be acyclic (directed acyclic graphs, or DAGs), otherwise you get infinite loops and can't calculate conditional probabilities. There's also a check.illegal argument that checks for another violation of the model when adding an arc (see the docs).

But, their example is not great and neither are the docs. The operations return a model, so you have to overwrite your old model with the returned one.

data(learning.test)
# model ends up the same every time here, but may want
# to set random seed for reproducibility in other cases
set.seed(42)
model = tabu(learning.test)  # tabu is a better algo than hc I think
plot(model)

model <- set.arc(model, "A", "F")
plot(model)
model <- drop.arc(model, "A", "F")
plot(model)

set.edge sets undirected edges, while set.arc sets directed edges.

like image 110
wordsforthewise Avatar answered Jul 28 '26 06:07

wordsforthewise


So my attempt for this has been to use the modelstring function. Get the string, remove the node I know it hasn't any arcs/edges - I do this by hand -, save to a new modified string and then convert the string to a network again with the command model2network. Here is the sequence of commands:

model.string <- modelstring(mymodel)
model.string
new.string <- "your string except the node you want to remove from the output above"
new.model <- model2network(new.string)

I guess that would work if you don't have many nodes in total (I've got 22) and you just want to remove a few from the list.

Hope that helps!

like image 38
Fabiola Fernández Avatar answered Jul 28 '26 07:07

Fabiola Fernández



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!