Implementation of tidybayes::tidy_draws() for rethinking::map() and rethinking::quap() models. Extract draws from a Bayesian fit into a wide-format data frame with a .chain, .iteration, and .draw column, as well as all variables as columns. While this function can be useful for quick glances at models (especially combined with gather_variables() and median_qi()), it is generally speaking not as useful as spread_draws() or gather_draws() for most applications, and is mainly used internally.

# S3 method for map
tidy_draws(model, n = 5000, ...)

# S3 method for quap
tidy_draws(model, n = 5000, ...)

# S3 method for map2stan
tidy_draws(model, ...)

# S3 method for ulam
tidy_draws(model, ...)

Arguments

model

A model fit using rethinking::map(), rethinking::quap(), rethinking::map2stan(), or rethinking::ulam()

n

For map and quap models, the number of draws to generate (defaults to 5000). Ignored for map2stan and ulam models.

...

Further arguments passed to other methods (mostly unused).

Details

The main additional functionality compared to tidybayes::tidy_draws() when used on other models is that since draws must be generated on-the-fly, an argument (n) is provided to indicate how many draws to take. The .chain and .iteration columns are also always NA, since they have no meaning for these model types (use the .draw column if you need to index draws). Otherwise, the result of this function follows the same format as tidybayes::tidy_draws(); see that documentation for more information.

Examples

library(rethinking)
#> Loading required package: rstan
#> Loading required package: StanHeaders
#> #> rstan version 2.26.2 (Stan version 2.26.1)
#> For execution on a local, multicore CPU with excess RAM we recommend calling #> options(mc.cores = parallel::detectCores()). #> To avoid recompilation of unchanged Stan programs, we recommend calling #> rstan_options(auto_write = TRUE) #> For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions, #> change `threads_per_chain` option: #> rstan_options(threads_per_chain = 1)
#> Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
#> Loading required package: parallel
#> rethinking (Version 2.13)
#> #> Attaching package: 'rethinking'
#> The following object is masked from 'package:stats': #> #> rstudent
#> #> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats': #> #> filter, lag
#> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union
m = quap(alist( mpg ~ dlnorm(mu, sigma), mu <- a + b*wt, c(a,b) ~ dnorm(0, 10), sigma ~ dexp(1) ), data = mtcars, start = list(a = 4, b = -1, sigma = 1) ) m %>% tidy_draws() %>% gather_variables() %>% median_qi()
#> # A tibble: 3 x 7 #> .variable .value .lower .upper .width .point .interval #> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 a 3.83 3.67 3.99 0.95 median qi #> 2 b -0.271 -0.319 -0.224 0.95 median qi #> 3 sigma 0.132 0.0995 0.163 0.95 median qi