| nest_rvars {tidybayes} | R Documentation |
Nest and unnest rvar columns in data frames
Description
Converts between data-frame-of-rvars format and long-data-frame-of-draws
formats by nesting or unnesting all columns containing posterior::rvar
objects.
Usage
nest_rvars(data)
unnest_rvars(data)
Arguments
data |
A data frame to nest or unnest.
|
Value
For nest_rvars(), returns a data frame without .chain, .iteration, and
.draw columns, where all non-grouped columns have been converted to rvars.
For unnest_rvars(), returns a data frame with .chain, .iteration, and
.draw columns added, where every rvar column in the input has been
converted to (one or more) columns containing draws from those rvars
in long format. The result is grouped by all non-rvar columns in the
input; this ensures that nest_rvars(unnest_rvars(x)) returns x.
Examples
library(dplyr)
data(RankCorr, package = "ggdist")
# here's a data frame with some rvars
rvar_df = RankCorr %>%
spread_rvars(b[i,], tau[i])
rvar_df
# we can unnest it into long format.
# note how the result is grouped by all non-rvar input columns,
# and nested indices in `b` are converted into columns.
draws_df = rvar_df %>%
unnest_rvars()
draws_df
# calling nest_rvars() again on the result of unnest_rvars()
# recovers the original data frame
nest_rvars(draws_df)