贝叶斯网络中一个节点的条件概率修改(R代码)

时间:2016-09-21 07:53:00

标签: r bayesian-networks

估计贝叶斯网络中的条件概率后, 我问了一个节点(" Inlet_gas_total_pressure")的概率如下;

bn.mle.before$"Inlet_gas_total_pressure"

节点Inlet_gas_total_pressure(多项分布)的参数

条件概率表:

      no      yes 
0.843127 0.156873 

bn.mle.before$"Inlet_gas_total_pressure"$prob

      no      yes 
0.843127 0.156873 

我想改变"是"的概率值。从0.156873到0.4 我怎样才能做到这一点 ? 以下是我的试用,但失败了。

bn.mle.before$"Inlet_gas_total_pressure" <- list(prob=c("no"=0.6, "yes"=0.4))
  

check.fit.dnode.spec(value,node = name)出错:     节点Inlet_gas_total_pressure的条件概率分布必须是表,矩阵或多维数组。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 这是一些玩具示例,将向您展示如何挽救这一天。

library(bnlearn)
Learning.set4=cbind(c("Yes","Yes","Yes","No","No","No"),c("Blue","Green","Blue","Green","Green","Green"),c(9,10,8,3,2,1))
Learning.set4=as.data.frame(Learning.set4)
Learning.set4[,c(3)]=as.numeric(as.character(Learning.set4[,c(3)]))
colnames(Learning.set4)=c("Cause1","Cause2","Cons")
b.network=empty.graph(colnames(Learning.set4))
struct.mat=matrix(0,3,3)
colnames(struct.mat)=colnames(Learning.set4)
rownames(struct.mat)=colnames(struct.mat)
struct.mat[2,3]=1
struct.mat[1,3]=1
bnlearn::amat(b.network)=struct.mat
haha=bn.fit(b.network,Learning.set4)

print(haha$Cause1$prob)

T=haha$Cause1$prob
T[[1]]=0.8
T[[2]]=0.2

haha$Cause1=T
print(haha$Cause1$prob)

我成功地改变了节点Cause1

的概率

干杯

答案 1 :(得分:0)

# a similar example

fit=bn.fit(dag,traindata)

# Below I want to set any zero prob to something small

for (i in 1:10) {
  my=fit[[i]]
  idx=which(my$prob==0)
  if (length(idx)>0){
    for (j in idx ) {
      my$prob[[j]]=0.001
      my$prob[[j-1]]=1-0.001
    }
  }
  fit[i]=list(my)
}