stan: Build Stan Models from Directed Acyclic Graphs

bayesvl-stan-utilitiesR Documentation

Build Stan Models from Directed Acyclic Graphs

Description

Functions to generate Stan code and run simulations using a model object of class bayesvl, which represents a Bayesian directed acyclic graph (DAG).

Usage

bvl_model2Stan(dag, ppc = "")

bvl_modelFit(dag, data, warmup = 1000, iter = 5000, chains = 2, ppc = "", ...)

bvl_stanPriors(dag)

bvl_stanParams(dag)

bvl_formula (dag, nodeName, outcome = T, re = F)

bvl_stanLikelihood (dag)

bvl_stanLoo (dag, ...)

bvl_stanWAIC (dag, ...)

Arguments

dag

An object of class bayesvl representing the model DAG.

data

A data frame or list containing the observed data for model fitting.

warmup

Number of warmup iterations; defaults to half of iter.

iter

Total number of iterations for sampling. Default is 5000.

chains

Number of MCMC chains to run. Default is 2.

ppc

Optional: a character string containing Stan code for posterior predictive checks.

...

Additional arguments passed to underlying functions.

nodeName

The name of the node to generate formula for.

outcome

Logical. Whether to include outcome distribution. Default is TRUE.

re

Logical. Whether to recursively trace all upstream nodes. Default is FALSE.

Value

The following outputs are returned depending on the function used:

  • bvl_model2Stan: Returns a character string containing the generated Stan model code.

  • bvl_modelFit: Returns an object of class bayesvl with the following slots:

    • model: The Stan model code.

    • stanfit: A stanfit object returned by rstan.

    • standata: The data list used in sampling.

    • pars: A list of parameter names being monitored.

    • formula: The formula representation of the model.

  • bvl_stanPriors: Returns a character string of the prior distributions used in the model.

  • bvl_stanParams: Returns a character string of parameter block content for Stan.

  • bvl_formula: Returns the formula associated with the specified node.

Author(s)

La Viet-Phuong, Vuong Quan-Hoang

References

For documentation, case studies, worked examples, and other tutorial materials, see:

Examples

# Design the model using a 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 Stan model code
model_string <- bvl_model2Stan(model)
cat(model_string)

# Display priors in generated Stan model
bvl_stanPriors(model)

bayesvl documentation built on June 8, 2025, 10:38 a.m.

Related to stan in bayesvl...