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

问题描述 投票:1回答:1

我正在使用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)

Heeeelllp!

r shinydashboard shiny-server shiny-reactivity bayesian-networks
1个回答
1
投票

该解决方案非常感谢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)'
)))})}
)
© www.soinside.com 2019 - 2024. All rights reserved.