Create pdqr-functions with new_*()

Package ‘pdqr’ supports two types of distributions:

Note that all distributions assume finite support (output values are bounded from below and above) and finite values of density function (density function in case of “continuous” type can’t go to infinity).

All new_*() functions create a pdqr-function of certain type (“discrete” or “continuous”) based on sample or data frame of appropriate structure:

We will use the following data frame inputs in examples:

# For type "discrete"
dis_df <- data.frame(x = 1:4, prob = 4:1 / 10)
# For type "continuous"
con_df <- data.frame(x = 1:4, y = c(0, 1, 1, 1))

This vignette is organized as follows:

P-functions

P-function (analogue of p*() functions in base R) represents a cumulative distribution function of distribution.

From sample

From data frame

D-functions

D-function (analogue of d*() functions in base R) represents a probability mass function for “discrete” type and density function for “continuous”:

From sample

From data frame

Q-functions

Q-function (analogue of q*() functions in base R) represents a quantile function, an inverse of corresponding p-function:

From sample

From data frame

R-functions

R-function (analogue of r*() functions in base R) represents a random generation function. For “discrete” type it will generate only values present in input. For “continuous” function it will generate values from distribution corresponding to one estimated with density().

From sample

From data frame

Special cases

Dirac-like

When creating “continuous” pdqr-function with new_*() from single number, a special “dirac-like” pdqr-function is created. It is an approximation of single number with triangular distribution of very narrow support (1e-8 of magnitude):

Boolean

Boolean pdqr-function is a special case of “discrete” function, which values are exactly 0 and 1. Those functions are usually created after transformations involving logical operators (see vignette on transformation for more details). It is assumed that 0 represents that some expression is false, and 1 is for being true. Corresponding probabilities describe distribution of expression’s logical values. The only difference from other “discrete” pdqr-functions is in more detailed printing.

Using density() arguments

When creating pdqr-function of “continuous” type, density() is used to estimate density. To tweak its performance, supply its extra arguments directly to new_*() functions. Here are some examples:

plot(
  new_d(mtcars$mpg, "continuous"), lwd = 3,
  main = "Examples of `density()` options"
)

# Argument `adjust` of `density()` helps to define smoothing bandwidth
lines(new_d(mtcars$mpg, "continuous", adj = 0.3), col = "blue")

# Argument `n` defines number of points to be used in piecewise-linear
# approximation
lines(new_d(mtcars$mpg, "continuous", n = 5), col = "green")

# Argument `cut` defines the "extending" property of density estimation.
# Using `cut = 0` assumes that density can't go outside of input's range
lines(new_d(mtcars$mpg, "continuous", cut = 0), col = "magenta")

Metadata of pdqr-functions

Every pdqr-function has metadata, information which describes underline distribution and pdqr-function. Family of meta_*() functions are implemented to extract that information:

# Type "discrete"
d_dis <- new_d(1:4, type = "discrete")
meta_x_tbl(d_dis)
#>   x prob cumprob
#> 1 1 0.25    0.25
#> 2 2 0.25    0.50
#> 3 3 0.25    0.75
#> 4 4 0.25    1.00
meta_class(d_dis)
#> [1] "d"
meta_type(d_dis)
#> [1] "discrete"
meta_support(d_dis)
#> [1] 1 4

# Type "continuous"
p_con <- new_p(1:4, type = "continuous")
head(meta_x_tbl(p_con))
#>           x           y      cumprob
#> 1 -1.290542 0.001477707 0.000000e+00
#> 2 -1.275706 0.001568091 2.259340e-05
#> 3 -1.260870 0.001660241 4.654081e-05
#> 4 -1.246034 0.001759392 7.190728e-05
#> 5 -1.231199 0.001862292 9.877253e-05
#> 6 -1.216363 0.001970915 1.272068e-04
meta_class(p_con)
#> [1] "p"
meta_type(p_con)
#> [1] "continuous"
meta_support(p_con)
#> [1] -1.290542  6.290542

# Dirac-like "continuous" function
r_dirac <- new_r(1, type = "continuous")
dput(meta_x_tbl(r_dirac))
#> structure(list(x = c(0.99999999, 1, 1.00000001), y = c(0, 100000000.052636, 
#> 0), cumprob = c(0, 0.5, 1)), row.names = c(NA, -3L), class = "data.frame")
dput(meta_support(r_dirac))
#> c(0.99999999, 1.00000001)

# `meta_all()` returns all metadata in a single list
meta_all(d_dis)
#> $class
#> [1] "d"
#> 
#> $type
#> [1] "discrete"
#> 
#> $support
#> [1] 1 4
#> 
#> $x_tbl
#>   x prob cumprob
#> 1 1 0.25    0.25
#> 2 2 0.25    0.50
#> 3 3 0.25    0.75
#> 4 4 0.25    1.00

For more details go to help page of meta_all().