我使用R中的程序包bnlearn使用数据和专家知识来构造定制拟合离散贝叶斯网络。 http://www.bnlearn.com/examples/custom/R bnlearn程序包错误重新分配条件概率
这需要使用bn.fit()创建一个bn.fit对象并修改感兴趣节点的局部分布。对于离散贝叶斯网络(或条件高斯网络中的离散节点),可以使用coef()从bn.fit对象中提取条件概率表,并进行更新并重新保存。
library(bnlearn)
dag = model2network("[A][C][F][B|A][D|A:C][E|B:F]") #creates a network
fitted <- bn.fit(dag, learning.test) #(determines conditional probability
given data in learning.test)
fitted[[3]] #CP for node [C] as example, fitted$C also works
cpt <- coef(fitted[[3]]) #extract coefficients from table
cpt[1:length(cpt)] = c(0.50, 0.25, 0.25) #new CPs
fitted$C<-cpt #assign new CPs to joint CP table
fitted$C#Works
Parameters of node C (multinomial distribution)
Conditional probability table:
a b c
0.50 0.25 0.25
我想通过索引bn.fit对象更新大量节点,即
fitted[[3]][[4]][1:3]<-cpt #returns error
fitted[[3]][[4]]<-cpt #returns error
Error in check.nodes(name, x) :
nodes must be a vector of character strings, the labels of the nodes.
鉴于[和$运营商之间的对等,任何人都可以解释为什么是这样的案件和潜在的工作。
identical(fitted$C,fitted[[3]])
TRUE
感谢
爱和尊重,如果任何人可以计算出如何添加反引号,而不是单'bnlearn :::'函数中的引号 – user20650
Thanks。通过索引bn.fit对象来提取节点名称 拟合[[拟合[[3]] [[1]]]] < - cpt – DRBG
你'欢迎。你可以通过使用'nms < - names(fitted)'然后使用'fits [[nms [3]]] < - cpt'上的索引来清理它。 – user20650