Learn R Programming

tidybayes (version 0.12.1.9000)

ungather_samples: Turn tidy data frames of parameters from a Bayesian model back into untidy data

Description

Inverse operations of spread_samples and gather_samples, giving results that look like as_sample_tibble.

Usage

ungather_samples(data, ..., term = "term", estimate = "estimate",
  indices = c(".chain", ".iteration"), drop_indices = FALSE)

unspread_samples(data, ..., indices = c(".chain", ".iteration"), drop_indices = FALSE)

Arguments

data

A tidy data frame of samples, such as one output by spread_samples or gather_samples.

...

Expressions in the form of variable_name[index_1, index_2, ...]. See spread_samples.

term

The name of the column in data that contains the names of parameters from the model.

estimate

The name of the column in data that contains the samples of the parameters.

indices

Character vector of column names in data that should be treated as indices of chain/iteration. The default is c(".chain",".iteration"), which are the same names used for chain/iteration indices returned by spread_samples or gather_samples.

drop_indices

Drop the columns specified by indices from the resulting data frame. Default FALSE.

Value

A data frame.

Details

These functions take symbolic specifications of parameter names and indices in the same format as spread_samples and gather_samples and invert the tidy data frame back into a data frame whose column names are parameters with indices in them.

See Also

spread_samples, gather_samples, as_sample_tibble.

Examples

Run this code
# NOT RUN {
library(dplyr)

data(RankCorr, package = "tidybayes")

# We can use unspread_samples to allow us to manipulate samples with tidybayes
# and then transform the samples into a form we can use with packages like bayesplot.
# Here we subset b[i,j] to just values of i in 1:3 and j == 1, then plot with bayesplot
RankCorr %>%
  spread_samples(b[i,j]) %>%
  filter(i %in% 1:3, j == 1) %>%
  unspread_samples(b[i,j], drop_indices = TRUE) %>%
  bayesplot::mcmc_areas()

# As another example, we could use compare_levels to plot all pairwise comparisons
# of b[i,1] for i in 1:3
RankCorr %>%
  spread_samples(b[i,j]) %>%
  filter(i %in% 1:3, j == 1) %>%
  compare_levels(b, by = i) %>%
  unspread_samples(b[i], drop_indices = TRUE) %>%
  bayesplot::mcmc_areas()

# }

Run the code above in your browser using DataLab