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. This function does not parse indices from variable names (e.g. for variable names like "x[1]"); see spread_draws() or gather_draws() for functions that parse variable indices.

tidy_draws(model, ...)

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

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

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

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

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

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

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

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

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

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

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

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

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

Arguments

model

A supported Bayesian model fit. Tidybayes supports a variety of model objects; for a full list of supported models, see tidybayes-models.

...

Further arguments passed to other methods (mostly unused).

Value

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

Details

This function can be useful for quick glances at models (especially combined with gather_variables() and median_qi()), and for models with parameters without indices in their names (like "x[1]"). spread_draws() and gather_draws(), which do parse variable name indices, call this function internally if their input is not already a tidy data frame.

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(). This functionality can be useful if the tidying step is expensive: you can tidy once, possibly subsetting to some particular variables of interest, then call spread_draws() or gather_draws() repeatedly to extract variables and indices from the already-tidied data frame.

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.

Author

Matthew Kay

Examples


library(magrittr)

data(line, package = "coda")

line %>%
  tidy_draws()
#> # A tibble: 400 × 6
#>    .chain .iteration .draw alpha   beta  sigma
#>     <int>      <int> <int> <dbl>  <dbl>  <dbl>
#>  1      1          1     1  7.17 -1.57  11.2  
#>  2      1          2     2  2.95  1.50   4.89 
#>  3      1          3     3  3.67  0.628  1.40 
#>  4      1          4     4  3.32  1.18   0.663
#>  5      1          5     5  3.71  0.490  1.36 
#>  6      1          6     6  3.58  0.207  1.04 
#>  7      1          7     7  2.70  0.883  1.29 
#>  8      1          8     8  2.96  1.09   0.459
#>  9      1          9     9  3.53  1.07   0.634
#> 10      1         10    10  2.09  1.48   0.913
#> # ℹ 390 more rows