set.seed(9262017)
# load ggplot2 to use its functions to modify our plots
library(ggplot2)
# some parameter draws to use for demonstration
x <- example_mcmc_draws(params = 6)
dim(x)
dimnames(x)
color_scheme_set("brightblue")
mcmc_intervals(x)
mcmc_intervals(x, pars = c("beta[1]", "beta[2]"))
mcmc_areas(x, regex_pars = "beta\\[[1-3]\\]", prob = 0.8) +
labs(
title = "Posterior distributions",
subtitle = "with medians and 80% intervals"
)
color_scheme_set("red")
p <- mcmc_areas(
x,
pars = c("alpha", "beta[4]"),
prob = 2/3,
prob_outer = 0.9,
point_est = "mean",
border_size = 1.5 # make the ridgelines fatter
)
plot(p)
# \donttest{
# control spacing at top and bottom of plot
# see ?ggplot2::expansion
p + scale_y_discrete(
limits = c("beta[4]", "alpha"),
expand = expansion(add = c(1, 2))
)
p + scale_y_discrete(
limits = c("beta[4]", "alpha"),
expand = expansion(add = c(.1, .3))
)
# relabel parameters
p + scale_y_discrete(
labels = c("alpha" = "param label 1",
"beta[4]" = "param label 2")
)
# relabel parameters and define the order
p + scale_y_discrete(
labels = c("alpha" = "param label 1",
"beta[4]" = "param label 2"),
limits = c("beta[4]", "alpha")
)
# color by rhat value
color_scheme_set("blue")
fake_rhat_values <- c(1, 1.07, 1.3, 1.01, 1.15, 1.005)
mcmc_intervals(x, rhat = fake_rhat_values)
# get the dataframe that is used in the plotting functions
mcmc_intervals_data(x)
mcmc_intervals_data(x, rhat = fake_rhat_values)
mcmc_areas_data(x, pars = "alpha")
color_scheme_set("gray")
p <- mcmc_areas(x, pars = c("alpha", "beta[4]"), rhat = c(1, 1.1))
p + legend_move("bottom")
p + legend_move("none") # or p + legend_none()
# }
# Different area calculations
b3 <- c("beta[1]", "beta[2]", "beta[3]")
mcmc_areas(x, pars = b3, area_method = "equal area") +
labs(
title = "Curves have same area",
subtitle = "A wide, uncertain interval is spread thin when areas are equal"
)
mcmc_areas(x, pars = b3, area_method = "equal height") +
labs(
title = "Curves have same maximum height",
subtitle = "Local curvature is clearer but more uncertain curves use more area"
)
mcmc_areas(x, pars = b3, area_method = "scaled height") +
labs(
title = "Same maximum heights but heights scaled by square-root",
subtitle = "Compromise: Local curvature is accentuated and less area is used"
)
# \donttest{
# apply transformations
mcmc_intervals(
x,
pars = c("beta[2]", "sigma"),
transformations = list("sigma" = "log", "beta[2]" = function(x) x + 3)
)
# apply same transformation to all selected parameters
mcmc_intervals(x, regex_pars = "beta", transformations = "exp")
# }
if (FALSE) {
# example using fitted model from rstanarm package
library(rstanarm)
fit <- stan_glm(
mpg ~ 0 + wt + factor(cyl),
data = mtcars,
iter = 500,
refresh = 0
)
x <- as.matrix(fit)
color_scheme_set("teal")
mcmc_intervals(x, point_est = "mean", prob = 0.8, prob_outer = 0.95)
mcmc_areas(x, regex_pars = "cyl", bw = "SJ",
rhat = rhat(fit, regex_pars = "cyl"))
}
if (FALSE) {
# Example of hierarchically related parameters
# plotted with ridgelines
m <- shinystan::eight_schools@posterior_sample
mcmc_areas_ridges(m, pars = "mu", regex_pars = "theta", border_size = 0.75) +
ggtitle("Treatment effect on eight schools (Rubin, 1981)")
}
Run the code above in your browser using DataLab