tidy_draws.map: Get a sample of posterior draws from quap and map models as a...

View source: R/tidy_draws.R

tidy_draws.mapR Documentation

Get a sample of posterior draws from quap and map models as a tibble

Description

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.

Usage

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

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

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

## S3 method for class '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)
library(tidybayes)
library(dplyr)

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()


mjskay/tidybayes.rethinking documentation built on April 3, 2024, 1 p.m.