parse_dist: Parse distribution specifications into columns of a data...

Description Usage Arguments Details See Also Examples

View source: R/parse_dist.R

Description

Parses simple string distribution specifications, like "normal(0, 1)", into two columns of a data frame, suitable for use with stat_dist_slabinterval and its shortcut stats (like stat_dist_halfeye). This format is output by get_prior, making it particularly useful for visualizing priors from brms models.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
parse_dist(object, ..., dist = ".dist", args = ".args",
  to_r_names = TRUE)

## Default S3 method:
parse_dist(object, ...)

## S3 method for class 'data.frame'
parse_dist(object, dist_col, ..., dist = ".dist",
  args = ".args", to_r_names = TRUE)

## S3 method for class 'character'
parse_dist(object, ..., dist = ".dist",
  args = ".args", to_r_names = TRUE)

## S3 method for class 'factor'
parse_dist(object, ..., dist = ".dist",
  args = ".args", to_r_names = TRUE)

r_dist_name(dist_name)

Arguments

object

A character vector containing distribution specifiations or a data frame with a column containing distribution specifications.

...

Arguments passed to other implementations of parse_dist.

dist

The name of the output column to contain the distribution name

args

The name of the output column to contain the arguments to the distribution

to_r_names

If TRUE (the default), certain common aliases for distribution names are automatically translated into names that R can recognize (i.e., names which have functions starting with r, p, q, and d representing random number generators, distribution functions, etc. for that distribution), using the r_dist_name function. For example, "normal" is translated into "norm" and "lognormal" is translated into "lnorm".

dist_col

A bare (unquoted) column or column expression that resolves to a character vector of distribution specifications.

dist_name

For r_dist_name, a character vector of distribution names to be translated into distribution names R recognizes. Unrecognized names are left as-is.

Details

parse_dist() can be applied to character vectors or to a data frame + bare column name of the column to parse, and returns a data frame with ".dist" and ".args" columns added. parse_dist() uses r_dist_name() to translate distribution names into names reconized by R.

r_dist_name() takes a character vector of names and translates common names into R distribution names. Names are first made into valid R names using make.names, then translated (ignoring character case, ".", and "_"). Thus, "lognormal", "LogNormal", "log_normal", "log-Normal", and any number of other variants all get translated into "lnorm".

See Also

See stat_dist_slabinterval and its shortcut stats, which can easily make use of the output of this function using the dist and args aesthetics.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
library(dplyr)

# parse dist can operate on strings directly...
parse_dist(c("normal(0,1)", "student_t(3,0,1)"))

# ... or on columns of a data frame, where it adds the
# parsed specs back on as columns
data.frame(prior = c("normal(0,1)", "student_t(3,0,1)")) %>%
  parse_dist(prior)

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

  # parse_dist is particularly useful with brms prior specifications,
  # which follow the same format...

  # get priors for a brms model
  priors = get_prior(mpg ~ log(hp), data = mtcars, family = lognormal)

  # The `prior` column output by `get_prior()` is a character vector
  # of distribution specifications. We can parse this directly...
  parse_dist(priors$prior)

  # ... or we can parse it and have it added back onto the original data frame
  # (this form is likely more useful for plotting, since the other columns
  # are retained)
  parse_dist(priors, prior)

}

mjskay/tidybayes documentation built on Oct. 18, 2019, 3:23 a.m.