Description Usage Arguments Details Value Author(s) References See Also Examples
View source: R/cptable-create.R
Creates conditional probability tables of the form p(v|pa(v)).
1 |
vpar |
Specifications of the names in P(v|pa1,...pak). See section 'details' for information about the form of the argument. |
levels |
See 'details' below. |
values |
Probabilities; recycled if necessary. Regarding the order, please see section 'details' and the examples. |
normalize |
See 'details' below. |
smooth |
See 'details' below. |
If normalize=TRUE
then the probabilities are normalized to sum to one
for each configuration of the parents.
If smooth
is non–zero then zero entries of values
are
replaced with smooth
before normalization takes place.
Regarding the form of the argument vpar
: 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 is used as a separator only. The order of the
variables IS important so the operators DO NOT commute.
If a
has levels a1,a2
and likewise for b
and c
then the order of values
corresponds to the configurations
(a1,b1,c1)
, (a2,b1,c1)
(a1,b2,c1)
, (a2,b2,c1)
etc. That is, the first variable varies fastest. Hence the first two
elements in values
will be the conditional probabilities of a
given b=b1, c=c1
.
A cptable
object (a numeric vector with various attributes).
Søren Højsgaard, sorenh@math.aau.dk
Søren Højsgaard (2012). Graphical Independence Networks with the gRain Package for R. Journal of Statistical Software, 46(10), 1-26. http://www.jstatsoft.org/v46/i10/.
andtable
, ortable
,
extractCPT
, compileCPT
,
extractPOT
, compilePOT
,
grain
, parray
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ## 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)
# or
ssp <- list(R=yn, S=yn, G=yn) # state space
p.R <- cptable(~R, values=c(.2, .8), levels=ssp)
p.S_R <- cptable(~S:R, values=c(.01, .99, .4, .6), levels=ssp)
p.G_SR <- cptable(~G:S:R, values=c(.99, .01, .8, .2, .9, .1, 0, 1), levels=ssp)
# components above are "intermediate representations" and are turned into arrays with
wet.cpt <- compileCPT(p.R, p.S_R, p.G_SR)
wet.cpt
wet.cpt$S # etc
# A Bayesian network is created with:
wet.bn <- grain(wet.cpt)
# Can also create arrays directly
## Not run:
ssp <- list(R=yn, S=yn, G=yn) # state space
p.R <- c(.2, .8)
p.S_R <- c(.01, .99, .4, .6)
p.G_SR <- c(.99, .01, .8, .2, .9, .1, 0, 1)
dim(p.R) <- 2
dimnames(p.R) <- ssp["R"]
dim(p.S_R) <- c(2, 2)
dimnames(p.S_R) <- ssp[c("S", "R")]
dim(p.G_SR) <- c(2, 2, 2)
dimnames(p.G_SR) <- ssp[c("G", "S", "R")]
# Arrays can be created (easier?) with parray() from gRbase
p.R <- parray("R", levels=ssp, values=c(.2, .8))
p.S_R <- parray(c("S", "R"), levels = ssp, values=c(.01, .99, .4, .6))
p.G_SR <- parray(~ G:S:R, levels = ssp, values=c(.99, .01, .8, .2, .9, .1, 0, 1))
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.