x <- example_mcmc_draws(params = 6)
dimnames(x)
mcmc_hex(x, pars = vars(alpha, `beta[2]`))
mcmc_dens(x, pars = vars(sigma, contains("beta")))
mcmc_hist(x, pars = vars(-contains("beta")))
# using the param_range() helper
mcmc_hist(x, pars = vars(param_range("beta", c(1, 3, 4))))
# \donttest{
#############################
## Examples using rstanarm ##
#############################
if (requireNamespace("rstanarm", quietly = TRUE)) {
# see ?rstanarm::example_model
fit <- example("example_model", package = "rstanarm", local=TRUE)$value
print(fit)
posterior <- as.data.frame(fit)
str(posterior)
color_scheme_set("brightblue")
mcmc_hist(posterior, pars = vars(size, contains("period")))
# same as previous but using dplyr::select() and piping
library("dplyr")
posterior %>%
select(size, contains("period")) %>%
mcmc_hist()
mcmc_intervals(posterior, pars = vars(contains("herd")))
mcmc_intervals(posterior, pars = vars(contains("herd"), -contains("Sigma")))
bayesplot_theme_set(ggplot2::theme_dark())
color_scheme_set("viridisC")
mcmc_areas_ridges(posterior, pars = vars(starts_with("b[")))
bayesplot_theme_set()
color_scheme_set("purple")
not_789 <- vars(starts_with("b["), -matches("[7-9]"))
mcmc_intervals(posterior, pars = not_789)
# using the param_glue() helper
just_149 <- vars(param_glue("b[(Intercept) herd:{level}]", level = c(1,4,9)))
mcmc_intervals(posterior, pars = just_149)
# same but using param_glue() with dplyr::select()
# before passing to bayesplot
posterior %>%
select(param_glue("b[(Intercept) herd:{level}]",
level = c(1, 4, 9))) %>%
mcmc_intervals()
}
# }
if (FALSE) {
###################################
## More examples of param_glue() ##
###################################
library(dplyr)
posterior <- tibble(
b_Intercept = rnorm(1000),
sd_condition__Intercept = rexp(1000),
sigma = rexp(1000),
`r_condition[A,Intercept]` = rnorm(1000),
`r_condition[B,Intercept]` = rnorm(1000),
`r_condition[C,Intercept]` = rnorm(1000),
`r_condition[A,Slope]` = rnorm(1000),
`r_condition[B,Slope]` = rnorm(1000)
)
posterior
# using one expression in braces
posterior %>%
select(
param_glue("r_condition[{level},Intercept]", level = c("A", "B"))
) %>%
mcmc_hist()
# using multiple expressions in braces
posterior %>%
select(
param_glue(
"r_condition[{level},{type}]",
level = c("A", "B"),
type = c("Intercept", "Slope"))
) %>%
mcmc_hist()
}
Run the code above in your browser using DataLab