我有一个非常快速的问题,其中包含一个易于重现的示例,该示例与我使用 bnlearn 进行预测的工作有关
library(bnlearn)
Learning.set4=cbind(c("Yes","Yes","Yes","No","No","No"),c(9,10,8,3,2,1))
Learning.set4=as.data.frame(Learning.set4)
Learning.set4[,c(2)]=as.numeric(as.character(Learning.set4[,c(2)]))
colnames(Learning.set4)=c("Cause","Cons")
b.network=empty.graph(colnames(Learning.set4))
struct.mat=matrix(0,2,2)
colnames(struct.mat)=colnames(Learning.set4)
rownames(struct.mat)=colnames(struct.mat)
struct.mat[1,2]=1
bnlearn::amat(b.network)=struct.mat
haha=bn.fit(b.network,Learning.set4)
#Some predictions with "lw" method
#Here is the approach I know with a SET particular modality.
#(So it's happening with certainty, here for example I know Cause is "Yes")
classic_prediction=cpdist(haha,nodes="Cons",evidence=list("Cause"="Yes"),method="lw")
print(mean(classic_prediction[,c(1)]))
#What if I wanted to predict the value of Cons, when Cause has a 60% chance of being Yes and 40% of being no?
#I decided to do this, according the help
#I could also make a function that generates "Yes" or "No" with proper probabilities.
prediction_idea=cpdist(haha,nodes="Cons",evidence=list("Cause"=c("Yes","Yes","Yes","No","No")),method="lw")
print(mean(prediction_idea[,c(1)]))
以下是帮助的内容:
"对于离散或有序节点,还可以提供两个或多个值。在这种情况下,该节点的值将从指定的值集中以均匀的概率进行采样"
当我使用分类变量预测变量的值时,我现在只使用了所述变量的某种模态,如示例中的第一个预测所示。(将证据设置为"是"会使缺点具有很高的值)
但是,如果我想在不确定变量 Cause 的确切模态的情况下预测缺点,我可以使用我在第二个预测中所做的(只知道概率)吗?这是一种优雅的方式,还是有我不知道的更好的实现方式?
我与包的创建者取得了联系,我将在此处粘贴他与问题相关的答案:
对 cpquery() 的调用是错误的:
Prediction_idea=cpdist(haha,nodes="Cons",evidence=list("Cause"=c("Yes","Yes","Yes","No","No")),method="lw")
print(mean(prediction_idea[,c(1)]))
具有 40%-60% 软证据的查询要求您首先将这些新概率放入网络中
haha$Cause = c(0.40, 0.60)
然后在没有证据参数的情况下运行查询。(因为你没有任何确凿的证据,真的,只是原因的不同概率分布。
我将发布代码,让我从示例中的拟合网络中执行我想要的操作。
change=haha$Cause$prob
change[1]=0.4
change[2]=0.6
haha$Cause=change
new_prediction=cpdist(haha,nodes="Cons",evidence=TRUE,method="lw")
print(mean(new_prediction[,c(1)]))