Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sample_draws

Sample draws from a tidy-format data frame of draws


Description

Given a tidy-format data frame of draws with a column indexing each draw, subsample the data frame to a given size based on a column indexing draws, ensuring that rows in sub-groups of a grouped data frame are sampled from the same draws.

Usage

sample_draws(data, n, draw = ".draw")

Arguments

data

Data frame to sample from

n

The number of draws to select

draw

The name of the column indexing the draws

Details

sample_draws() makes it easier to sub-sample a grouped, tidy-format data frame of draws. On a grouped data frame, the naive approach of using filter with the .draw column will give incorrect results as it will select a different sample within each group. sample_draws() ensures the same sample is selected within each group.

Author(s)

Matthew Kay

Examples

library(ggplot2)
library(dplyr)

if (
  require("brms", quietly = TRUE) &&
  require("modelr", quietly = TRUE)
) {

  theme_set(theme_light())

  m_mpg = brm(mpg ~ hp * cyl, data = mtcars,
    # 1 chain / few iterations just so example runs quickly
    # do not use in practice
    chains = 1, iter = 500)

  # draw 100 fit lines from the posterior and overplot them
  mtcars %>%
    group_by(cyl) %>%
    data_grid(hp = seq_range(hp, n = 101)) %>%
    add_fitted_draws(m_mpg) %>%
    sample_draws(100) %>%
    ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) +
    geom_line(aes(y = .value, group = paste(cyl, .draw)), alpha = 0.25) +
    geom_point(data = mtcars)
}

tidybayes

Tidy Data and 'Geoms' for Bayesian Models

v2.3.1
GPL (>= 3)
Authors
Matthew Kay [aut, cre], Timothy Mastny [ctb]
Initial release
2020-10-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.