1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
params <-
list(EVAL = TRUE)
## ----SETTINGS-knitr, include=FALSE--------------------------------------------
stopifnot(require("knitr"))
library("bayesplot")
knitr::opts_chunk$set(
dev = "png",
dpi = 150,
fig.asp = 0.618,
fig.width = 5,
out.width = "60%",
fig.align = "center",
comment = NA,
eval = if (isTRUE(exists("params"))) params$EVAL else FALSE
)
## ----pkgs, include=FALSE------------------------------------------------------
library("ggplot2")
library("rstanarm")
## ----eval=FALSE---------------------------------------------------------------
# library("bayesplot")
# library("ggplot2")
# library("rstanarm")
## ----mtcars-------------------------------------------------------------------
head(mtcars) # see help("mtcars")
## ----eval=FALSE---------------------------------------------------------------
# # linear regression model using stan_glm
# # using '~ .' to include all variables
# fit <- stan_glm(mpg ~ ., data = mtcars, seed = 1111)
# print(fit)
## ----stan_glm, include=FALSE--------------------------------------------------
fit <- stan_glm(mpg ~ ., data = mtcars, QR = TRUE, seed = 1111)
## ----print-fit, echo=FALSE----------------------------------------------------
print(fit)
## ----get-draws----------------------------------------------------------------
posterior <- as.array(fit)
dim(posterior)
dimnames(posterior)
## ----mcmc_intervals-----------------------------------------------------------
color_scheme_set("red")
mcmc_intervals(posterior, pars = c("cyl", "drat", "am", "sigma"))
## ----mcmc_areas---------------------------------------------------------------
mcmc_areas(
posterior,
pars = c("cyl", "drat", "am", "sigma"),
prob = 0.8, # 80% intervals
prob_outer = 0.99, # 99%
point_est = "mean"
)
## ----mcmc_hist, message=FALSE-------------------------------------------------
color_scheme_set("green")
mcmc_hist(posterior, pars = c("wt", "sigma"))
## ----mcmc_hist-transform, message=FALSE---------------------------------------
color_scheme_set("blue")
mcmc_hist(posterior, pars = c("wt", "sigma"),
transformations = list("sigma" = "log"))
## ----mcmc_hist_by_chain, message=FALSE----------------------------------------
color_scheme_set("brightblue")
mcmc_hist_by_chain(posterior, pars = c("wt", "sigma"))
## ----mcmc_dens, message=FALSE-------------------------------------------------
color_scheme_set("purple")
mcmc_dens(posterior, pars = c("wt", "sigma"))
## ----mcmc_dens_overlay, message=FALSE-----------------------------------------
mcmc_dens_overlay(posterior, pars = c("wt", "sigma"))
## ----mcmc_violin--------------------------------------------------------------
color_scheme_set("teal")
mcmc_violin(posterior, pars = c("wt", "sigma"), probs = c(0.1, 0.5, 0.9))
## ----mcmc_scatter-------------------------------------------------------------
color_scheme_set("gray")
mcmc_scatter(posterior, pars = c("(Intercept)", "wt"),
size = 1.5, alpha = 0.5)
## ----mcmc_hex-----------------------------------------------------------------
# requires hexbin package
if (requireNamespace("hexbin", quietly = TRUE)) {
mcmc_hex(posterior, pars = c("(Intercept)", "wt"))
}
## ----mcmc_pairs, message=FALSE------------------------------------------------
color_scheme_set("pink")
mcmc_pairs(posterior, pars = c("(Intercept)", "wt", "sigma"),
off_diag_args = list(size = 1.5))
## ----mcmc_trace---------------------------------------------------------------
color_scheme_set("blue")
mcmc_trace(posterior, pars = c("wt", "sigma"))
## ----change-scheme------------------------------------------------------------
color_scheme_set("mix-blue-red")
mcmc_trace(posterior, pars = c("wt", "sigma"),
facet_args = list(ncol = 1, strip.position = "left"))
## ----viridis-scheme-----------------------------------------------------------
color_scheme_set("viridis")
mcmc_trace(posterior, pars = "(Intercept)")
## ----mcmc_trace_highlight-----------------------------------------------------
mcmc_trace_highlight(posterior, pars = "sigma", highlight = 3)
|