R gRain 错误 - table 尺寸不匹配
R gRain error - table dimensions do not match
我正在使用 R 中的 gRain 包创建贝叶斯网络。尝试编译条件概率 tables 时,出现错误 'Table dimensions do not match'。所讨论的 table 采用 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!
您为 bp|oil
的条件概率 table 指定的值过多。由于 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)
我正在使用 R 中的 gRain 包创建贝叶斯网络。尝试编译条件概率 tables 时,出现错误 'Table dimensions do not match'。所讨论的 table 采用 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!
您为 bp|oil
的条件概率 table 指定的值过多。由于 oil
有 2 个水平和 bp
3 你需要 6 个条件概率但是你在
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)