extract_bn: Extract a Bayesian network based on string model...

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/extract_bn.R

Description

Take a fully specified bayesian network, a sub string model, and return a subset of former matching the configuration of the latter.

Usage

1
extract_bn(bn, string_model)

Arguments

bn

an object of class bn.fit or grain-main.

string_model

Character string describing the configuration of the Bayesian network as return by modelstring.

Details

Currently only the classes of the arguments are checked. The user should take care of providing the right inputs.

Value

An object of class bn.fit or grain-main depending on the class of bn.

Author(s)

Issoufou Liman

See Also

bn.fit

modelstring

Examples

 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
library (gRain)
library(bnlearn)
## setting a bayesian network with gRain
Soil_type <- cptable (~Soil_type, values = c(0.05, 0.55, 0.4),
levels = c('Sandy', 'Loamy', 'Clayey'))
Manure_application <- cptable(~Manure_application, values = c(0.3, 0.7),
levels = c('FALSE', 'TRUE'))
Soil_water_holding_capacity_tmp <- make_gRain_CPT(
 parent_effects = list(c(0, 2.5, 3), c(0, 2)),
 parent_weights = c(2,1),
 b = 3,
 child_prior = c(0.2,0.5,0.3),
 child_states = c('Low', 'Medium', 'High'),
 parent_states = list(c('Sandy', 'Loamy', 'Clayey'), c('FALSE', 'TRUE'))
)
Soil_water_holding_capacity_values <- Soil_water_holding_capacity_tmp$values
Soil_water_holding_capacity_levels <- Soil_water_holding_capacity_tmp$levels
Soil_water_holding_capacity <- cptable (
~Soil_water_holding_capacity|Soil_type:Manure_application,
values = Soil_water_holding_capacity_values,
levels = Soil_water_holding_capacity_levels)
## Compile conditional probability tables
network <- compileCPT(list(Soil_type, Manure_application, Soil_water_holding_capacity))
## Graphical Independence Network ####
network <- grain(network)
## converting the grain bayesian network to bn.fit
network_bn_fit <- as.bn.fit(network)

## extract the entire Bayesian network as it is
extracted <- extract_bn(network_bn_fit, modelstring(as.bn.fit(network)))
graphviz.plot (extracted, shape = 'ellipse')

## Dropping all but soil type and Manure application nodes from the Bayesian network
extracted <- extract_bn(network_bn_fit, '[Soil_type][Manure_application]')
graphviz.plot (extracted, shape = 'ellipse')

## Dropping Manure_application
extracted <- extract_bn(network_bn_fit, '[Soil_type][Soil_water_holding_capacity|Soil_type]')
graphviz.plot (extracted, shape = 'ellipse')

Issoufou-Liman/decisionSupportExtra documentation built on Dec. 21, 2020, 6:28 p.m.