闪亮服务器中使用 bnlearn cpquery 的贝叶斯网络问题 - 提供证据

Problem with Bayesian Network with bnlearn cpquery in shiny server - supplying evidence

我正在使用 bnlearn 构建带有贝叶斯网络引擎的 ShinyDashboard 评估工具。它是一个使用专家知识创建的离散网络,用于构建条件概率表。闪亮的前端用于引出证据,但是,当我尝试使用 cpquery 在后端应用证据时,它不起作用。如果我在后端闪亮服务器中对证据进行硬编码,它就可以工作。所以我认为这与访问我缺少的输入变量有关。

我尝试了各种格式化 cpquery 证据的方法,但都无济于事,正如我所说,尝试了对值进行硬编码,效果很好。

这很好用!

Index <- shiny::reactive({
  cpquery(fitted = tdag,
          event = (A == "High"),              # event
          evidence = ( (B == "Yes") &      # evidence
                       (C == "Medium") &
                       (D == "Medium") &
                       (E == "Yes") &
                       (G == "High") &
                       (H == "Low") 
          ), # end evidence
          n = 1000000,                       # no of samples generated
          debug = TRUE
  ) # end cpqery
}) # end reactive

这不是:

Index <- shiny::reactive({
  # Create a string of the selected evidence
  str1 <<- paste0(
    "(B == '", input$BChoiceInp, "') & ",
    "(C == '", input$CChoiceInp, "') & ",
    "(D == '", input$DChoiceInp, "') & ",
    "(E == '", input$EChoiceInp, "') & ",
    "(G == '", input$GChoiceInp, "') & ",
    "(H == '", input$HChoiceInp, "')"
  )

  cpquery(fitted = tdag,
          event = (A == "High"),              # event
          evidence = (eval(parse(text = str1))),       # evidence
          n = 1000000,                       # no of samples generated
          debug = TRUE
  ) # end cpqery
}) # end reactive

我也试过使用

str2 = "(A == "'High'")"

eval(parse(text = paste("cpquery(fitted,",str2,",",str1,", n = 100000, debug=TRUE)")))

同样的结果。 网络运行但结果如下 - 它似乎没有看到输入。:

* checking which nodes are needed.
  > event involves the following nodes: A
  > evidence involves the following nodes: B C D E G H
  > upper closure is ' A B C D E F G H I J  '
  > generating observations from 10 / 10 nodes.
* generated 10000 samples from the bayesian network.
  > evidence matches 0 samples out of 10000 (p = 0).
  > event matches 0 samples out of 0 (p = 0).
* generated 10000 samples from the bayesian network.
  > evidence matches 0 samples out of 10000 (p = 0).
  > event matches 0 samples out of 0 (p = 0).

这是硬编码证据的结果 - 工作正常:

* generated 10000 samples from the bayesian network.
  > evidence matches 39 samples out of 10000 (p = 0.0039).
  > event matches 30 samples out of 39 (p = 0.7692308).
* generated 10000 samples from the bayesian network.
  > evidence matches 33 samples out of 10000 (p = 0.0033).
  > event matches 21 samples out of 33 (p = 0.6363636).
* generated 10000 samples from the bayesian network.
  > evidence matches 36 samples out of 10000 (p = 0.0036).
  > event matches 23 samples out of 36 (p = 0.6388889).
* generated a grand total of 1e+06 samples.
  > event matches 2666 samples out of 4173 (p = 0.6388689)

嘻嘻嘻!

非常感谢 user20650,解决方案是在整个计算过程中使用 renderText。做工精美。

library(shiny)
library(bnlearn)

tdag = bn.fit(hc(learning.test[5:6]), learning.test[5:6])

shinyApp(

ui = basicPage(
selectInput("e", "E:", choices=letters[1:3] ),
selectInput("f", "F:", choices=letters[1:2] ),
textOutput("prob")
),

server = function(input, output, session) {
output$prob <- renderText({
event <- paste0("(F == '", input$f, "')")
evidence <- paste0("(E == '", input$e, "')")
eval(parse(text=paste(
'cpquery(fitted=tdag,
event = ', event, ',
evidence = ', evidence, ',
n = 100000,
debug = TRUE)'
)))})}
)