I was trying to make a simple illustration of computing conditional probability using bnlearn
package in R
. I have a simple graph with three nodes:
DAG = model2network("[A][B][C|A:B]")
cptA = matrix(c(0.7, 0.3), ncol = 2, dimnames = list(NULL, c("True", "False")))
cptB = matrix(c(0.5, 0.5), ncol = 2, dimnames = list(NULL, c("True", "False")))
cptC = c(1, 0, 0.5, 0.5, 0, 1, 0.5, 0.5)
dim(cptC) = c(2, 2, 2)
dimnames(cptC) = list("C" = c("True", "False"), "A" = c("True", "False"), "B" = c("True", "False"))
fitted = custom.fit(DAG, dist = list("A" = cptA, "B" = cptB, "C" = cptC))
Now I want to keep node A unchanged, and change node C to have probability 1 of being True and 0 of being False, and I want to know the probability distribution of B after this change. I tried to use cpquery()
in the following way:
cpquery(fitted, event = ("B" == "True"), evidence = ("C" == "True"))
and I got an error code:
Error in sampling(fitted = fitted, event = event, evidence = evidence, :
logical vector for evidence is of length 1 instead of 5000.
I am relatively new to the package and I wonder if I misunderstood the code or if there are better ways to do this. I tried to look up online for the solution and they all used the seemingly built-in dataset which in my case there is actually no data but more like manipulating the CPT at some nodes.
user24992972 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.