Learn R Programming

gRain (version 1.3-2)

extract_components: Extract conditional probabilities and clique potentials from data.

Description

Extract list of conditional probability tables and list of clique potentials from data.

Usage

extract_cpt(data_, graph, smooth = 0)

extract_pot(data_, graph, smooth = 0)

extract_marg(data_, graph, smooth = 0)

data2cpt(data_, graph, smooth = 0)

data2pot(data_, graph, smooth = 0)

data2marg(data_, graph, smooth = 0)

marg2pot(mg)

pot2marg(pt)

Arguments

data_

A named array or a dataframe.

graph

A graphNEL object or a list or formula which can be turned into a graphNEL object by calling ug or dag. For extract_cpt, graph must be/define a DAG while for extract_pot, graph must be/define undirected triangulated graph.

smooth

See 'details' below.

mg

An object of class marg_rep

pt

An object of class pot_rep

Value

  • extract_cpt: A list of conditional probability tables.

  • extract_pot: A list of clique potentials.

Details

If smooth is non-zero then smooth is added to all cell counts before normalization takes place.

extractCPT is alias for extract_cpt extractPOT is alias for extract_pot and extractMARG is alias for extract_marg; retained for backward compatibility.

References

S<U+00F8>ren H<U+00F8>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/.

See Also

compile_cpt, compile_pot, grain

Examples

Run this code
# NOT RUN {
## FIXME: Review example 
## Asia (chest clinic) example:

## Version 1) Specify conditional probability tables.
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))
pn1 <- grain(plist)
q1 <- querygrain(pn1)

## Version 2) Specify DAG and data
data(chestSim100000, package="gRbase")
dgf   <- ~asia + tub * asia + smoke + lung * smoke +
         bronc * smoke + either * tub * lung +
         xray * either + dysp * bronc * either
dg    <- dag(dgf)
pp    <- extract_cpt(chestSim100000, dg)

pn2   <- grain(pp)
## Same as:
cpp2  <- compileCPT(pp)
pn2   <- grain(cpp2)

q2    <- querygrain(pn2)

## Version 2) Specify triangulated undirected graph and data
ugf <- list(c("either", "lung", "tub"), c("either", "lung", "bronc"), 
    c("either", "xray"), c("either", "dysp", "bronc"), c("smoke", 
    "lung", "bronc"), c("asia", "tub"))
gg    <- ug(ugf)
pp    <- extract_pot(chestSim100000, gg)

pn3   <- grain(pp)
## Same as:
cpp3  <- compilePOT(pp)
pn3   <- grain(cpp3)

q3    <- querygrain(pn3)

## Compare results:
str(q1)
str(q2[names(q1)])
str(q3[names(q1)])

# }

Run the code above in your browser using DataLab