cpt.Rd
Creates conditional probability tables of the form p(v|pa(v)).
cpt(names, levels, values, normalize = "first", smooth = 0)
cptable(vpar, levels = NULL, values = NULL, normalize = TRUE, smooth = 0)
Specifications of the names in P(v|pa1,...pak). See section 'details' for information about the form of the argument.
a list with specification of the levels of the
factors in names
or 2) a vector with number of levels of
the factors in names
. See 'examples' below.
Probabilities; recycled if necessary. Regarding the order, please see section 'details' and the examples.
See 'details' below.
Should values be smoothed, see 'Details' below.
node an its parents
An array.
cptable
is simply a wrapper for cpt
and the functions can hence
be used synonymously.
If smooth
is non–zero, then this value is added to all cells before
normalization takes place.
Regarding the form of the argument names
: To specify
\(P(a|b,c)\) one may write ~a|b:c
, ~a:b:c
,
~a|b+c
, ~a+b+c
or c("a","b","c")
. Internally,
the last form is used. Notice that the +
and :
operator are used as a separators only. The order of the variables IS
important so the operators DO NOT commute.
The first variable in levels
varies fastest.
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 the wet grass example at
## https://en.wikipedia.org/wiki/Bayesian_network
yn <- c("yes", "no")
ssp <- list(R=yn, S=yn, G=yn) # state space
## Different forms
t1 <- cpt(c("S", "R"), levels=ssp, values=c(.01, .99, .4, .6))
t2 <- cpt(~S:R, levels=ssp, values=c(.01, .99, .4, .6))
t3 <- cpt(~S:R, levels=c(2, 2), values=c(.01, .99, .4, .6))
t4 <- cpt(~S:R, levels=yn, values=c(.01, .99, .4, .6))
t1; t2; t3; t4
#> R
#> S yes no
#> yes 0.01 0.4
#> no 0.99 0.6
#> R
#> S yes no
#> yes 0.01 0.4
#> no 0.99 0.6
#> R
#> S 1 2
#> 1 0.01 0.4
#> 2 0.99 0.6
#> R
#> S yes no
#> yes 0.01 0.4
#> no 0.99 0.6
varNames(t1)
#> [1] "S" "R"
valueLabels(t1)
#> $S
#> [1] "yes" "no"
#>
#> $R
#> [1] "yes" "no"
#>
## Wet grass example
ssp <- list(R=yn, S=yn, G=yn) # state space
p.R <- cpt(~R, levels=ssp, values=c(.2, .8))
p.S_R <- cpt(~S:R, levels=ssp, values=c(.01, .99, .4, .6))
p.G_SR <- cpt(~G:S:R, levels=ssp, values=c(.99, .01, .8, .2, .9, .1, 0, 1))
wet.cpt <- compileCPT(p.R, p.S_R, p.G_SR)
wet.cpt
#> P( R )
#> P( S | R )
#> P( G | S R )
wet.cpt$S # etc
#> R
#> S yes no
#> yes 0.01 0.4
#> no 0.99 0.6
# A Bayesian network is created with:
wet.bn <- grain(wet.cpt)