我正在构建一个使用贝叶斯网络引擎的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)海耶尔普!
发布于 2019-09-25 22:45:37
非常感谢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)'
)))})}
)https://stackoverflow.com/questions/57976260
复制相似问题