Learn R Programming

tidybayes (version 0.12.1.9000)

gather_terms: Gather terms from a tidy data frame of parameter samples into a single column

Description

Given a data frame such as might be returned by as_sample_tibble or spread_samples, gather terms and their estimates from that data frame into a term and estimate column.

Usage

gather_terms(data, ignore_columns = "^\\..*")

Arguments

data

A data frame with parameter/term names spread across columns, such as one returned by as_sample_tibble or spread_samples.

ignore_columns

A regular expression that matches column names to ignore in the gather. The default ignores columns that start with ".".

Value

A data frame.

Details

This function gather every column except grouping columns and those matching the regular expression ignore_columns into key/value columns "term" and "estimate".

This function uses "term" and "estimate" instead of names like "parameter" and "value" in order to be consistent with the naming scheme of tidy.

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

  • column ".chain": the chain number

  • column ".iteration": the interation number

  • column "i": value in 1:5

  • column "v": value in 1:10

  • column "a": value of "a[i]" for iteration number ".iteration" on chain number ".chain"

  • column "b": value of "b[i,v]" for iteration number ".iteration" on chain number ".chain"

gather_terms(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 interation number

  • column "i": value in 1:5

  • column "v": value in 1:10

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

  • column "estimate": value of "a[i]" (when "term" is "a"; repeated for every value of "v") or "b[i,v]" (when "term" is "b") for iteration number ".iteration" on chain number ".chain"

In this example, this call:

gather_terms(data)

Is roughly equivalent to:

data %>%
  gather(term, estimate, -c(.chain, .iteration, i, v)) %>%
  group_by(term, add = TRUE)

See Also

spread_samples, as_sample_tibble.

Examples

Run this code
# NOT RUN {
library(dplyr)

data(RankCorr, package = "tidybayes")

RankCorr %>%
  spread_samples(b[i,v], tau[i]) %>%
  gather_terms() %>%
  median_qi()

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

# }

Run the code above in your browser using DataLab