首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在闪亮的服务器提供证据中使用bnlearn cpquery的贝叶斯网络问题

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

Stack Overflow用户
提问于 2019-09-17 14:16:11
回答 1查看 398关注 0票数 1

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

我尝试过为cpquery格式化证据的各种方法,但都没有效果,正如我所说的,尝试了硬编码值,这很好。

这很好用!

代码语言:javascript
复制
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

这并不意味着:

代码语言:javascript
复制
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

我也试过用

代码语言:javascript
复制
str2 = "(A == "'High'")"

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

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

代码语言:javascript
复制
* 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).

这是硬编码证据的结果--效果很好:

代码语言:javascript
复制
* 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)

海耶尔普!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-25 22:45:37

非常感谢user20650的解决方案是在整个计算过程中使用renderText。效果很好。

代码语言:javascript
复制
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)'
)))})}
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57976260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档