Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tidy_draws

Get a sample of posterior draws from a model as a tibble


Description

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 (see Details).

Usage

tidy_draws(model, ...)

## Default S3 method:
tidy_draws(model, ...)

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

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

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

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

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

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

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

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

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

Arguments

model

A supported Bayesian model fit object. See tidybayes-models() for a list of supported models.

...

Further arguments passed to other methods (mostly unused).

Details

In practice, apart from quick looks at a model you will probably not call this directly; spread_draws() or gather_draws(), which are build on top of this function, provide support for extracting variable dimensions are so are often more useful.

To provide support for new models in tidybayes, you must provide an implementation of this function or an implementation of coda::as.mcmc.list() (tidy_draws should work on any model with an implementation of coda::as.mcmc.list())

tidy_draws can be applied to a data frame that is already a tidy-format data frame of draws, provided it has one row per draw. In other words, it can be applied to data frames that have the same format it returns, and it will return the same data frame back, while checking to ensure the .chain, .iteration, and .draw columns are all integers (converting if possible) and that the .draw column is unique. This allows you to pass already-tidy-format data frames into other tidybayes functions, like spread_draws() or gather_draws().

Value

A data frame (actually, a tibble) with a .chain column, .iteration column, .draw column, and one column for every variable in model.

Author(s)

Matthew Kay

See Also

spread_draws() or gather_draws(), which use this function internally and provides a friendly interface for extracting tidy data frames from model fits.

Examples

library(magrittr)

data(line, package = "coda")

line %>%
  tidy_draws()

tidybayes

Tidy Data and 'Geoms' for Bayesian Models

v2.3.1
GPL (>= 3)
Authors
Matthew Kay [aut, cre], Timothy Mastny [ctb]
Initial release
2020-10-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.