You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reusable ggplot2-based plotting functions for fitted Stan models.
stanviz standardises the figures you produce when working with
Bayesian models in Stan: prior and posterior predictive checks, MCMC
diagnostics, coefficient inference, conditional and counterfactual
effects, mediation decomposition, hierarchical / multilevel results,
prior sensitivity, and PSIS-LOO model comparison. Every function takes a
fitted Stan model (CmdStanR first-class; rstan and draws_df also
accepted) and returns a ggplot object that’s ready to ship into a
report.
The design assumes the typical Stan workflow: declare the quantities you
care about (y_rep, log_lik, pit, per-arm predictions, mediation
scalars, group-level effects) in generated quantities, then point the
plotting functions at them by name. The plots layer comes ready; the
modelling layer stays in your hands.
Robust draws extractor (CmdStanR / rstan / draws).
draws_matrix_of()
Build an [n_draws, N] matrix from a vector parameter.
gather_indexed()
Tidy an indexed parameter family to long format.
geom_null_line()
Dashed reference line at a given x.
What stanviz expects in your Stan code
To get the most out of the library, have your generated quantities
emit these standard names. Functions accept the variable name as an
argument, so you can deviate — but the defaults assume these:
Quantity
Used by
vector[N] y_rep
Every PPC plot.
vector[N] log_lik
compute_loo() and all LOO plots.
vector[N] pit
plot_ppc_pit().
real lprior (alongside log_lik)
priorsense power-scaling functions.
Per-arm vectors, e.g. mu_treated, mu_control
extract_ate_from_draws() and counterfactual plots.
Hierarchical: a vector of group effects (alpha_j), the grand mean (mu), the group SD (tau)
Hierarchical plots.
Design conventions
Every plot returns a ggplot. You can keep customising
(+ ggplot2::labs(...)) or composing with patchwork (p1 / p2).
One palette, one theme. All figures use bayes_palette() and
theme_bayes() so multi-panel reports look coherent.
CmdStanR first-class. All non-marginaleffects functions work with
raw CmdStanR fits; nothing needs a brms wrapper.
You provide the variable names. No magic auto-detection; the
function takes what to plot as an explicit argument. Trades a few
keystrokes for total transparency about what’s being shown.
Development
# After editing R/ source:devtools::document() # regenerate help and NAMESPACEdevtools::load_all() # reload without installingdevtools::check() # R CMD checkdevtools::install() # install into your library