| MCMC-traces {bayesplot} | R Documentation |
Trace plot (or traceplot) of MCMC draws. See the Plot Descriptions section, below, for details.
mcmc_trace(x, pars = character(), regex_pars = character(), transformations = list(), facet_args = list(), ..., n_warmup = 0, window = NULL, size = NULL, np = NULL, np_style = trace_style_np(), divergences = NULL) mcmc_trace_highlight(x, pars = character(), regex_pars = character(), transformations = list(), facet_args = list(), ..., n_warmup = 0, window = NULL, size = NULL, alpha = 0.2, highlight = 1) trace_style_np(div_color = "red", div_size = 0.25, div_alpha = 1)
x |
A 3-D array, matrix, list of matrices, or data frame of MCMC draws. The MCMC-overview page provides details on how to specify each these allowed inputs. |
pars |
An optional character vector of parameter names. If neither
|
regex_pars |
An optional regular expression to use for
parameter selection. Can be specified instead of |
transformations |
Optionally, transformations to apply to parameters
before plotting. If Note: due to partial argument matching |
facet_args |
A named list of arguments (other than |
... |
Currently ignored. |
n_warmup |
An integer; the number of warmup iterations included in
|
window |
An integer vector of length two specifying the limits of a range of iterations to display. |
size |
An optional value to override the default line size
( |
np |
For models fit using |
np_style |
A call to the |
divergences |
Deprecated. Use the |
alpha |
For |
highlight |
For |
div_color, div_size, div_alpha |
Optional arguments to the
|
A ggplot object that can be further customized using the ggplot2 package.
mcmc_traceStandard trace plots of MCMC draws. For models fit using NUTS,
the np argument can be used to also show divergences on the trace plot.
mcmc_trace_highlightTraces are plotted using points rather than lines and the opacity of all
chains but one (specified by the highlight argument) is reduced.
Other MCMC: MCMC-combos,
MCMC-diagnostics,
MCMC-distributions,
MCMC-intervals, MCMC-nuts,
MCMC-overview, MCMC-parcoord,
MCMC-recover,
MCMC-scatterplots
# some parameter draws to use for demonstration
x <- example_mcmc_draws(chains = 4, params = 6)
dim(x)
dimnames(x)
# trace plots of the betas
color_scheme_set("viridis")
mcmc_trace(x, regex_pars = "beta")
color_scheme_set("viridisA")
mcmc_trace(x, regex_pars = "beta")
color_scheme_set("viridisC")
mcmc_trace(x, regex_pars = "beta")
# mix color schemes
color_scheme_set("mix-blue-red")
mcmc_trace(x, regex_pars = "beta")
# use traditional ggplot discrete color scale
mcmc_trace(x, pars = c("alpha", "sigma")) +
ggplot2::scale_color_discrete()
# zoom in on a window of iterations, increase line size,
# add tick marks, move legend to the top, add gray background
color_scheme_set("viridisA")
mcmc_trace(x[,, 1:4], window = c(100, 130), size = 1) +
panel_bg(fill = "gray90", color = NA) +
legend_move("top")
## Not run:
# parse facet label text
color_scheme_set("purple")
p <- mcmc_trace(
x,
regex_pars = "beta\\[[1,3]\\]",
facet_args = list(labeller = ggplot2::label_parsed)
)
p + facet_text(size = 15)
# mark first 100 draws as warmup
mcmc_trace(x, n_warmup = 100)
# plot as points, highlighting chain 2
color_scheme_set("brightblue")
mcmc_trace_highlight(x, pars = "sigma", highlight = 2, size = 2)
# for models fit using HMC/NUTS divergences can be displayed in the trace plot
library("rstanarm")
fit <- stan_glm(mpg ~ ., data = mtcars,
# next line to keep example fast and also ensure we get some divergences
prior = hs(), iter = 400, adapt_delta = 0.8)
# extract draws using as.array (instead of as.matrix) to keep
# chains separate for trace plot
posterior <- as.array(fit)
# for stanfit and stanreg objects use nuts_params() to get the divergences
mcmc_trace(posterior, pars = "sigma", np = nuts_params(fit))
color_scheme_set("viridis")
mcmc_trace(
posterior,
pars = c("wt", "sigma"),
size = 0.5,
facet_args = list(nrow = 2),
np = nuts_params(fit),
np_style = trace_style_np(div_color = "black", div_size = 0.5)
)
color_scheme_set("viridis")
mcmc_trace(
posterior,
pars = c("wt", "sigma"),
size = 0.8,
facet_args = list(nrow = 2),
divergences = nuts_params(fit),
div_color = "black"
)
## End(Not run)