2016-11-15 76 views

回答

0

所以我的尝试是使用modelstring函数。获取字符串,删除我知道它没有任何弧/边的节点 - 我手工完成 - 保存到一个新的修改过的字符串,然后使用命令model2network再次将字符串转换为网络。这是命令序列:

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) 

我想,如果你没有在总(我已经有了22)许多节点,你只是想从列表中删除一些,将工作。

希望有帮助!

0

法比奥拉的回答帮了我很多。

这里有一种方法可以做到这一点,但不必手动更改模型字符串。

这是我第一次回答一个问题,所以请在格式上轻松一点。

“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] 
+0

它很好用Mery!做得好! –

+0

只需使用内置于bnlearn的drop.arc(),参见我的答案[here](https://stackoverflow.com/a/48676996/4549682)。 – wordsforthewise

1

bnlearn有内置的只是本作arc operations(文档也here)。因为贝叶斯网络需要是无环的(有向无环图,或DAG),所以这些函数还有检查图中的周期的好处,否则会得到无限循环并且无法计算条件概率。还有一个check.illegal参数,用于在添加弧时检查模型的另一个违规(请参阅文档)。

但是,他们的例子不是很好,文档也不是。操作返回一个模型,所以你必须用返回的模型覆盖旧模型。

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套无向边,而set.arc套有向边。