barplot_CPT: A ggplot wrapper for visualizing Conditional Probability...

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

View source: R/barplot_CPT.R

Description

Take a bayesian network, a node of interest along with other optional ggplot paramaters to produce a barchart corresponding to the CPT of the node of interest given the Bayesian network.

Usage

1
2
3
4
5
6
7
8
9
barplot_CPT(
  bn,
  target_node,
  bar_width = 0.25,
  ncol_facet = NULL,
  n_pages = 1,
  show_states_only = FALSE,
  separator = "\n"
)

Arguments

bn

A grain object from gRain package (see grain-main) or a bn.fit object from bnlearn package.

target_node

A single character indicating the name of the node which CPT is to be plotted.

bar_width

numeric. The width of the bars in the barplot (see geom_bar).

ncol_facet

A single integer specifying the number of facet columns (see ncol argument in geom_bar).

n_pages

A single integer specifying the number of pages over which the plot is be spread over. This can be particularly useful for lisibility in large CPT plot.

show_states_only

logical. Should both node names and their respective states be shown in the strip labels? If TRUE only the state labels are shown.

separator

character or regular expression. How should node names and their states be separated?

Details

It often difficult to know whether something went wrong or not looking at the CPT generated by make_CPT. barplot_CPT may help to graphically visualise the generated CPT by make_gRain_CPT and fix potential mis-specification. This function make visualization and examination of these CPTs easier.

Value

A single ggplot object (or a list of these, if ncol_facet > 1) such that one could customize it with theme and other ggplot updating utilites.

Author(s)

Issoufou Liman

See Also

make_gRain_CPT.

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(ggplot2)
## Simple nodes specification using gRain package.
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'))
## Complex nodes specification.
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)
network
barplot_CPT (network, "Soil_water_holding_capacity")

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