Converts between data-frame-of-rvars format and long-data-frame-of-draws formats by nesting or unnesting all columns containing posterior::rvar objects.

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
#> # A tibble: 3 × 3
#>       i          b[,1]          [,2]            [,3]           [,4]         tau
#>   <int>     <rvar[,1]>    <rvar[,1]>      <rvar[,1]>     <rvar[,1]>  <rvar[1d]>
#> 1     1  -1.08 ± 0.095  -1.82 ± 0.14   0.176 ± 0.071  -0.101 ± 0.12  6.0 ± 0.53
#> 2     2  -0.74 ± 0.146   0.10 ± 0.25  -0.178 ± 0.120   0.268 ± 0.22  3.3 ± 0.53
#> 3     3  -0.34 ± 0.144  -0.73 ± 0.23   0.069 ± 0.121   0.084 ± 0.21  3.7 ± 0.52

# 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
#> # A tibble: 3,000 × 9
#> # Groups:   i [3]
#>        i `b[1,1]` `b[1,2]` `b[1,3]`  `b[1,4]`   tau .chain .iteration .draw
#>    <int>    <dbl>    <dbl>    <dbl>     <dbl> <dbl>  <int>      <int> <int>
#>  1     1   -0.927    -2.09   0.111  -0.000774  5.79      1          1     1
#>  2     1   -0.979    -1.90   0.258  -0.232     6.26      1          2     2
#>  3     1   -1.15     -1.79   0.256  -0.236     7.38      1          3     3
#>  4     1   -1.09     -1.67   0.164  -0.112     5.97      1          4     4
#>  5     1   -1.20     -1.50   0.245  -0.232     6.01      1          5     5
#>  6     1   -1.07     -1.83   0.0839  0.0479    7.03      1          6     6
#>  7     1   -1.11     -1.82   0.0702  0.0382    7.39      1          7     7
#>  8     1   -1.06     -1.94   0.0600  0.0438    5.98      1          8     8
#>  9     1   -0.831    -2.19   0.353  -0.413     6.75      1          9     9
#> 10     1   -0.986    -1.86   0.0827  0.0591    6.76      1         10    10
#> # ℹ 2,990 more rows

# calling nest_rvars() again on the result of unnest_rvars()
# recovers the original data frame
nest_rvars(draws_df)
#> # A tibble: 3 × 3
#> # Groups:   i [3]
#>       i          b[,1]          [,2]            [,3]           [,4]         tau
#>   <int>     <rvar[,1]>    <rvar[,1]>      <rvar[,1]>     <rvar[,1]>  <rvar[1d]>
#> 1     1  -1.08 ± 0.095  -1.82 ± 0.14   0.176 ± 0.071  -0.101 ± 0.12  6.0 ± 0.53
#> 2     2  -0.74 ± 0.146   0.10 ± 0.25  -0.178 ± 0.120   0.268 ± 0.22  3.3 ± 0.53
#> 3     3  -0.34 ± 0.144  -0.73 ± 0.23   0.069 ± 0.121   0.084 ± 0.21  3.7 ± 0.52