beta release of new R package "bayesplot"

584 views
Skip to first unread message

Jonah Gabry

unread,
Oct 31, 2016, 11:23:34 AM10/31/16
to Stan users mailing list
Dear Stan users,

The purpose of this post is to announce the beta release of a new R package
called bayesplot and to ask you for your feedback before we release the
package on CRAN.

The bayesplot R package is a new ggplot2-based plotting package for use after
fitting models with Stan (or other packages). The functions in bayesplot are
separated into modules. Currently there is the PPC module for graphical
posterior predictive checking and the MCMC module for plotting posterior draws
and various diagnostics. Going forward we plan on adding other modules (e.g.,
plots for forecasting) and expanding the functionality of the existing PPC and
MCMC modules. The package documentation and (drafts of) the package 
vignettes provide more details and many examples.

The beta release of bayesplot can be installed by following the instructions in
the README file at the stan-dev/bayesplot GitHub repository. If you find bugs,
want to request a feature, or have general comments or questions about the
package, we encourage you to open an issue on GitHub and/or respond to this
post. Please don't hesitate to let us know what you like, what don't like, what
you'd like to see added to the package, etc. Your feedback is greatly
appreciated!

After the initial CRAN release later this fall we will be using the new package to unify 
the underlying plotting functionality in our other R packages (particularly rstan,
rstanarm, and parts of shinystan). But bayesplot itself does not depend on any
of our other packages and is compatible with output from any package for fitting
Bayesian models. Our goal is not only to provide functionality for our own
packages, but also to offer a library of plotting functions that can be used by
developers of other R packages for Bayesian modeling (particularly, but not
exclusively, those interfacing with Stan), as well as a simple enough interface
that individual users can use the bayesplot functions interactively (as opposed
to only via wrappers provided by rstan, rstanarm, or other packages).

- The Stan Development Team

Bob Carpenter

unread,
Oct 31, 2016, 12:16:18 PM10/31/16
to stan-...@googlegroups.com
That looks great. I really like the increasing modularity
of the Stan moving pieces---it's the only way forward for
both development of new features and maintenance of existing
ones.

Next up, we'll be breaking the core stan-dev/stan repo into
modules for the language translator, the algorithms, and
the interface services. And splitting the manual into a
language reference manual and user's guide.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

krishna mohan

unread,
Oct 31, 2016, 3:09:44 PM10/31/16
to Stan users mailing list
Thanks for the update and the new module.

In the ReadMe page of "bayesplot" install instructions, could you add a note to reiterate that users should have pandoc and shinystan installed in addition to ggplot2 before installing bayesplot. I might have missed this in the documentation.but found out through the error messages during the install.

It looks good and it is working well. 

Krishna


Jonah Sol Gabry

unread,
Oct 31, 2016, 4:35:47 PM10/31/16
to stan-...@googlegroups.com
Hi Krishna, thanks for the heads up. I added `dependencies=TRUE` to the `install_github` command in the README, which should force shinystan to be installed. That said, I'm not sure why it wouldn't work without shinystan since it's not a dependency (only listed in Suggests). I'll have to look into that. 

As for pandoc, I assume that has something to do with the vignettes. I've added a note about that too. 

Thanks again, 

Jonah

--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/aGw4IBVkdcA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

krishna mohan

unread,
Oct 31, 2016, 5:01:14 PM10/31/16
to Stan users mailing list
I was not sure about shinystan; The pandoc error did come up with vignettes. Thanks for all of the hard work and the efforts of the Stan team

Krishna


krishna mohan

unread,
Nov 1, 2016, 8:49:46 AM11/1/16
to Stan users mailing list
Hi Jonah

Have you tested the bayesplot package with Windows 10. I work with a W7 and a W10 machine. My R environment is an exact replica on both of my machines (version, locations and tools). After successfully installing it on W7 machine, when I replicated the steps for W10 machine the installation does not work for pandoc or bayesplot. Gives me the message "exited with 1". I have full admin access when installing.

Krishna

Jonah Sol Gabry

unread,
Nov 1, 2016, 12:39:51 PM11/1/16
to stan-...@googlegroups.com
Hey Krishna, 

I haven't personally tested it with Windows 10 but I do know that's it's been installed and used successfully on Windows 10. Would you mind opening an issue at the GitHub repository and sharing the full error message(s) there?

Thanks a lot,

Jonah
--

krishna mohan

unread,
Nov 1, 2016, 10:16:04 PM11/1/16
to Stan users mailing list
Jonah

This is the error I get

> devtools::install_github("stan-dev/bayesplot", dependencies = TRUE, build_vignettes = TRUE)
Downloading GitHub repo stan-dev/bayesplot@master
Installing bayesplot
"C:/PROGRA~1/R/R-33~1.1/bin/x64/R" --no-site-file --no-environ --no-save  \
  --no-restore --quiet CMD build  \
  "C:\Users\AppData\Local\Temp\RtmpsLXtvm\devtools170452a74e8\stan-dev-bayesplot-f971edc"  \
  --no-resave-data --no-manual 

* checking for file 'C:\Users\AppData\Local\Temp\RtmpsLXtvm\devtools170452a74e8\stan-dev-bayesplot-f971edc/DESCRIPTION' ... OK
* preparing 'bayesplot':
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ...Warning: running command '"C:/PROGRA~1/R/R-33~1.1/bin/x64/Rscript" --vanilla --default-packages= -e "tools::buildVignettes(dir = '.', tangle = TRUE)"' had status 1
 ERROR
Error: processing vignette 'MCMC-diagnostics.Rmd' failed with diagnostics:
there is no package called 'stringi'
Execution halted
Error: Command failed (1)

krishna mohan

unread,
Nov 1, 2016, 10:31:33 PM11/1/16
to Stan users mailing list
Jonah

I resolved it in this manner:

install.packages("stringi")
library("stringi")
devtools::install_github("stan-dev/bayesplot", dependencies = TRUE, build_vignettes = TRUE)

what is strange is that this error did not crop in W7. And the "stringi" package is not installed even though dependencies = TRUE . I had to install it manually.

Krishna

Jonah Sol Gabry

unread,
Nov 1, 2016, 11:47:10 PM11/1/16
to stan-...@googlegroups.com
Thanks for following up. That stringi issue is indeed strange. Glad it's working now.

Jonah

On Tuesday, November 1, 2016, krishna mohan <krish4...@gmail.com> wrote:

Sebastian Weber

unread,
Nov 2, 2016, 5:04:08 AM11/2/16
to Stan users mailing list
Hi!

As far as I can tell from the source there is no way to customize line strength and alpha values, etc.. Was this left out on purpose for a first release? I know that this is tedious, but I also know that my users wish to set these to their taste (I consider to use bayesplot in my own package).

Should I file an issue for this?

Best,

Sebastian

Lei Zhang

unread,
Nov 2, 2016, 8:59:50 AM11/2/16
to Stan users mailing list
Many thanks for developing this plotting package. 

I wonder whether there is a standard (or at least recommended) way to plot PPC for discrete outputs. For instance, the target output (1, 2, or 3) follows a categorical distribution. How to visualize the PPC properly after the Stan draws? 

Thanks.


On Monday, October 31, 2016 at 4:23:34 PM UTC+1, Jonah Gabry wrote:

Jonah Sol Gabry

unread,
Nov 2, 2016, 9:33:42 AM11/2/16
to stan-...@googlegroups.com
On Wednesday, November 2, 2016, Sebastian Weber <sdw....@gmail.com> wrote:
Hi!

As far as I can tell from the source there is no way to customize line strength and alpha values, etc.. Was this left out on purpose for a first release? I know that this is tedious, but I also know that my users wish to set these to their taste (I consider to use bayesplot in my own package).

Should I file an issue for this?

Best,

Sebastian

Some functions do already have size and alpha arguments but for the prerelease I tried to keep those to ones were it was essential to be able to change those things to make cluttered plots more readable. Additional flexibility in that regard can definitely be added going forward. Opening an issue would be helpful, thanks!

Jonah

Jonah Sol Gabry

unread,
Nov 2, 2016, 9:45:07 AM11/2/16
to stan-...@googlegroups.com
Hi Lei,

On Wednesday, November 2, 2016, Lei Zhang <bnuzhan...@gmail.com> wrote:
Many thanks for developing this plotting package. 

Hope it's useful! 

I wonder whether there is a standard (or at least recommended) way to plot PPC for discrete outputs. For instance, the target output (1, 2, or 3) follows a categorical distribution. How to visualize the PPC properly after the Stan draws?  

For binomial and poison models a lot of the existing plots should be useful. For categorical/multinomial I think it's a bit trickier but going forward we should add some plots specifically for that. One thing you can already do for those models is look at the proportion in a category or set of categories, comparing observed to replicated, e.g. using the functions prefixed with ppc_stat. In the PPC vignette there's an example of looking at the proportion of predicted zeros after fitting a poisson model, but that can be adapted to proportion in category 1, etc. 

Jonah

Bill Harris

unread,
Nov 2, 2016, 10:27:44 PM11/2/16
to stan-...@googlegroups.com
Jonah,

Thanks for this.  I made good use of it yesterday and today.

Bill

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Jonah Sol Gabry

unread,
Nov 3, 2016, 12:26:12 AM11/3/16
to stan-...@googlegroups.com
Thanks Bill, that's great to hear. Glad it's being put to good use already!

Jonah

If you run into any mysterious behavior or have any suggestions for improvement at any point 
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/aGw4IBVkdcA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+unsubscribe@googlegroups.com.

Jonah Sol Gabry

unread,
Nov 3, 2016, 12:28:14 AM11/3/16
to stan-...@googlegroups.com
got cut off there. Meant to say that there's still time before the first CRAN release if you end up noticing any bugs. After the release I'll start working on additional functionality. 


On Thursday, November 3, 2016, Jonah Sol Gabry <jga...@gmail.com> wrote:
Thanks Bill, that's great to hear. Glad it's being put to good use already!

Jonah

If you run into any mysterious behavior or have any suggestions for improvement at any point 

Bill Harris

unread,
Nov 3, 2016, 1:26:09 AM11/3/16
to stan-...@googlegroups.com
Jonah,

No doubt not a bug (except in my mind, perhaps), but I liked the stat_2d type of ppc: it was good to see that the mean and sd were firmly in the middle of the round yrep cloud of a linear model out of brms.  Then I tried it on a lognormal model.  Even though the fit between yrep and y was closer when looked at via the ecdf, the (mean, sd) point was well outside the yrep point cloud in that model.  Is it generally true that the mean and sd fall outside the yrep typical set when there's a link function between the linear predictor and y?

Bill

Jonah Sol Gabry

unread,
Nov 3, 2016, 2:56:07 AM11/3/16
to stan-...@googlegroups.com
On Thu, Nov 3, 2016 at 1:26 AM, Bill Harris <bill_...@facilitatedsystems.com> wrote:
Is it generally true that the mean and sd fall outside the yrep typical set when there's a link function between the linear predictor and y?

Good question, but no I don't think that should necessarily be true in general. For instance, the classic R glm example with log link is a counterexample:

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

counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
fit <- stan_glm(counts ~ outcome + treatment, family = poisson(link="log"))

yrep <- posterior_predict(fit)
ppc_stat_2d(counts, yrep, stat = c("mean", sd"))

wsha...@snopud.com

unread,
Nov 4, 2016, 5:33:57 PM11/4/16
to Stan users mailing list
Jonah,

It helps to use a better model. :-)

Here's a suggestion for a feature.  In using mcmc_areas, I would like to put a name on the x axis, but adding a title to scale_x_continuous() doesn't seem to have any effect, even though adding breaks and labels does work (with a complaint that I'm redefining the x axis).  I haven't tested to see if the same issue exists on other bayesplot functions.

Bill

Jonah Sol Gabry

unread,
Nov 4, 2016, 5:47:37 PM11/4/16
to stan-...@googlegroups.com
Hey Bill, does the x-axis title appear if you add "+ xaxis_title()"? For some plots it defaults to no axis title for one or both of the axes but you should be able to override that using the provided convenience functions like xaxis_title.

Jonah

wsha...@snopud.com

unread,
Nov 4, 2016, 6:45:54 PM11/4/16
to stan-...@googlegroups.com
Jonah,

From the documentation (?xaxis_title), it appears I want labs(), not xaxis_title().  Unfortunately, that doesn't appear to work.

posterior[,12:17] %>% mcmc_areas(prob = 0.5) produces a basic graph as I want.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(x = 'test') produces the same graph--no word "test" in sight.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(list(x = 'test')) does the same thing, although this seems correct.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(list(title = 'Title', x = 'test')) produces the same thing plus "Title" right above the y axis but no "test" anywhere.

Am I missing something?

Bill

Jonah Sol Gabry

unread,
Nov 4, 2016, 9:53:27 PM11/4/16
to stan-...@googlegroups.com
Yeah try this:

posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(x = 'test') + xaxis_title()

You need both labs and xaxis_title. labs specifies the title text and xaxis_title() can be used to toggle it on and off and format it (color, size, etc.). For mcmc_areas it's off by default so xaxis_title() should turn it back on if it's working properly. I guess I need to document this better! 

On Friday, November 4, 2016, <wsha...@snopud.com> wrote:
Jonah,

From the documentation (?xaxis_title), it appears I want labs(), not xaxis_title().  Unfortunately, that doesn't appear to work.

posterior[,12:17] %>% mcmc_areas(prob = 0.5) produces a basic graph as I want.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(x = 'test') produces the same graph--no word "test" in sight.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(list(x = 'test')) does the same thing, although this seems correct.
posterior[,12:17] %>% mcmc_areas(prob = 0.5) + labs(list(title = 'Title', x = 'test')) produces the same thing plus "Title" right above the y axis.

wsha...@snopud.com

unread,
Nov 15, 2016, 6:10:12 PM11/15/16
to Stan users mailing list
Thanks, Jonah.  That seems to work; I just got wrapped up in other things and forgot to reply.

Bill

Jonah Gabry

unread,
Nov 20, 2016, 1:27:29 PM11/20/16
to stan-...@googlegroups.com
Hey Bill, I already submitted bayesplot to CRAN but for the next release I'll make it so you can use labs to set the axis titles without having to manually turn them back on with xaxis_title. That seems like an unnecessary step that I should have originally avoided. 

Jonah

Stephen Martin

unread,
Nov 20, 2016, 9:44:13 PM11/20/16
to Stan users mailing list
I know bayesplot aims to generalize to any MCMC output, but given that it has originated (presumably) from rstan devs, why can you not just feed it a stan object?
Seems like the S4/S3 system would make this fairly simple; set a method for each user-facing plot function that expects a stanfit, extract the chains and call the mcmc method afterward.

Jonah Sol Gabry

unread,
Nov 21, 2016, 2:34:09 AM11/21/16
to stan-...@googlegroups.com
Hi Stephen, 

We're actually intentionally trying to avoid having anything like that in bayesplot to keep the code more modular and easier to maintain. But what you're asking about will definitely exist soon in the rstan package. bayesplot is intended to not have as many dependencies as rstan and to be usable by rstan, rstanarm, and other R packages that return different types of objects. The idea is that those packages can offer wrappers for bayesplot that make it work with the specific objects from those packages. This is what the plot method in rstanarm now does for stanreg objects in the new version of rstanarm and what the plot method in rstan will soon do for stanfit objects. So, for example, to get bayesplot's mcmc_violin plot with a stanfit object you'll be able to do something like plot(stanfit, "mcmc_violin") or just plot(stanfit, "violin") and it will get what bayesplot needs from the stanfit object and call bayesplot::mcmc_violin.

Jonah

wsha...@snopud.com

unread,
Nov 21, 2016, 10:59:04 AM11/21/16
to Stan users mailing list
Thanks, Jonah!  Hey, this little issue did teach me how one turns axis titles on and off!

Bill


On Sunday, November 20, 2016 at 10:27:29 AM UTC-8, Jonah Gabry wrote:

Stefano

unread,
Nov 27, 2016, 10:00:25 PM11/27/16
to Stan users mailing list
That sounds great.

However I could not make the example work

# 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)


Error in eval(expr, envir, enclos) : object 'Value' not found


> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS release 6.4 (Final)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reshape_0.8.6        bayesplot_1.0.0      rstan_2.12.1         StanHeaders_2.12.0-1 ggplot2_2.2.0        devtools_1.12.0     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      magrittr_1.5     munsell_0.4.3    colorspace_1.3-1 R6_2.2.0         stringr_1.1.0    httr_1.2.1       plyr_1.8.4      
 [9] tools_3.3.0      grid_3.3.0       gtable_0.2.0     git2r_0.15.0     withr_1.0.2      lazyeval_0.2.0   digest_0.6.10    assertthat_0.1  
[17] tibble_1.2       gridExtra_2.2.1  reshape2_1.4.2   codetools_0.2-15 curl_2.2         memoise_1.0.0    inline_0.3.14    labeling_0.3    
[25] stringi_1.1.2    scales_0.4.1     stats4_3.3.0  

Cheers.

Jonah Gabry

unread,
Nov 28, 2016, 3:19:53 PM11/28/16
to Stan users mailing list
Hi Stefano,

I'm not sure why that isn't working for you and I'm not able to reproduce the error. Is it only this example or have you found others that don't work for you? Would you mind opening an issue at https://github.com/stan-dev/bayesplot/issues?

Thanks, 

Jonah

Stephen Martin

unread,
Dec 9, 2016, 11:30:02 AM12/9/16
to Stan users mailing list
Is there a way to simply use the colors that, e.g., stan_trace(...) uses?
The color schemes don't differentiate between chains enough, in my opinion.
The light purple, dark purple, dark orange, light orange scheme works well for that purpose.
I've tried doing some mixing ('mix-orange-green') or whatever, but I can't find a combination that works as well as stan_trace's color scheme.

Jonah Gabry

unread,
Dec 9, 2016, 2:26:21 PM12/9/16
to Stan users mailing list
Hi Stephen, 

For now I think this should work:

library("bayesplot")
trace_colors <- c("#E66101", "#998EC3", "#542788", "#F1A340", "#D8DAEB", "#FEE0B6")
color_scheme_set(trace_colors)
x <- example_mcmc_draws()
mcmc_trace(x)

Maybe it's worth making this one of the color schemes that can be set by name instead of having to specify the exact colors each time. 

Jonah

Daniel Emaasit

unread,
Dec 11, 2016, 5:14:25 PM12/11/16
to Stan users mailing list
I get the error below when I try to plot posterior draws from an HMM.
```{r}
posterior_array <- as.array(my_fit)
dimnames(posterior_array)
mcmc_areas(posterior_array, pars = c("mu[1]", "mu[2]"))
```

$iterations
NULL

$chains
 [1] "chain:1"  "chain:2"  "chain:3"  "chain:4"  "chain:5"  "chain:6"  "chain:7" 
 [8] "chain:8"  "chain:9"  "chain:10"

$parameters
 [1] "mu[1]"      "mu[2]"      "mu[3]"      "mu[4]"      "mu[5]"      "mu[6]"     
 [7] "mu[7]"      "mu[8]"      "mu[9]"      "sigma[1]"   "sigma[2]"   "sigma[3]"


Error in theme_get()[newitem_names] : object of type 'closure' is not subsettable

I tried extracting the stan fit object as a dataframe but still get error. Any idea what's going on?

-- Daniel

Daniel Emaasit

unread,
Dec 11, 2016, 10:08:53 PM12/11/16
to Stan users mailing list
I restarted my R session and the plotting worked just fine.
Mmmh! not sure what happened.

Jonah Sol Gabry

unread,
Dec 11, 2016, 11:55:51 PM12/11/16
to stan-...@googlegroups.com
Hi Daniel, that looks like an error in the ggplot2 internals. Not sure why it wasn't working but glad it's ok now. Definitely let us know if it starts happening again. 

Jonah

--


You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.


To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/aGw4IBVkdcA/unsubscribe.


To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages