Learn R Programming

gRain (version 1.4.5)

replace_cpt: Replace CPTs in Bayesian network

Description

Replace CPTs of Bayesian network.

Usage

replace_cpt(object, value)

# S3 method for cpt_grain replace_cpt(object, value)

Arguments

object

A grain object.

value

A named list, see examples below.

Author

Søren Højsgaard, sorenh@math.aau.dk

Details

When a Bayesian network (BN) is constructed from a list of conditional probability tables (CPTs) (e.g. using the function grain()), various actions are taken:

  1. It is checked that the list of CPTs define a directed acyclic graph (DAG).

  2. The DAG is moralized and triangulated.

  3. A list of clique potentials (one for each clique in the triangulated graph) is created from the list of CPTs.

  4. The clique potentials are, by default, calibrated to each other so that the potentials contain marginal distributions.

The function described here bypass the first two steps which can provide an important gain in speed compared to constructing a new BN with a new set of CPTs with the same DAG.

References

Søren Højsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. https://www.jstatsoft.org/v46/i10/.

See Also

grain, propagate, triangulate, rip, junctionTree

Examples

Run this code
## See the wet grass example at
## https://en.wikipedia.org/wiki/Bayesian_network

yn <- c("yes", "no")
p.R    <- cptable(~R, values=c(.2, .8), levels=yn)
p.S_R  <- cptable(~S:R, values=c(.01, .99, .4, .6), levels=yn)
p.G_SR <- cptable(~G:S:R, values=c(.99, .01, .8, .2, .9, .1, 0, 1), levels=yn)

wet.bn <- compileCPT(p.R, p.S_R, p.G_SR)  |> grain()
getgrain(wet.bn, "cpt")[c("R","S")]

# Update some CPTs
wet.bn <- replace_cpt(wet.bn, list(R=c(.3, .7), S=c(.1, .9, .7, .3)))
getgrain(wet.bn, "cpt")[c("R","S")]

Run the code above in your browser using DataLab