data(chest_cpt)
chest.bn <- grain(compileCPT(chest_cpt))
chest.bn <- compile(chest.bn)
uni <- list(asia = c("yes", "no"), tub = c("yes", "no"),
smoke = c("yes", "no"), lung = c("yes", "no"),
bronc = c("yes", "no"), either = c("yes", "no"),
xray = c("yes", "no"), dysp = c("yes", "no"))
ev <- list(tabNew("asia", levels=uni, values=c(1,0)),
tabNew("dysp", levels=uni, values=c(1,0)),
tabNew(c("dysp","bronc"), levels=uni, values=c(.1, .2, .9, .8)) )
chest.bn
chest.bn2 <- setJEvidence(chest.bn, evidence=ev)
chest.bn2
getEvidence(chest.bn2)
# Notice: The evidence is defined on (subsets of) cliques of the junction tree
# and therefore evidence can readily be absorbed:
getgrain(chest.bn, "rip")$cliques %>% str
# On the other hand, below is evidence which is not defined cliques
# of the junction tree and therefore evidence can not easily be
# absorbed. Hence this will fail:
if (FALSE) {
ev.fail <- list(tabNew(c("dysp","smoke"), levels=uni, values=c(.1, .2, .9, .8)) )
setJEvidence(chest.bn, evidence=ev.fail)
}
## Evidence can be removed with
retractJEvidence(chest.bn2) ## All evidence removed.
retractJEvidence(chest.bn2, 0) ## No evidence removed.
retractJEvidence(chest.bn2, 1:2) ## Evidence items 1 and 2 are removed.
# Setting additional joint evidence to an object where joint
# evidence already is set will cause an error. Hence this will fail:
if (FALSE) {
ev2 <- list(smoke="yes")
setJEvidence(chest.bn2, evidence=ev2)
}
## Instead we can do
new.ev <- c(getEvidence(chest.bn2), list(smoke="yes"))
chest.bn
setJEvidence(chest.bn, evidence=new.ev)
## Create joint evidence object:
yn <- c("yes", "no")
db <- tabNew(c("dysp", "bronc"), list(dysp=yn, bronc=yn), values=c(.1, .2, .9, .8))
db
ev <- list(asia=c(1, 0), dysp="yes", db)
jevi <- new_jev(ev, levels=uni)
jevi
chest.bn3 <- setJEvidence(chest.bn, evidence=jevi)
evidence(chest.bn3)
Run the code above in your browser using DataLab