gather_variables {tidybayes}R Documentation

Gather variables from a tidy data frame of draws from variables into a single column

Description

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.

Usage

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.

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:

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

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)

Value

A data frame.

Author(s)

Matthew Kay

See Also

spread_draws(), tidy_draws().

Examples



library(dplyr)

data(RankCorr, package = "ggdist")

RankCorr %>%
  spread_draws(b[i,v], tau[i]) %>%
  gather_variables() %>%
  median_qi()

# the first three lines below are roughly equivalent to ggmcmc::ggs(RankCorr)
RankCorr %>%
  tidy_draws() %>%
  gather_variables() %>%
  median_qi()



[Package tidybayes version 3.0.6 Index]