library(distplyr)

One purpose of distplyr is to handle the menial distribution-related calculations for you. Just specify a distribution once, and there is no need to manage its components anymore.

Functional Representations of a Distribution

A distribution can be represented by different functions. In distplyr, you can:

  • evaluate the function directly by calling eval_*;
  • evaluate the function and enframe results together with the function arguments with enframe_*; or
  • get the function itself using get_*.

Here are the representations and the corresponding distplyr functions:

Quantity distplyr Functions
Cumulative Distribution Function eval_cdf(), get_cdf(), enframe_cdf()
Survival Function eval_survival(), get_survival(), enframe_survival()
Quantile Function eval_quantile(), get_quantile(), enframe_quantile()
Hazard Function eval_hazard(), get_hazard(), enframe_hazard()
Cumulative Hazard Function eval_chf(), get_chf(), enframe_chf()
Probability density function eval_density(), get_density(), enframe_density()
Probability mass function eval_pmf(), get_pmf(), enframe_pmf()
Random sample generator realise(), realize()

These functions all take a distribution object as their first argument, and eval_* and enframe_* have a second argument named at indicating where to evaluate the function. The at argument is vectorized.

Note that all of the stats::p*/d*/q*/r* functions are included in this list of representations, except for the r functions (such as rnorm(), rpois(), etc.). These r functions generate a random sample, which cannot be used to reconstruct a distribution exactly, and are therefore omitted in the list of representations. Instead, use the realise() or realize() function.

Here is an example of evaluating the hazard function and the random sample generator of a Uniform(0,1) distribution:

d <- dst_unif(0, 3)
eval_hazard(d, at = 0:10)
##  [1] 0.3333333 0.5000000 1.0000000       Inf       NaN       NaN       NaN
##  [8]       NaN       NaN       NaN       NaN
set.seed(10)
realise(d, n = 5)
## [1] 1.5224346 0.9203055 1.2807230 2.0793062 0.2554079

Properties of Distributions

Distributions have various numeric properties. Common examples are the mean and variance, but there are many others as well.

Below is a table of the properties incorporated in distplyr:

Property distplyr Function
Mean get_mean()
Median get_median()
Mode get_mode()
Variance get_variance()
Standard Deviation get_sd()
Inter-Quartile Range get_iqr()
Entropy get_entropy()
Skewness get_skewness()
Excess Kurtosis get_kurtosis_exc()
Kurtosis get_kurtosis_raw()
Extreme Value (Tail) Index get_evi()

Here are some properties of a certain mixture of Gaussians:

m <- mix(dst_norm(0, 1), dst_norm(5, 1), weights = c(1, 4))
get_mean(m)
## [1] 4
## [1] 2.236068
## [1] 0