old_components_extract.Rd
Extract list of conditional probability tables and list of clique potentials from data.
extractCPT(data_, graph, smooth = 0)
extractPOT(data_, graph, smooth = 0)
extractMARG(data_, graph, smooth = 0)
extract_cpt
: A list of conditional probability tables.
extract_pot
: A list of clique potentials.
extract_marg
: A list of clique marginals.
If smooth
is non-zero then smooth
is added
to all cell counts before normalization takes place.
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/.
## Extract cpts / clique potentials from data and graph
# specification and create network. There are different ways:
data(lizard, package="gRbase")
# DAG: height <- species -> diam
daG <- dag(~species + height:species + diam:species, result="igraph")
# UG : [height:species][diam:species]
uG <- ug(~height:species + diam:species, result="igraph")
pt <- extract_pot(lizard, ~height:species + diam:species)
cp <- extract_cpt(lizard, ~species + height:species + diam:species)
pt
#> [[1]]
#> species
#> height anoli dist
#> >4.75 0.1051345 0.2493888
#> <=4.75 0.2958435 0.3496333
#>
#> [[2]]
#> species
#> diam anoli dist
#> <=4 0.7195122 0.5469388
#> >4 0.2804878 0.4530612
#>
#> attr(,"rip")
#> cliques
#> 1 : height species
#> 2 : species diam
#> separators
#> 1 :
#> 2 : species
#> parents
#> 1 : 0
#> 2 : 1
#> attr(,"graph")
#> IGRAPH f63b773 UN-- 3 2 --
#> + attr: name (v/c)
#> + edges from f63b773 (vertex names):
#> [1] height --species species--diam
#> attr(,"class")
#> [1] "pot_representation"
cp
#> [[1]]
#> species
#> anoli dist
#> 0.400978 0.599022
#>
#> [[2]]
#> species
#> height anoli dist
#> >4.75 0.2621951 0.4163265
#> <=4.75 0.7378049 0.5836735
#>
#> [[3]]
#> species
#> diam anoli dist
#> <=4 0.7195122 0.5469388
#> >4 0.2804878 0.4530612
#>
#> attr(,"graph")
#> IGRAPH b2e0ec3 DN-- 3 2 --
#> + attr: name (v/c)
#> + edges from b2e0ec3 (vertex names):
#> [1] species->height species->diam
#> attr(,"class")
#> [1] "cpt_representation"
# Both specify the same probability distribution
tabListMult(pt) |> as.data.frame.table()
#> height diam species Freq
#> 1 >4.75 <=4 anoli 0.07564554
#> 2 <=4.75 <=4 anoli 0.21286302
#> 3 >4.75 >4 anoli 0.02948894
#> 4 <=4.75 >4 anoli 0.08298050
#> 5 >4.75 <=4 dist 0.13640038
#> 6 <=4.75 <=4 dist 0.19122798
#> 7 >4.75 >4 dist 0.11298837
#> 8 <=4.75 >4 dist 0.15840527
tabListMult(cp) |> as.data.frame.table()
#> height diam species Freq
#> 1 >4.75 <=4 anoli 0.07564554
#> 2 <=4.75 <=4 anoli 0.21286302
#> 3 >4.75 >4 anoli 0.02948894
#> 4 <=4.75 >4 anoli 0.08298050
#> 5 >4.75 <=4 dist 0.13640038
#> 6 <=4.75 <=4 dist 0.19122798
#> 7 >4.75 >4 dist 0.11298837
#> 8 <=4.75 >4 dist 0.15840527
if (FALSE) { # \dontrun{
# Bayesian networks can be created as
bn.uG <- grain(pt)
bn.daG <- grain(cp)
# The steps above are wrapped into a convenience method which
# builds a network from at graph and data.
bn.uG <- grain(uG, data=lizard)
bn.daG <- grain(daG, data=lizard)
} # }