0% found this document useful (0 votes)
50 views

Bayesplot

This document provides information on using the bayesplot package in R to visualize and diagnose Bayesian regression models fit with rstanarm. Key points: - It fits a linear regression using rstanarm and stores the posterior samples - bayesplot functions like mcmc_areas, mcmc_pairs, and mcmc_trace are used to visualize the posterior distributions and check for convergence - posterior_predict is used to generate posterior predictive checks to assess model fit - nuts_params and mcmc_scatter check diagnostics related to the NUTS sampling algorithm used by rstanarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Bayesplot

This document provides information on using the bayesplot package in R to visualize and diagnose Bayesian regression models fit with rstanarm. Key points: - It fits a linear regression using rstanarm and stores the posterior samples - bayesplot functions like mcmc_areas, mcmc_pairs, and mcmc_trace are used to visualize the posterior distributions and check for convergence - posterior_predict is used to generate posterior predictive checks to assess model fit - nuts_params and mcmc_scatter check diagnostics related to the NUTS sampling algorithm used by rstanarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Bayesplot :: CHEAT SHEET chains_trace

library("bayesplot")
ppd %>%
library("rstanarm") (Intercept) σ
options(mc.cores = parallel::detectCores()) Chain ppc_stat_grouped(y = mtcars$mpg, group = mtcars$am,
library("ggplot2") 5 stat = "median", binwidth=0.5)
50 1
library("dplyr") 4
2 0 1
0 3
Model Parameters 3
To showcase bayesplot, we’ll fit linear regression using rstanarm::stan glm and use this −50 2
4
model throughout. 0 200 400 600 800 1000 0 200 400 600 800 1000 T = median
T (y rep)
model <- stan_glm(mpg ˜ ., data=mtcars, chains=4) The pairs plot is helpful in determining if you have any highly correlated parameters.
posterior <- as.matrix(model)
posterior_chains %>% T (y )
Chances are good you’re most interested in the posterior distributions for select parameters. mcmc_pairs(pars = c("(Intercept)", "wt", "sigma"))

(Intercept) 13 15 17 19 2120.0 22.5 25.0 27.5 30.0


plot_title <- ggtitle("Posterior distributions", 50 50
"medians and 80% intervals")
0 0
mcmc_areas(posterior, Diagnostics
pars = c("drat", "am", "wt"), −50 −50 Bayesplot makes it easy to check diagnostics specific to the NUTS sampling method that
prob = 0.8) + plot_title −50 0 50 −10 −5 0 2 3 4 5 rstanarm uses by default.
4 wt
Posterior distributions 0 0
mcmc_scatter(posterior, pars = c("wt", "sigma"),
np = nuts_params(model$stanfit))
medians and 80% intervals −4
−5
−8
−12 −10 5
−50 0 50 −12 −8 −4 0 4 2 3 4 5
drat 5 5 sigma 4

sigma
4 4
3 3
3
am 2 2
−50 0 50 −12 −8 −4 0 4 2 3 4 5
2

wt Posterior Predictive Checks −12 −8 −4 0 4


Check how well the model covers your data with draws from the posterior predictive density.
−10 −5 0 5 10 wt
Diagnosing convergence with traceplots is simple.
ppd <- posterior_predict(model, draws=500) np <- nuts_params(model$stanfit)
mcmc_trace(posterior, pars=c("sigma", "wt")) ppd %>% mcmc_nuts_energy(np, binwidth=1) +
ppc_intervals(y = mtcars$mpg, yrep = ., x = mtcars$wt, prob = 0.5) + ggtitle("NUTS Energy Diagnostic")
labs(x = "Weight (1000 lbs)", y = "MPG",
sigma wt title = "50% posterior predictive intervals of MPG by weight")
5 4 NUTS Energy Diagnostic
4
0 50% posterior predictive intervals of MPG by weight 1 2
−4
3 ●

−8 ●●
2 30 ●●
−12 ● ●● ●
●●
0 1000 2000 3000 4000 0 1000 2000 3000 4000 ● ● ● πE
●●

3 4
●●
MPG

● ●●



Using as.array, you can extract each of the four chain’s posterior draws, different from 20 ●●
● ●
●●


● ● ●
● y
π∆E
above. This allows you to see each chain’s traceplot for selected parameters. ●


● ●●



●● ●
●●


● ● ●
● y rep
posterior_chains <- as.array(model) ●●
●●
●●●
fargs <- list(ncol = 2, labeller = label_parsed) 10
pars <- c("(Intercept)", "sigma") −20 −10 0 10 20 −20 −10 0 10 20
chains_trace <- mcmc_trace(posterior_chains, pars = pars, E−E
n_warmup = 300, facet_args = fargs) 2 3 4 5
Weight (1000 lbs)
CC BY-SA 4.0 ◦ Edward A. Roualdes ◦ roualdes.us ◦ version 0.0.2 ◦ updated: 2020-05-19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy