bnlearn Error: Wrong number of conditional probability distributions

bnlearn Error: Wrong number of conditional probability distributions

我正在学习使用 bnlearn,我在下面的代码的最后一行中 运行 出现以下错误:

Error in custom.fit(dag, cpt) : wrong number of conditional probability distributions

我做错了什么?

    modelstring(dag)= "[s][r][nblw|r][nblg|nblw][mlw|s:r][f|s:r:mlw][mlg|mlw:f] 
    [mlgr|mlg:nblg]"
    ###View DAG Specifics  
    dag
    arcs(dag)
    nodes(dag)
  # Create Levels
  State <- c("State0", "State1")
 ##Create probability distributions given; these are all 2d b/c they have 1 or 2 nodes
  cptS <- matrix(c(0.6, 0.4), ncol=2, dimnames=list(NULL, State))
  cptR <- matrix(c(0.7, 0.3), ncol=2, dimnames=list(NULL, State))
  cptNBLW <- matrix(c(0.95, 0.05, 0.05, 0.95), ncol=2, dimnames=list(NULL, "r"= State))
  cptNBLG <- matrix(c(0.9, 0.099999999999999998, 0.2, 0.8), ncol=2, dimnames=list(NULL, 
  "nblw"=State))
  cptMLG <- matrix(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),ncol=2,nrow = 2,
         dimnames=list("mlw"= State, "f"=State))

 cptMLGR <- matrix(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45),ncol=2,nrow = 2,
              dimnames=list("mlg"= State, "nblg"=State))

 cptMLW <-matrix(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), ncol=2,nrow = 2,byrow = TRUE,
           dimnames=list("r"= State, "s"=State))

    # Build  3-d matrices( becuase you have 3 nodes, you can't use the matrix function; you 
 have to build it from scratch)
 cptF <- c(0.05, 0.95, 0.4, 0.6, 0.9, 0.1, 0.99, 0.01, 0.9, 0.1, 0.95, 0.05, 0.95, 0.05, 0.99, 
  0.01)
   dim(cptF) <- c(2, 2, 2, 2)
  dimnames(cptF) <- list("s"=State, "r"=State, "mlw"=State)


             ###Create CPT Table
     cpt <- list(s = cptS, r = cptR, mlw = cptMLW,nblw= cptNBLW,
         mlg= cptMLG, nblg= cptNBLG, mlgr= cptMLGR)
   # Construct BN network with Conditional Probability Table
    S.net <- custom.fit(dag,cpt)

参考:https://rpubs.com/sarataheri/bnlearnCGM

您的 CPT 定义有几个错误。首先,您需要确保:

  • 提供的概率数等于子节点和父节点中状态数的乘积,
  • 即matrix/array的维数等于父节点数加一,对于子节点,
  • 当节点维度大于1时,子节点应该在第一个维度给出。
  • dimnames 参数中给出的名称(例如 dimnames=list(ThisName = ...) 中的名称)应与 DAG 中定义的名称匹配,在你的情况下为 modelstring,在我的情况下用 model2network 回答。 (所以我之前使用 dimnames=list(cptNBLW = ...) 的建议应该是 dimnames=list(nblw = ...) 以匹配节点 nblw 在模型字符串中的声明方式)

您也没有将节点 f 添加到您的 cpt 列表中。

以下是您的代码,其中包含已更改的注释。 (我已经注释掉了有问题的行并在之后直接添加了行)

library(bnlearn)

dag <- model2network("[s][r][nblw|r][nblg|nblw][mlw|s:r][mlg|mlw:f][mlgr|mlg:nblg][f|s:r:mlw]")

State <- c("State0", "State1")
cptS <- matrix(c(0.6, 0.4), ncol=2, dimnames=list(NULL, State))
cptR <- matrix(c(0.7, 0.3), ncol=2, dimnames=list(NULL, State))
 
# add child node into first slot of dimnames
cptNBLW <- matrix(c(0.95, 0.05, 0.05, 0.95), ncol=2, dimnames=list(nblw=State, "r"= State))
cptNBLG <- matrix(c(0.9, 0.099999999999999998, 0.2, 0.8), ncol=2, dimnames=list(nblg=State,"nblw"=State))
 
# Use a 3d array and not matrix, and add child node into dimnames
# cptMLG <- matrix(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),ncol=2,nrow = 2, dimnames=list("mlw"= State, "f"=State))
cptMLG <- array(c(0.95, 0.05, 0.4, 0.6, 0.2, 0.8, 0.05, 0.95),dim=c(2,2,2), dimnames=list(mlg = State, "mlw"= State, "f"=State))

# cptMLGR <- matrix(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45),ncol=2,nrow = 2, dimnames=list("mlg"= State, "nblg"=State))
cptMLGR <- array(c(0.6,0.4,0.95,0.05,0.2,0.8,0.55,0.45), dim=c(2,2,2), dimnames=list(mlgr=State, "mlg"= State, "nblg"=State))

# cptMLW <-matrix(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), ncol=2,nrow = 2,byrow = TRUE,  dimnames=list("r"= State, "s"=State))
cptMLW <-array(c(0.95, 0.05, 0.1, 0.9, 0.2, 0.8, 0.01, 0.99), dim=c(2,2,2),  dimnames=list(mlw=State, "r"= State, "s"=State))

# add child into first slot of dimnames
cptF <- c(0.05, 0.95, 0.4, 0.6, 0.9, 0.1, 0.99, 0.01, 0.9, 0.1, 0.95, 0.05, 0.95, 0.05, 0.99, 0.01)
dim(cptF) <- c(2, 2, 2, 2)
dimnames(cptF) <- list("f" = State, "s"=State, "r"=State, "mlw"=State)

# add missing node f into list
cpt <- list(s = cptS, r = cptR, mlw = cptMLW,nblw= cptNBLW, mlg= cptMLG, nblg= cptNBLG, mlgr= cptMLGR, f=cptF)
  
# Construct BN network with Conditional Probability Table
S.net <- custom.fit(dag, dist=cpt)