【问题标题】:R gRain error - table dimensions do not matchR gRain 错误 - 表格尺寸不匹配
【发布时间】:2017-09-10 15:21:23
【问题描述】:

我正在使用 R 中的 gRain 包创建贝叶斯网络。尝试编译条件概率表时,出现错误“表尺寸不匹配”。有问题的表格采用 A|B 的形式,其中 A 可以取三个可能的值,B 可以取两个。根据六种可能的组合,我在 cptable 定义中总共输入了 12 个值。任何帮助将不胜感激,因为我已经检查和研究无济于事并且看不到出了什么问题。这是我的代码。

# define levels
lh <- c("low", "high")
lmh <- c("low", "medium", "high")

# specify the conditional probability tables
eh <- cptable(~eh, values=c(0.2, 0.8), levels=lh)
inf.oil.eh <- cptable(~inf | oil:eh, values = c(0.9, 0.1, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99), levels=lh)
bp.oil <- cptable(~bp | oil, values=c(0.9, 0.1, 0.1, 0.9, 0, 1, 0.1, 0.9, 0.4, 0.6, 0.5, 0.5), levels=lmh)
oil.eh <- cptable(~oil | eh, values=c(0.9, 0.1, 0.05, 0.95), levels=lh)
rt.inf.eh <-cptable(~rt | inf:eh, values=c(0.9, 0.1, 0.1, 0.9, 0.1, 0.9, 0.01, 0.99), levels=lh)
# compile the tables
plist <- compileCPT(list(eh, oil.eh, inf.oil.eh, bp.oil, rt.inf.eh))

错误如下:

    Error for v,pa(v): bp, oil
    List of 2
     $ bp : chr [1:3] "low" "medium" "high"
     $ oil: chr [1:2] "low" "high"
     num [1:12] 0.9 0.1 0.1 0.9 0 1 0.1 0.9 0.4 0.6 ...
        Error in compileCPT(list(eh, oil.eh, inf.oil.eh, bp.oil, rt.inf.eh)) : 
      Table dimensions do not match!

【问题讨论】:

    标签: r bayesian


    【解决方案1】:

    您为bp|oil 的条件概率表指定了太多值。因为oil 有 2 个级别,bp 3 你需要 6 个条件概率,但你有 12 个在行中

    bp.oil <- cptable(~bp | oil, values=c(0.9, 0.1, 0.1, 0.9, 0, 1, 0.1, 0.9, 0.4, 0.6, 0.5, 0.5), levels=lmh)
    

    【讨论】:

    • 感谢 Harald - 我已经习惯于为二值变量添加 p、1-p 对,并在这里做了同样的事情!
    • @Kate 你是怎么解决这个问题的?我得到了确切的错误和同样的问题
    猜你喜欢
    • 1970-01-01
    • 2017-09-12
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2014-12-14
    相关资源
    最近更新 更多