Learn R Programming

rvec (version 0.0.7)

draws_ci: Credible Intervals from Random Draws

Description

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

Usage

draws_ci(x, width = 0.95, prefix = NULL, na_rm = FALSE)

# S3 method for rvec draws_ci(x, width = 0.95, prefix = NULL, na_rm = FALSE)

# S3 method for rvec_chr draws_ci(x, width = 0.95, prefix = NULL, na_rm = FALSE)

Value

A tibble

with three columns.

Arguments

x

An object of class rvec.

width

Width(s) of credible interval(s). One or more numbers greater than 0 and less than or equal to 1. Default is 0.975.

prefix

String to be added to the names of columns in the result. Defaults to name of x.

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_ci() to a column in a data frame, as in

my_df$ci <- draws_ci(my_rvec)

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

See Also

draws_quantile() gives more options for forming quantiles.

Other ways of applying pre-specified functions across draws are:

  • draws_all()

  • draws_any

  • draws_min()

  • draws_max()

  • draws_median()

  • draws_mean()

  • draws_mode()

  • draws_quantile()

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_ci(x)
draws_ci(x, width = c(0.5, 0.99))
draws_ci(x, prefix = "results")

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

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

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

Run the code above in your browser using DataLab