How To Explore And Summarize A Mcmc Draw
Plotting MCMC draws using the bayesplot package
2022-03-09
- Introduction
- Setup
- Example model
- Posterior uncertainty intervals
- Univariate marginal posterior distributions
- Bivariate plots
- Trace plots
- References
Introduction
This vignette focuses on plotting parameter estimates from MCMC draws. MCMC diagnostic plots are covered in the divide vignette Visual MCMC diagnostics, and graphical posterior predictive model checking is covered in the vignette Graphical posterior predictive checks.
Setup
In addition to bayesplot we'll load the following packages:
- ggplot2, in case nosotros want to customize the ggplot objects created by bayesplot
- rstanarm, for plumbing equipment the example models used throughout the vignette
library("bayesplot") library("ggplot2") library("rstanarm")
Example model
The bayesplot package provides diverse plotting functions for visualizing Markov concatenation Monte Carlo (MCMC) draws from the posterior distribution of the parameters of a Bayesian model.
In this vignette nosotros'll use draws obtained using the stan_glm
function in the rstanarm parcel (Gabry and Goodrich, 2017), but MCMC draws from using any packet can be used with the functions in the bayesplot packet. Encounter, for example, brms, which, similar rstanarm, calls the rstan package internally to use Stan's MCMC sampler.
head(mtcars) # see help("mtcars")
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 six 160 110 iii.90 2.620 xvi.46 0 i 4 four Mazda RX4 Wag 21.0 vi 160 110 3.90 2.875 17.02 0 1 4 four Datsun 710 22.8 4 108 93 3.85 ii.320 18.61 i 1 4 i Hornet 4 Drive 21.4 6 258 110 three.08 3.215 nineteen.44 i 0 3 one Hornet Sportabout xviii.7 eight 360 175 3.15 iii.440 17.02 0 0 3 two Valiant eighteen.1 6 225 105 ii.76 3.460 20.22 ane 0 three 1
# linear regression model using stan_glm # using '~ .' to include all variables fit <- stan_glm(mpg ~ ., information = mtcars, seed = 1111) print(fit)
stan_glm family: gaussian [identity] formula: mpg ~ . observations: 32 predictors: 11 ------ Median MAD_SD (Intercept) 12.0 19.3 cyl -0.1 i.1 disp 0.0 0.0 hp 0.0 0.0 drat 0.8 ane.6 wt -3.7 one.nine qsec 0.8 0.8 vs 0.iii 2.1 am two.half-dozen 2.1 gear 0.vi one.5 carb -0.2 0.ix Auxiliary parameter(s): Median MAD_SD sigma two.seven 0.four ------ * For assist interpreting the printed output meet ?print.stanreg * For info on the priors used see ?prior_summary.stanreg
To use the posterior draws with the functions in the bayesplot package nosotros'll extract them from the fitted model object:
posterior <- every bit.array(fit) dim(posterior)
[1] m iv 12
$iterations NULL $chains [1] "concatenation:1" "concatenation:ii" "chain:three" "concatenation:4" $parameters [one] "(Intercept)" "cyl" "disp" "hp" "drat" [half dozen] "wt" "qsec" "vs" "am" "gear" [11] "carb" "sigma"
We've used every bit.array
above (as opposed to as.matrix
) because it keeps the Markov chains divide (stan_glm
runs four bondage by default). Most of the plots don't actually need the chains to be divide, only for a few of the plots we make in this vignette nosotros'll want to show the chains individually.
Posterior dubiety intervals
For models fit using MCMC we can compute posterior uncertainty intervals (sometimes chosen "apparent intervals") in various ways. bayesplot currently provides plots of key intervals based on quantiles, although boosted options may be provided in future releases (e.g., HDIs, which tin be useful in item cases).
Documentation:
-
help("MCMC-intervals")
- mc-stan.org/bayesplot/reference/MCMC-intervals
mcmc_intervals, mcmc_areas
Central posterior uncertainty intervals can be plotted using the mcmc_intervals
function.
color_scheme_set("ruby-red") mcmc_intervals(posterior, pars = c("cyl", "drat", "am", "sigma"))
The default is to show 50% intervals (the thick segments) and 90% intervals (the thinner outer lines). These defaults tin can be changed using the prob
and prob_outer
arguments, respectively. The points in the above plot are posterior medians. The point_est
argument tin be used to select posterior means instead or to omit the point estimates.
To show the uncertainty intervals as shaded areas under the estimated posterior density curves nosotros tin use the mcmc_areas
part.
mcmc_areas( posterior, pars = c("cyl", "drat", "am", "sigma"), prob = 0.8, # 80% intervals prob_outer = 0.99, # 99% point_est = "mean" )
Univariate marginal posterior distributions
bayesplot provides functions for looking at histograms or kernel density estimates of marginal posterior distributions, either with all Markov bondage combined or with the bondage split up.
Documentation:
-
help("MCMC-distributions")
- mc-stan.org/bayesplot/reference/MCMC-distributions
mcmc_hist
The mcmc_hist
office plots marginal posterior distributions (combining all chains):
color_scheme_set("green") mcmc_hist(posterior, pars = c("wt", "sigma"))
If we want to plot log(sigma)
rather than sigma
nosotros can either transform the draws in advance or utilize the transformations
statement.
color_scheme_set("blueish") mcmc_hist(posterior, pars = c("wt", "sigma"), transformations = list("sigma" = "log"))
Most of the other functions for plotting MCMC draws as well have a transformations
argument.
mcmc_hist_by_chain
To view separate histograms of each of the four Markov bondage we can employ mcmc_hist_by_chain
, which plots each concatenation in a separate facet in the plot.
color_scheme_set("brightblue") mcmc_hist_by_chain(posterior, pars = c("wt", "sigma"))
mcmc_dens
The mcmc_dens
function is similar to mcmc_hist
simply plots kernel density estimates instead of histograms.
color_scheme_set("purple") mcmc_dens(posterior, pars = c("wt", "sigma"))
mcmc_dens_overlay
Like mcmc_hist_by_chain
, the mcmc_dens_overlay
role separates the Markov bondage. Only instead of plotting each chain individually, the density estimates are overlaid.
mcmc_dens_overlay(posterior, pars = c("wt", "sigma"))
mcmc_violin
The mcmc_violin
function plots the density estimates of each chain as violins and draws horizontal line segments at user-specified quantiles.
color_scheme_set("teal") mcmc_violin(posterior, pars = c("wt", "sigma"), probs = c(0.1, 0.5, 0.9))
Bivariate plots
Various functions are available for plotting bivariate marginal posterior distributions. Some of these functions also take optional arguments for adding MCMC diagnostic information to the plots. That additional functionality is discussed in the separate Visual MCMC diagnostics vignette.
Documentation:
-
assist("MCMC-scatterplots")
- mc-stan.org/bayesplot/reference/MCMC-scatterplots
mcmc_scatter
The mcmc_scatter
office creates a simple scatterplot of 2 parameters.
color_scheme_set("grayness") mcmc_scatter(posterior, pars = c("(Intercept)", "wt"), size = 1.5, blastoff = 0.five)
mcmc_hex
The mcmc_hex
function creates a similar plot but using hexagonal binning, which tin be useful to avert overplotting.
# requires hexbin package if (requireNamespace("hexbin", quietly = True)) { mcmc_hex(posterior, pars = c("(Intercept)", "wt")) }
mcmc_pairs
In addition to mcmc_scatter
and mcmc_hex
, bayesplot now provides an mcmc_pairs
function for creating pairs plots with more than than two parameters.
color_scheme_set("pink") mcmc_pairs(posterior, pars = c("(Intercept)", "wt", "sigma"), off_diag_args = list(size = 1.5))
The univariate marginal posteriors are shown along the diagonal as histograms, only this tin can exist changed to densities by setting diag_fun="dens"
. Bivariate plots are displayed above and below the diagonal every bit scatterplots, just it is also possible to apply hex plots by setting off_diag_fun="hex"
. By default, mcmc_pairs
shows some of the Markov chains (one-half, if an even number of bondage) above the diagonal and the others beneath. There are many more options for decision-making how the draws should be split up between the plots above and below the diagonal (see the documentation for the status
argument), but they are more than useful when MCMC diagnostic information is included. This is discussed in the Visual MCMC diagnostics vignette.
Trace plots
Trace plots are fourth dimension series plots of Markov bondage. In this vignette we evidence the standard trace plots that bayesplot tin brand. For models fit using whatsoever Stan interface (or Hamiltonian Monte Carlo in general), the Visual MCMC diagnostics vignette provides an example of also adding data nearly divergences to trace plots.
Documentation:
-
help("MCMC-traces")
- mc-stan.org/bayesplot/reference/MCMC-traces
mcmc_trace
The mcmc_trace
part creates standard trace plots:
color_scheme_set("blue") mcmc_trace(posterior, pars = c("wt", "sigma"))
If information technology's hard to meet the difference betwixt the chains nosotros tin can modify to a mixed color scheme, for example:
color_scheme_set("mix-blue-red") mcmc_trace(posterior, pars = c("wt", "sigma"), facet_args = listing(ncol = i, strip.position = "left"))
The code above also illustrates the employ of the facet_args
argument, which is a list of parameters passed to facet_wrap
in ggplot2. Specifying ncol=1
means the trace plots will be stacked in a single column rather than placed side by side, and strip.position="left"
moves the facet labels to the y-axis (instead of above each facet).
The "viridis"
color scheme is also useful for trace plots because it is comprised of very singled-out colors:
color_scheme_set("viridis") mcmc_trace(posterior, pars = "(Intercept)")
mcmc_trace_highlight
The mcmc_trace_highlight
office uses points instead of lines and reduces the opacity of all but a single chain (which is specified using the highlight
argument).
mcmc_trace_highlight(posterior, pars = "sigma", highlight = iii)
References
Gabry, J., and Goodrich, B. (2017). rstanarm: Bayesian Applied Regression Modeling via Stan. R package version 2.15.3. https://mc-stan.org/rstanarm/, https://CRAN.R-project.org/packet=rstanarm
Gabry, J., Simpson, D., Vehtari, A., Betancourt, Yard. and Gelman, A. (2019), Visualization in Bayesian workflow. J. R. Stat. Soc. A, 182: 389-402. :10.1111/rssa.12378. (journal version, arXiv preprint, lawmaking on GitHub)
Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A., and Rubin, D. B. (2013). Bayesian Data Assay. Chapman & Hall/CRC Press, London, tertiary edition.
Stan Development Team. (2017). Stan Modeling Language Users Guide and Reference Manual. https://mc-stan.org/users/documentation/
Source: https://cran.r-project.org/web/packages/bayesplot/vignettes/plotting-mcmc-draws.html
Posted by: smithknorted.blogspot.com
0 Response to "How To Explore And Summarize A Mcmc Draw"
Post a Comment