Learn R Programming

rvec (version 0.0.7)

draws_quantile: Quantiles Across Random Draws

Description

Summarise the distribution of random draws in an rvec, using quantiles.

Usage

draws_quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975), na_rm = FALSE)

# S3 method for rvec draws_quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975), na_rm = FALSE)

# S3 method for rvec_chr draws_quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975), na_rm = FALSE)

Value

A tibble.

Arguments

x

An object of class rvec.

probs

Vector of probabilities.

na_rm

Whether to remove NAs before calculating summaries. Default is FALSE.

Warning

It is tempting to assign the results of a call to draws_quantile() to a column in a data frame, as in

my_df$quantile <- draws_quantile(my_rvec)

However, creating data frame columns in this way can corrupt data frames. For safer options, see the examples below.

Details

The probs argument defaults to c(0.025, 0.25, 0.5, 0.75, 0.975), the values needed for a median, a 50% credible intervals, and a 95% credible interval.

See Also

draws_ci() creates simple credible intervals.

Other functions for applying pre-specified functions across draws are:

  • draws_all()

  • draws_any()

  • draws_ci()

  • draws_min()

  • draws_max()

  • draws_median()

  • draws_mean()

  • draws_mode()

Apply arbitrary function across draws:

  • draws_fun()

For additional functions for summarising random draws, see tidybayes and ggdist. Function as_list_col() converts rvecs into a format that tidybayes and ggdist can work with.

Examples

Run this code
set.seed(0)
m <- rbind(a = rnorm(100, mean = 5, sd = 2),
           b = rnorm(100, mean = -3, sd = 3),
           c = rnorm(100, mean = 0, sd = 20))
x <- rvec(m)
x
draws_quantile(x)

## results from 'draws_quantile'
## assigned to a data frame
library(dplyr)
df <- data.frame(x)

## base R approach
cbind(df, draws_quantile(x))

## a tidyverse alternative:
## mutate with no '='
df |>
  mutate(draws_quantile(x))

Run the code above in your browser using DataLab