在 R 中使用贝叶斯网络进行预测

机器算法验证 r 贝叶斯 网络 贝叶斯网络
2022-03-16 18:05:14

我一直在尝试自学网络分析,并且已经能够在 R 中开发 DAG 图表。但是,我已经浏览了三四个 R 包,并且几乎没有看到生成联合函数的方式网络的概率。DAG 图告诉我变量之间的关系,但我对概率更好奇,并且没有找到在 R 中做到这一点的方法。如果有的话,似乎有很多包专门用于生成情节或专注于推理,我想知道如何获得网络的概率。

library("bnlearn")
library("Rgraphviz")

dat=data.frame(won=c(1,0,0,1,0,0), sold=c(0,0,0,1,0,0), insured=c(0,0,1,0,0,1), 
               credit=c("POOR","FAIR","GOOD","FAIR","FAIR","GOOD"))
dat$won = factor(dat$won)
dat$sold = factor(dat$sold)
dat$insured = factor(dat$insured)
dat$credit = factor(dat$credit) 

highlight.opts <- list(nodes = c("won","sold","insured","credit"),
                       col = "red", fill = "grey")
bn.hc <- hc(dat, score = "aic")
graphviz.plot(bn.hc, highlight=highlight.opts)
1个回答

我认为一切都在函数中bn.fit跟进你的代码,我得到了

> bn.fit(bn.hc, dat)

  Bayesian network parameters

  Parameters of node won (multinomial distribution)

Conditional probability table:

        0         1 
0.6666667 0.3333333 

  Parameters of node sold (multinomial distribution)

Conditional probability table:

    won
sold   0   1
   0 1.0 0.5
   1 0.0 0.5

  Parameters of node insured (multinomial distribution)

Conditional probability table:

       won
insured   0   1
      0 0.5 1.0
      1 0.5 0.0

  Parameters of node credit (multinomial distribution)

Conditional probability table:

      insured
credit    0    1
  FAIR 0.75 0.00
  GOOD 0.00 1.00
  POOR 0.25 0.00