Title: | Visually Learning the Graphical Structure of Bayesian Networks and Performing MCMC with 'Stan' |
---|---|
Description: | Provides users with its associated functions for pedagogical purposes in visually learning Bayesian networks and Markov chain Monte Carlo (MCMC) computations. It enables users to: a) Create and examine the (starting) graphical structure of Bayesian networks; b) Create random Bayesian networks using a dataset with customized constraints; c) Generate Stan code for structures of Bayesian networks for sampling the data and learning parameters; d) Plot the network graphs; e) Perform Markov chain Monte Carlo computations and produce graphs for posteriors checks. The package refers to one reference item, which describes the methods and algorithms: Vuong, Quan-Hoang and La, Viet-Phuong (2019) <doi:10.31219/osf.io/w5dx6> The 'bayesvl' R package. Open Science Framework (May 18). |
Authors: | Viet-Phuong La [aut, cre], Quan-Hoang Vuong [aut] |
Maintainer: | Viet-Phuong La <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.0 |
Built: | 2024-11-10 03:30:24 UTC |
Source: | https://github.com/sshpa/bayesvl |
The R package for visually learning the graphical structures of Bayesian networks, and performing Hamiltonian MCMC with Stan through bvl_model2Stan
, bvl_modelFit
Package: | bayesvl |
Type: | Package |
Version: | 0.8.0 |
Date: | 13 May 2019 |
License: | GPL-3 |
Website: | Bayesvl |
Quan-Hoang Vuong, Viet-Phuong La
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
bayesvl-class
, bvl_modelFit
, bvl_model2Stan
# Design the model in directed acyclic graph model <- bayesvl() # add observed data nodes to the model model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") # add path between nodes model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") summary(model)
# Design the model in directed acyclic graph model <- bayesvl() # add observed data nodes to the model model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") # add path between nodes model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") summary(model)
Provides the interface to the functions in the bnlearn package for network diagnostics of an object of class bayesvl
.
# Interface to bn.fit function to fit the parameters of # a Bayesian network conditional on its structure. bvl_bnBayes(dag, data = NULL, method = "bayes", iss = 10, ...) # Interface to bnlearn score function to compute the score of the Bayesian network. bvl_bnScore(dag, data = NULL, ...) # Interface to arc.strength function to measure the strength of the probabilistic # relationships expressed by the arcs of a Bayesian network. bvl_bnStrength(dag, data = NULL, criterion = "x2", ...) # Interface to bn.fit.barchart function to plot fit # the parameters of a Bayesian network conditional on its structure. bvl_bnBarchart(dag, data = NULL, method = "bayes", iss = 10, ...)
# Interface to bn.fit function to fit the parameters of # a Bayesian network conditional on its structure. bvl_bnBayes(dag, data = NULL, method = "bayes", iss = 10, ...) # Interface to bnlearn score function to compute the score of the Bayesian network. bvl_bnScore(dag, data = NULL, ...) # Interface to arc.strength function to measure the strength of the probabilistic # relationships expressed by the arcs of a Bayesian network. bvl_bnStrength(dag, data = NULL, criterion = "x2", ...) # Interface to bn.fit.barchart function to plot fit # the parameters of a Bayesian network conditional on its structure. bvl_bnBarchart(dag, data = NULL, method = "bayes", iss = 10, ...)
dag |
an object of class |
data |
a data frame containing the variables in the model. |
method |
a character string, either mle for Maximum Likelihood parameter estimation or bayes for Bayesian parameter estimation (currently implemented only for discrete data). |
iss |
a numeric value, the imaginary sample size used by the bayes method to estimate the conditional probability tables associated with discrete nodes |
criterion |
a character string, the method using for measuring |
... |
extra arguments from the generic method |
bvl_bnScore()
return a number, value of score.
La Viet-Phuong, Vuong Quan-Hoang
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
Manipulate directed acyclic graph of an object of class bayesvl
.
# added a new node to the graph. bvl_addNode(dag, name, dist = "norm", priors = NULL, fun = NULL, out_type = NULL, lower = NULL, upper=NULL, test = NULL) # added a new path between nodes to the graph. bvl_addArc(dag, from, to, type = "slope", priors = NULL, fun = NULL) # added a new path between nodes to the graph. bvl_addArc(dag, from, to, type = "slope", priors = NULL, fun = NULL)
# added a new node to the graph. bvl_addNode(dag, name, dist = "norm", priors = NULL, fun = NULL, out_type = NULL, lower = NULL, upper=NULL, test = NULL) # added a new path between nodes to the graph. bvl_addArc(dag, from, to, type = "slope", priors = NULL, fun = NULL) # added a new path between nodes to the graph. bvl_addArc(dag, from, to, type = "slope", priors = NULL, fun = NULL)
dag |
an object of class |
name |
a character string, the name of a node. |
dist |
a character string, distribution code of the node ( |
priors |
a vector of string, the priors of the node or path. |
fun |
a character string, the transform function of the node. |
out_type |
a character string, the variable data type ( |
lower |
integer or real, the lower bound of variable data type ( |
upper |
integer or real, the upper bound of variable data type ( |
test |
a vector of testing values for variable. |
from |
a character string, the name of node the path connect from. |
to |
a character string, the name of node the path connect to. |
type |
a character string, the path type between nodes ( |
bvl_addNode(), bvl_addArc()
return object class bayesvl
.
La Viet-Phuong, Vuong Quan-Hoang
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
dag = bayesvl() # add nodes to dag dag = bvl_addNode(dag, "node1") dag = bvl_addNode(dag, "node2") # add the path between two nodes dag = bvl_addArc(dag, "node1", "node2") summary(dag)
dag = bayesvl() # add nodes to dag dag = bvl_addNode(dag, "node1") dag = bvl_addNode(dag, "node2") # add the path between two nodes dag = bvl_addArc(dag, "node1", "node2") summary(dag)
Provides plot methods and the interface to the MCMC module in the bayesplot package for plotting MCMC draws and diagnostics for an object of class bayesvl
.
# Plot network diagram to visualize the model bvl_bnPlot(dag, ...) # Plots historgram of regression parameters computed from posterior draws in grid layout bvl_plotParams (dag, row = 2, col = 2, credMass = 0.89, params = NULL) # The interface to mcmc_intervals for plotting uncertainty intervals # computed from posterior draws bvl_plotIntervals (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) # The interface to mcmc_intervals for plotting density computed from posterior draws bvl_plotAreas (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) bvl_plotPairs (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) bvl_plotDensity (dag, params = NULL, size = 1, labels = NULL) bvl_plotDensity2d(dag, x, y, color = NULL, color_scheme = "red", labels = NULL) bvl_plotTrace (dag, params = NULL) bvl_plotDiag (dag) bvl_plotGelman (dag, params = NULL) bvl_plotGelmans (dag, params = NULL, row = 2, col = 2) bvl_plotAcfs ( dag, params = NULL, row = 2, col = 2) bvl_plotTest (dag, y_name, test_name, n = 200, color_scheme = "blue")
# Plot network diagram to visualize the model bvl_bnPlot(dag, ...) # Plots historgram of regression parameters computed from posterior draws in grid layout bvl_plotParams (dag, row = 2, col = 2, credMass = 0.89, params = NULL) # The interface to mcmc_intervals for plotting uncertainty intervals # computed from posterior draws bvl_plotIntervals (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) # The interface to mcmc_intervals for plotting density computed from posterior draws bvl_plotAreas (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) bvl_plotPairs (dag, params = NULL, fun = "stat", stat = "mean", prob = 0.8, prob_outer = 0.95, color_scheme = "blue", labels = NULL) bvl_plotDensity (dag, params = NULL, size = 1, labels = NULL) bvl_plotDensity2d(dag, x, y, color = NULL, color_scheme = "red", labels = NULL) bvl_plotTrace (dag, params = NULL) bvl_plotDiag (dag) bvl_plotGelman (dag, params = NULL) bvl_plotGelmans (dag, params = NULL, row = 2, col = 2) bvl_plotAcfs ( dag, params = NULL, row = 2, col = 2) bvl_plotTest (dag, y_name, test_name, n = 200, color_scheme = "blue")
dag |
an object of class |
params |
Optional: character vector of parameter names. |
fun |
Optional: statistic function. |
stat |
Optional: the plotting function to call. |
prob |
Optional: the probability mass to include in the inner interval. Default is 0.8. |
prob_outer |
Optional: the probability mass to include in the outer interval. Default is 0.95. |
row |
Optional: number of rows of grid layout. |
col |
Optional: number of columns of grid layout. |
credMass |
Optional: specifying the mass within the credible interval. Default is 0.89. |
size |
Optional: the size of line width. |
color_scheme |
Optional: color scheme. Default is "blue" |
... |
extra arguments from the generic method |
y_name |
a character string. Name of outcome variable |
test_name |
a character string. Name of test variable and test value |
n |
number of yrep values to plot |
x |
a character string. Name of x parameter to pair with |
y |
a character string. Name of y parameter to pair with |
color |
a character string. Variable for color of points on density plot |
labels |
Optional: character vector of parameter labels. |
bvl_plotIntervals(), bvl_plotPairs()
return a ggplot object that can be further customized using the ggplot2 package.
La Viet-Phuong, Vuong Quan-Hoang
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
## create network model model <- bayesvl() ## add the observed data nodes model <- bvl_addNode(model, "O", "binom") model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "Viol", "binom") model <- bvl_addNode(model, "VB", "binom") model <- bvl_addNode(model, "VC", "binom") model <- bvl_addNode(model, "VT", "binom") model <- bvl_addNode(model, "Int1", "binom") model <- bvl_addNode(model, "Int2", "binom") ## add the tranform data nodes and arcs as part of the model model <- bvl_addNode(model, "B_and_Viol", "trans") model <- bvl_addNode(model, "C_and_Viol", "trans") model <- bvl_addNode(model, "T_and_Viol", "trans") model <- bvl_addArc(model, "VB", "B_and_Viol", "*") model <- bvl_addArc(model, "Viol", "B_and_Viol", "*") model <- bvl_addArc(model, "VC", "C_and_Viol", "*") model <- bvl_addArc(model, "Viol", "C_and_Viol", "*") model <- bvl_addArc(model, "VT", "T_and_Viol", "*") model <- bvl_addArc(model, "Viol", "T_and_Viol", "*") model <- bvl_addArc(model, "B_and_Viol", "O", "slope") model <- bvl_addArc(model, "C_and_Viol", "O", "slope") model <- bvl_addArc(model, "T_and_Viol", "O", "slope") model <- bvl_addArc(model, "Viol", "O", "slope") model <- bvl_addNode(model, "B_and_Lie", "trans") model <- bvl_addNode(model, "C_and_Lie", "trans") model <- bvl_addNode(model, "T_and_Lie", "trans") model <- bvl_addArc(model, "VB", "B_and_Lie", "*") model <- bvl_addArc(model, "Lie", "B_and_Lie", "*") model <- bvl_addArc(model, "VC", "C_and_Lie", "*") model <- bvl_addArc(model, "Lie", "C_and_Lie", "*") model <- bvl_addArc(model, "VT", "T_and_Lie", "*") model <- bvl_addArc(model, "Lie", "T_and_Lie", "*") model <- bvl_addArc(model, "B_and_Lie", "O", "slope") model <- bvl_addArc(model, "C_and_Lie", "O", "slope") model <- bvl_addArc(model, "T_and_Lie", "O", "slope") model <- bvl_addArc(model, "Lie", "O", "slope") model <- bvl_addNode(model, "Int1_or_Int2", "trans") model <- bvl_addArc(model, "Int1", "Int1_or_Int2", "+") model <- bvl_addArc(model, "Int2", "Int1_or_Int2", "+") model <- bvl_addArc(model, "Int1_or_Int2", "O", "varint") ## Plot network diagram to visualize the model bvl_bnPlot(model)
## create network model model <- bayesvl() ## add the observed data nodes model <- bvl_addNode(model, "O", "binom") model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "Viol", "binom") model <- bvl_addNode(model, "VB", "binom") model <- bvl_addNode(model, "VC", "binom") model <- bvl_addNode(model, "VT", "binom") model <- bvl_addNode(model, "Int1", "binom") model <- bvl_addNode(model, "Int2", "binom") ## add the tranform data nodes and arcs as part of the model model <- bvl_addNode(model, "B_and_Viol", "trans") model <- bvl_addNode(model, "C_and_Viol", "trans") model <- bvl_addNode(model, "T_and_Viol", "trans") model <- bvl_addArc(model, "VB", "B_and_Viol", "*") model <- bvl_addArc(model, "Viol", "B_and_Viol", "*") model <- bvl_addArc(model, "VC", "C_and_Viol", "*") model <- bvl_addArc(model, "Viol", "C_and_Viol", "*") model <- bvl_addArc(model, "VT", "T_and_Viol", "*") model <- bvl_addArc(model, "Viol", "T_and_Viol", "*") model <- bvl_addArc(model, "B_and_Viol", "O", "slope") model <- bvl_addArc(model, "C_and_Viol", "O", "slope") model <- bvl_addArc(model, "T_and_Viol", "O", "slope") model <- bvl_addArc(model, "Viol", "O", "slope") model <- bvl_addNode(model, "B_and_Lie", "trans") model <- bvl_addNode(model, "C_and_Lie", "trans") model <- bvl_addNode(model, "T_and_Lie", "trans") model <- bvl_addArc(model, "VB", "B_and_Lie", "*") model <- bvl_addArc(model, "Lie", "B_and_Lie", "*") model <- bvl_addArc(model, "VC", "C_and_Lie", "*") model <- bvl_addArc(model, "Lie", "C_and_Lie", "*") model <- bvl_addArc(model, "VT", "T_and_Lie", "*") model <- bvl_addArc(model, "Lie", "T_and_Lie", "*") model <- bvl_addArc(model, "B_and_Lie", "O", "slope") model <- bvl_addArc(model, "C_and_Lie", "O", "slope") model <- bvl_addArc(model, "T_and_Lie", "O", "slope") model <- bvl_addArc(model, "Lie", "O", "slope") model <- bvl_addNode(model, "Int1_or_Int2", "trans") model <- bvl_addArc(model, "Int1", "Int1_or_Int2", "+") model <- bvl_addArc(model, "Int2", "Int1_or_Int2", "+") model <- bvl_addArc(model, "Int1_or_Int2", "O", "varint") ## Plot network diagram to visualize the model bvl_bnPlot(model)
Build Stan models from directed acyclic graph of an object of class bayesvl
.
# build Stan models from directed acyclic graph. bvl_model2Stan(dag, ppc = "") # compile and simulate samples from the model. bvl_modelFit(dag, data, warmup = 1000, iter = 5000, chains = 2, ppc = "", ...) # summarize the stan priors used for the model. bvl_stanPriors(dag) # summarize the stan parameters used for the model. bvl_stanParams(dag) # summarize the generated formula at the node. bvl_formula(dag, nodeName, outcome = T, re = F)
# build Stan models from directed acyclic graph. bvl_model2Stan(dag, ppc = "") # compile and simulate samples from the model. bvl_modelFit(dag, data, warmup = 1000, iter = 5000, chains = 2, ppc = "", ...) # summarize the stan priors used for the model. bvl_stanPriors(dag) # summarize the stan parameters used for the model. bvl_stanParams(dag) # summarize the generated formula at the node. bvl_formula(dag, nodeName, outcome = T, re = F)
dag |
an object of class |
data |
a data frame or list containing the data |
warmup |
Optional: Number of warmup iterations. By default, half of iter |
iter |
Optional: Number of iterations of sampling. Default is 5000 |
chains |
Optional: Number of independent chains to sample from. Default is 2 |
ppc |
Optional: a character string contains posterior predictive check scripts |
... |
extra arguments from the generic method |
nodeName |
A character string contains the node name |
outcome |
Optional: Whether show out distribution |
re |
Optional: Whether run recursive for all up-level nodes |
bvl_model2Stan()
return character string of rstan code generated from the model.
bvl_modelFit()
return an object class bayesvl
which contains result with the following slots.
model |
Stan model code |
stanfit |
|
standata |
The data |
pars |
Parameter names monitored in samples |
formula |
Generated formula from the model |
bvl_stanPriors()
return character string of rstan priors generated from the model.
bvl_stanParams()
return character string of rstan parameters generated from the model.
La Viet-Phuong, Vuong Quan-Hoang
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
# Design the model in directed acyclic graph model <- bayesvl() model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") # Generate the Stan model's code model_string <- bvl_model2Stan(model) cat(model_string) # Show priors in generated Stan model bvl_stanPriors(model)
# Design the model in directed acyclic graph model <- bayesvl() model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") # Generate the Stan model's code model_string <- bvl_model2Stan(model) cat(model_string) # Show priors in generated Stan model bvl_stanPriors(model)
bayesvl
: object class of bayesvl modelThis object contains an object of class bayesvl
, return by bayesvl
.
call
:Original function call that produced the fit
nodes
:The list of nodes in model
arcs
:The list of arcs in model
pars
:List of parameters
stanfit
:Object of class stanfit
rawdata
:Observed data frame
standata
:Data used in sampling
posterior
:Coerce to a Data Frame of object of class stanfit
elapsed
:Elapsed time of MCMC simulation
show
signature(object = "bayesvl")
: print the default summary for the model.
summary
Displays the model
slot.
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
# Design the model in directed acyclic graph model <- bayesvl() # add observed data nodes to the model model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") # add path between nodes model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") summary(model)
# Design the model in directed acyclic graph model <- bayesvl() # add observed data nodes to the model model <- bvl_addNode(model, "Lie", "binom") model <- bvl_addNode(model, "B", "binom") model <- bvl_addNode(model, "C", "binom") model <- bvl_addNode(model, "T", "binom") # add path between nodes model <- bvl_addArc(model, "B", "Lie", "slope") model <- bvl_addArc(model, "C", "Lie", "slope") model <- bvl_addArc(model, "T", "Lie", "slope") summary(model)
Legends345.
data(Legends345)
data(Legends345)
O : Whether or not happy ending for main character
VB : Whether or not the main character behaves in accordance with the core values of Buddhism
VC : Whether or not the main character behaves in accordance with the core values of Confucianism
VT : Whether or not the main character behaves in accordance with the core values of Taoism
Lie : Whether or not the main character tells lie
Viol : Whether or not the main character commits acts of violence
Int1 : Whether there are interventions from the supernatural world
Int2 : Whether there are interventions from the human world
For documentation, case studies and worked examples, and other tutorial information visit the References section on our Github:
data(Legends345) data1 <- Legends345 head(data1)
data(Legends345) data1 <- Legends345 head(data1)