简体   繁体   English

使用bnlearn包从贝叶斯网络中删除节点

[英]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. 我用bnlearn构建了一个网络,但是有些节点的另一个节点没有边缘,因此我想删除它们。 Is there a command to remove a specific node from a bn object? 是否有命令从bn对象中删除特定节点?

bnlearn has built-in arc operations (docs also here ) made for just this. bnlearn具有内置的arc操作 (也在此处提供文档)。 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. 这些功能还具有检查图形中循环的好处,因为贝叶斯网络需要是非循环的(有向无环图或DAG),否则您将陷入无限循环并且无法计算条件概率。 There's also a check.illegal argument that checks for another violation of the model when adding an arc (see the docs). 还有一个check.illegal参数,用于在添加圆弧时检查是否再次违反了模型(请参见文档)。

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. set.edge设置无向边缘,而set.arc设置有向边缘。

So my attempt for this has been to use the modelstring function. 因此,我对此的尝试是使用modelstring函数。 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 . 获取字符串,删除我知道它没有任何弧/边的节点-我手动完成此操作,保存为新的修改后的字符串,然后使用命令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. 我想如果您总共没有很多节点(我有22个),而您只想从列表中删除几个节点,那将是可行的。

Hope that helps! 希望有帮助!

Fabiola's answer help me a lot. 法比奥拉的回答对我有很大帮助。

Here it is a way to do the same but without having to change the model string by hand. 这是一种执行此操作的方法,但无需手动更改模型字符串。

This is the first time I answer a question, so, please be easy on me regarding format. 这是我第一次回答问题,因此,关于格式,请对我方便。

"net" is my network, "TARGET_NODE" is the node I want to predict (I am including it within the list to be double sure I don't delete it) and "uniq" my dataset. “ net”是我的网络,“ TARGET_NODE”是我要预测的节点(我将其包括在列表中,以确保不删除它),并“ uniq”我的数据集。

model.string <- modelstring(net)
final_nodes <- unique(c(unlist(list(net$arcs)), TARGET_NODE))
nodes_to_delete <- paste("\\[",setdiff(names(net$nodes), final_nodes),"]", sep = "")
for (i in 1:length(nodes_to_delete)) {model.string <- gsub(nodes_to_delete[i], "", model.string)}
net <- model2network(model.string)

cols <- c(match(final_nodes, names(uniq)))
uniq <- uniq[,cols]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM