Given a data frame such as might be returned by tidy_draws() or spread_draws(), gather variables and their values from that data frame into a ".variable" and ".value" column.

gather_variables(data, exclude = c(".chain", ".iteration", ".draw", ".row"))

Arguments

data

A data frame with variable names spread across columns, such as one returned by tidy_draws() or spread_draws().

exclude

A character vector of names of columns to be excluded from the gather. Default ignores several meta-data column names used in tidybayes.

Value

A data frame.

Details

This function gathers every column except grouping columns and those matching the expression exclude into key/value columns ".variable" and ".value".

Imagine a data frame data as returned by spread_draws(fit, a[i], b[i,v]), like this:

  • column ".chain": the chain number

  • column ".iteration": the iteration number

  • column ".draw": the draw number

  • column "i": value in 1:5

  • column "v": value in 1:10

  • column "a": value of "a[i]" for draw number ".draw"

  • column "b": value of "b[i,v]" for draw number ".draw"

gather_variables(data) on that data frame would return a grouped data frame (grouped by i and v), with:

  • column ".chain": the chain number

  • column ".iteration": the iteration number

  • column ".draw": the draw number

  • column "i": value in 1:5

  • column "v": value in 1:10

  • column ".variable": value in c("a", "b").

  • column ".value": value of "a[i]" (when ".variable" is "a"; repeated for every value of "v") or "b[i,v]" (when ".variable" is "b") for draw number ".draw"

In this example, this call:

gather_variables(data)

Is roughly equivalent to:

data %>%
  gather(.variable, .value, -c(.chain, .iteration, .draw, i, v)) %>%
  group_by(.variable, .add = TRUE)

See also

Examples

# \donttest{ library(dplyr) data(RankCorr, package = "ggdist") RankCorr %>% spread_draws(b[i,v], tau[i]) %>% gather_variables() %>% median_qi()
#> # A tibble: 24 x 9 #> # Groups: i, v [12] #> i v .variable .value .lower .upper .width .point .interval #> <int> <int> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 1 1 b -1.08 -1.27 -0.897 0.95 median qi #> 2 1 1 tau 6.03 5.03 7.11 0.95 median qi #> 3 1 2 b -1.83 -2.08 -1.54 0.95 median qi #> 4 1 2 tau 6.03 5.03 7.11 0.95 median qi #> 5 1 3 b 0.175 0.0447 0.322 0.95 median qi #> 6 1 3 tau 6.03 5.03 7.11 0.95 median qi #> 7 1 4 b -0.101 -0.347 0.134 0.95 median qi #> 8 1 4 tau 6.03 5.03 7.11 0.95 median qi #> 9 2 1 b -0.739 -1.01 -0.430 0.95 median qi #> 10 2 1 tau 3.30 2.41 4.46 0.95 median qi #> # ... with 14 more rows
# the first three lines below are roughly equivalent to ggmcmc::ggs(RankCorr) RankCorr %>% tidy_draws() %>% gather_variables() %>% median_qi()
#> # A tibble: 19 x 7 #> .variable .value .lower .upper .width .point .interval #> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> #> 1 b[1,1] -1.08 -1.27 -0.897 0.95 median qi #> 2 b[1,2] -1.83 -2.08 -1.54 0.95 median qi #> 3 b[1,3] 0.175 0.0447 0.322 0.95 median qi #> 4 b[1,4] -0.101 -0.347 0.134 0.95 median qi #> 5 b[2,1] -0.739 -1.01 -0.430 0.95 median qi #> 6 b[2,2] 0.0909 -0.384 0.602 0.95 median qi #> 7 b[2,3] -0.175 -0.410 0.0563 0.95 median qi #> 8 b[2,4] 0.270 -0.151 0.710 0.95 median qi #> 9 b[3,1] -0.340 -0.609 -0.0615 0.95 median qi #> 10 b[3,2] -0.732 -1.18 -0.286 0.95 median qi #> 11 b[3,3] 0.0636 -0.163 0.312 0.95 median qi #> 12 b[3,4] 0.0918 -0.327 0.463 0.95 median qi #> 13 tau[1] 6.03 5.03 7.11 0.95 median qi #> 14 tau[2] 3.30 2.41 4.46 0.95 median qi #> 15 tau[3] 3.65 2.73 4.72 0.95 median qi #> 16 typical_r 0.548 0.309 0.778 0.95 median qi #> 17 u_tau[1] 5.62 3.86 8.03 0.95 median qi #> 18 u_tau[2] 5.48 3.46 9.07 0.95 median qi #> 19 u_tau[3] 4.96 3.10 7.86 0.95 median qi
# }