Replace CPTs of Bayesian network.

replace_cpt(object, value)

# S3 method for class 'cpt_grain'
replace_cpt(object, value)

Arguments

object

A grain object.

value

A named list, see examples below.

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/.

Author

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

Examples

## 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")]
#> $R
#> R
#> yes  no 
#> 0.2 0.8 
#> 
#> $S
#>      R
#> S      yes  no
#>   yes 0.01 0.4
#>   no  0.99 0.6
#> 

# 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")]
#> $R
#> R
#> yes  no 
#> 0.3 0.7 
#> 
#> $S
#>      R
#> S     yes  no
#>   yes 0.1 0.7
#>   no  0.9 0.3
#>