r - 使用 "probabilities"作为证据使用 cpdist 进行预测

标签 r bayesian bayesian-networks bnlearn

我有一个非常简单的问题,其中包含一个简单的可重现示例,该示例与我使用 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)]))

以下是帮助内容:

“对于离散或序数节点,还可以提供两个或多个值。在这种情况下,将从指定值集中以均匀概率对该节点的值进行采样”

当我使用分类变量预测变量的值时,我现在只使用了所述变量的某种模式,如示例中的第一个预测所示。 (将证据设置为"is"会使缺点取较高的值)

但是,如果我想在不知道变量 Cause 的确切模态的情况下预测 Cons,我可以使用我在第二个预测中所做的事情(只知道概率)吗? 这是一种优雅的方式还是有我不知道的更好的实现方式?

最佳答案

我与该包的创建者取得了联系,我将在此处粘贴他与该问题相关的答案:

对 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)]))

关于r - 使用 "probabilities"作为证据使用 cpdist 进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41441812/

相关文章:

r - 计算矩阵中类别/值之间的所有行和列转换,忽略顺序

r - 创建对多个连续变量执行 ttest 的函数时出错

julia - 根据图灵模型在 Julia 中绘制可信区间

matlab - 自由能强化学习实现

python - PyMC3 中的嘈杂或门

r - 使用循环和条件的矩阵计算

r - 栅格和 randomForest::predict() 中的 NA

statistics - MCMC 如何帮助贝叶斯推理?

r - 如何使用 MCMCpack 获得差异的后验?

machine-learning - 贝叶斯网络分类