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.

  • For nest_rvars(), the data frame should be in long-data-frame-of-draws format; i.e. it should contain a .draw column (and optionally .chain and .iteration columns) indexing draws. It should be a grouped by any columns that are not intended to be nested.

  • For unnest_rvars(), the data frame should have at least one column that is an rvar; all rvar columns will be unnested.

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)


[Package tidybayes version 3.0.6 Index]