sample_cpdist: Sampling from posterior distribution of a Bayesian network at...

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

View source: R/sample_cpdist.R

Description

Take a bayesian network, a node of interst, and a return value type option and returns either the probability/sampled data (depending on the op argument specified) of the different states of the node of interst given the different conbinaition of its and its parents states.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sample_cpdist(
  bn,
  node,
  op = c("sampler", "proba"),
  evidence = NULL,
  n_generation = NULL,
  include_relatives = TRUE,
  n_try = 10,
  n_run = 1000
)

Arguments

bn

an object of class bn.fit.

node

character string, the label of the node which conditional distribution is of interest.

op

a vector of character strings, the type of returned value: either probabilities or raw data sampled from the posterior distribution

evidence

a name value pair:a named character vector which values are node states and names are node names.

n_generation

how far to go in the network topology for building the conditionning specification for the query?

include_relatives

logical Should parents or ancestors, depending on the the argument n_generation, should be included in the query? If TRUE, the default, these will be internally involved in constructing the evidence argument.

n_try

integer cpquery often fails, when the argument evidence involves deep query, returning NULL. n_try can be used to retry the function call n_try times.

n_run

integer specifying the number of of model run. Default is 1000.

Details

Each row consists in a comditional probability of a state of the node of interest given the combination of the states of the parent nodes.

Value

A matrix containing either the probabilities or raw data sampled from the posterior distribution

Author(s)

Issoufou Liman

See Also

cpquery

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
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 <- as.bn.fit(network)
sample_cpdist (network, 'Soil_water_holding_capacity', n_try = 10, n_run =10)

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