rmachine-learningbayesian-networksbnlearn

bnlearn wrong dimensions for node


I was working on a simple problem in R. This is the code:

library(bnlearn)

dag <- model2network("[Location][Quality][Cost|Location:Quality][NoPeople|Location:Cost]")
plot(dag)

quality.values <- factor(c("Good", "Normal", "Bad"))
location.values <- factor(c("Good", "Bad"))
cost.values <- factor(c("High", "Low"))
nopeople.values <- factor(c("High", "Low"))

quality.prob <- array(c(0.3, 0.5, 0.2), dim = 3, dimnames = list(quality = quality.values))
location.prob <- array(c(0.6, 0.4), dim = 2, dimnames = list(location = location.values))
cost.prob <- array(c(0.8, 0.2, 0.6, 0.4, 0.1, 0.9, 0.6, 0.4, 0.6, 0.4, 0.05, 0.95), dim = c(2, 3, 2), dimnames = 
    list(cost = cost.values, quality = quality.values, location = location.values))
nopeople.prob <- array(c(0.6, 0.4, 0.8, 0.2, 0.1, 0.9, 0.6, 0.4), dim = c(2, 2, 2), dimnames = 
    list(NoPeople = nopeople.values, cost = cost.values, location = location.values))

condProbTable <- list(Location = location.prob, Quality = quality.prob, Cost = cost.prob, NoPeople = nopeople.prob)
bn <- custom.fit(dag, condProbTable)

Except when I try this I get a Error in check.dnode.vs.spec(dist[[cpd]], old = fitted[[cpd]]$parents, : wrong dimensions for node Cost error. I'm not sure what I'm doing wrong. I'm pretty new to R so any help would be great.

For reference, I'm trying to build this:

Bayesian network I'm trying to create

Thanks!


Solution

  • AS it turns out, it was a capitalization issue. Doing this

    dag <- model2network("[location][quality][cost|location:quality][nopeople|location:cost]")
    

    will fix the problem.