## Asia (chest clinic) example:
yn <- c("yes","no")
a <- cptable(~asia, values=c(1,99), levels=yn)
t.a <- cptable(~tub+asia, values=c(5,95,1,99), levels=yn)
s <- cptable(~smoke, values=c(5,5), levels=yn)
l.s <- cptable(~lung+smoke, values=c(1,9,1,99), levels=yn)
b.s <- cptable(~bronc+smoke, values=c(6,4,3,7), levels=yn)
e.lt <- cptable(~either+lung+tub, values=c(1,0,1,0,1,0,0,1), levels=yn)
x.e <- cptable(~xray+either, values=c(98,2,5,95), levels=yn)
d.be <- cptable(~dysp+bronc+either, values=c(9,1,7,3,8,2,1,9), levels=yn)
plist <- compileCPT(list(a, t.a, s, l.s, b.s, e.lt, x.e, d.be))
pn <- grain(plist)
pn
summary(pn)
plot(pn)
pnc <- compile(pn, propagate=TRUE)
## If we want to query the joint distribution of the disease nodes,
## computations can be speeded up by forcing these nodes to be in
## the same clique of the junction tree:
pnc2 <- compile(pn, root=c("lung", "bronc", "tub"), propagate=TRUE)
system.time({
for (i in 1:200)
querygrain(pnc, nodes=c("lung","bronc", "tub"), type="joint")})
system.time({
for (i in 1:200)
querygrain(pnc2, nodes=c("lung","bronc", "tub"), type="joint")})
## Create network from gmData (with data) and graph specification.
## There are different ways:
data(HairEyeColor)
d <- HairEyeColor
daG <- dagList(list(~Hair, ~Eye:Hair, ~Sex:Hair))
class( daG )
uG <- ugList(list(~Eye:Hair, ~Sex:Hair))
class( uG )
## Create directly from dag:
b1 <- grain( daG, d )
class( b1 )
## Build model from undirected (decomposable) graph
b3 <- grain( uG, d )
class( b3 )
## Simple example - one clique only in triangulated graph:
plist <- compileCPT( list(a, t.a) )
pn <- grain( plist )
querygrain(pn)
## Simple example - disconnected network:
plist <- compileCPT( list(a, t.a, s) )
pn <- grain( plist )
querygrain( pn )
Run the code above in your browser using DataLab