2016-11-15 75 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套有向邊。