• Stars
    star
    393
  • Rank 100,395 (Top 3 %)
  • Language
    R
  • License
    GNU General Publi...
  • Created over 7 years ago
  • Updated 29 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

bayesplot R package for plotting Bayesian models

bayesplot

CRAN_Status_Badge Downloads R-CMD-check codecov

Overview

bayesplot is an R package providing an extensive library of plotting functions for use after fitting Bayesian models (typically with MCMC). The plots created by bayesplot are ggplot objects, which means that after a plot is created it can be further customized using various functions from the ggplot2 package.

Currently bayesplot offers a variety of plots of posterior draws, visual MCMC diagnostics, graphical posterior (or prior) predictive checking, and general plots of posterior (or prior) predictive distributions.

The idea behind bayesplot is not only to provide convenient functionality for users, but also a common set of functions that can be easily used by developers working on a variety of packages for Bayesian modeling, particularly (but not necessarily) those powered by RStan.

Getting started

If you are just getting started with bayesplot we recommend starting with the tutorial vignettes, the examples throughout the package documentation, and the paper Visualization in Bayesian workflow:

Resources

Installation

  • Install from CRAN:
install.packages("bayesplot")
  • Install latest development version from GitHub (requires devtools package):
if (!require("devtools")) {
  install.packages("devtools")
}
devtools::install_github("stan-dev/bayesplot", dependencies = TRUE, build_vignettes = FALSE)

This installation won't include the vignettes (they take some time to build), but all of the vignettes are available online at mc-stan.org/bayesplot/articles.

Examples

Some quick examples using MCMC draws obtained from the rstanarm and rstan packages.

library("bayesplot")
library("rstanarm")
library("ggplot2")

fit <- stan_glm(mpg ~ ., data = mtcars)
posterior <- as.matrix(fit)

plot_title <- ggtitle("Posterior distributions",
                      "with medians and 80% intervals")
mcmc_areas(posterior, 
           pars = c("cyl", "drat", "am", "wt"), 
           prob = 0.8) + plot_title

color_scheme_set("red")
ppc_dens_overlay(y = fit$y, 
                 yrep = posterior_predict(fit, draws = 50))

# also works nicely with piping
library("dplyr")
color_scheme_set("brightblue")
fit %>% 
  posterior_predict(draws = 500) %>%
  ppc_stat_grouped(y = mtcars$mpg, 
                   group = mtcars$carb, 
                   stat = "median")

# with rstan demo model
library("rstan")
fit2 <- stan_demo("eight_schools", warmup = 300, iter = 700)
posterior2 <- extract(fit2, inc_warmup = TRUE, permuted = FALSE)

color_scheme_set("mix-blue-pink")
p <- mcmc_trace(posterior2,  pars = c("mu", "tau"), n_warmup = 300,
                facet_args = list(nrow = 2, labeller = label_parsed))
p + facet_text(size = 15)

# scatter plot also showing divergences
color_scheme_set("darkgray")
mcmc_scatter(
  as.matrix(fit2),
  pars = c("tau", "theta[1]"), 
  np = nuts_params(fit2), 
  np_style = scatter_style_np(div_color = "green", div_alpha = 0.8)
)

color_scheme_set("red")
np <- nuts_params(fit2)
mcmc_nuts_energy(np) + ggtitle("NUTS Energy Diagnostic")

# another example with rstanarm
color_scheme_set("purple")

fit <- stan_glmer(mpg ~ wt + (1|cyl), data = mtcars)
ppc_intervals(
  y = mtcars$mpg,
  yrep = posterior_predict(fit),
  x = mtcars$wt,
  prob = 0.5
) +
  labs(
    x = "Weight (1000 lbs)",
    y = "MPG",
    title = "50% posterior predictive intervals \nvs observed miles per gallon",
    subtitle = "by vehicle weight"
  ) +
  panel_bg(fill = "gray95", color = NA) +
  grid_lines(color = "white")

More Repositories

1

stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
C++
2,469
star
2

rstan

RStan, the R interface to Stan
R
971
star
3

pystan2

PyStan, the Python interface to Stan
Python
916
star
4

example-models

Example models for Stan
HTML
738
star
5

math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
C++
679
star
6

rstanarm

rstanarm R package for Bayesian applied regression modeling
R
358
star
7

pystan

PyStan, a Python interface to Stan, a platform for statistical modeling. Documentation: https://pystan.readthedocs.io
Python
270
star
8

stancon_talks

Materials from Stan conferences
HTML
248
star
9

shinystan

shinystan R package and ShinyStan GUI
R
190
star
10

cmdstan

CmdStan, the command line interface to Stan
C++
182
star
11

posteriordb

Database with posteriors of interest for Bayesian inference
Stan
154
star
12

posterior

The posterior R package
R
151
star
13

loo

loo R package for approximate leave-one-out cross-validation (LOO-CV) and Pareto smoothed importance sampling (PSIS)
R
135
star
14

cmdstanpy

CmdStanPy is a lightweight interface to Stan for Python users which provides the necessary objects and functions to compile a Stan program and fit the model to data using CmdStan.
Python
132
star
15

stanc3

The Stan transpiler (from Stan to C++ and beyond).
OCaml
121
star
16

cmdstanr

CmdStanR: the R interface to CmdStan
R
119
star
17

projpred

Projection predictive variable selection
R
105
star
18

stan-mode

Emacs mode for Stan.
Emacs Lisp
70
star
19

rstantools

Tools for Developing R Packages Interfacing with Stan
R
44
star
20

httpstan

HTTP interface to Stan, a package for Bayesian inference.
Python
35
star
21

docs

Documentation for the Stan language and CmdStan
TeX
33
star
22

design-docs

32
star
23

MathematicaStan

A Mathematica package to interact with CmdStan
Mathematica
28
star
24

connect22-space-time

StanCon Connect 2022 space and time
HTML
22
star
25

stancon2023

Materials for StanCon 2023
Jupyter Notebook
21
star
26

statastan

Stata interface for Stan.
Stata
20
star
27

nomad

Fast autodiff.
C++
18
star
28

gmo

Inference on marginal distributions using gradient-based optimization
R
13
star
29

posteriordb-python

Python
11
star
30

stat_comp_benchmarks

Benchmark Models for Evaluating Algorithm Accuracy
R
9
star
31

posteriordb-r

R
8
star
32

pystan-wheels

Automated builds of OSX and manylinux wheels for pystan
Shell
8
star
33

performance-tests-cmdstan

Performance testing tools for use with CmdStan
Python
8
star
34

perf-math

C++
7
star
35

logos

Stan logos
HTML
5
star
36

visual-diagnostics

Visual diagnostics for HMC using gnuplot.
Shell
4
star
37

r-packages

Repository for distributing (some) stan-dev R packages
4
star
38

httpstan-wheels

Wheels for httpstan
Shell
4
star
39

sgb

Stan Governing Body issue tracker and meeting notes
4
star
40

atom-language-stan

JavaScript
3
star
41

stan2tfp

Stan2TFP is a work-in-progress alternative backend for Stanc3 which targets TensorFlow Probability
OCaml
2
star
42

monostan

C++
2
star
43

.github

Stan organization READMEs and information
1
star
44

jenkins-shared-libraries

Libraries for our Jenkinsfiles
Groovy
1
star
45

stan-discourse-theme-component

HTML
1
star
46

propaganda

Sell sheets and the like
TeX
1
star
47

ci-scripts

Formerly syclik's stan-scripts repo. Contains scripts used by Jenkins as well as the release scripts and performance scripts.
Shell
1
star