Code Monkey home page Code Monkey logo

bayesplot's Introduction

Stan Logo

bayesplot

Travis-CI Build Status codecov CRAN_Status_Badge

An R package providing a library of plotting functions for use after fitting Bayesian models (typically with MCMC). The idea 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.

The plots created by bayesplot are ggplot objects, which means that after a plot is created it can be further customized using the various functions for modifying ggplot objects provided by the ggplot2 package.

Installation

bayesplot is not yet on CRAN (coming soon) but can be installed from GitHub using the devtools package. You will also need the preview version of the upcoming ggplot2 update.

if (!require("devtools"))
  install.packages("devtools")

devtools::install_github("hadley/ggplot2")
devtools::install_github("stan-dev/bayesplot", dependencies = TRUE, build_vignettes = TRUE)

If you are not using RStudio and you get an error related to "pandoc" you will either need to install pandoc or remove the argument build_vignettes=TRUE to avoid building the package vignettes.

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

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

```r # 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")
<img src=https://github.com/jgabry/bayesplot/blob/master/images/ppc_stat_grouped-rstanarm.png width=50% />
```r
# 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)

```r color_scheme_set("red") np <- nuts_params(fit2) mcmc_nuts_energy(np, merge_chains = FALSE) + ggtitle("NUTS Energy Diagnostic") ```

```r # 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")

<img src=https://github.com/jgabry/bayesplot/blob/master/images/ppc_intervals-rstanarm.png width=55% />

bayesplot's People

Contributors

jgabry avatar

Watchers

 avatar  avatar  avatar

Forkers

nemochina2008

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.