Package ‘pdqr’ supports two types of distributions:

• Type “discrete”: random variable has finite number of output values. It is explicitly defined by the collection of its values with their corresponding probability.
• Type “continuous”: there are infinite number of output values in the form of continuous random variable. It is explicitly defined by piecewise-linear density function.

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:

• Sample input is processed based on type. For “discrete” type it gets tabulated with frequency of unique values serving as their probability. For “continuous” type distribution density is estimated using density() function if input has at least 2 elements. For 1 element special “dirac-like” pdqr-function is created: an approximation single number with triangular distribution of very narrow support (1e-8 of magnitude). Basically, sample input is converted into data frame of appropriate structure that defines distribution (see next list item).
• Data frame input should completely define distribution. For “discrete” type it should have “x” and “prob” columns for output values and their probabilities. For “continuous” type - “x” and “y” columns for points, which define piecewise-linear continuous density function. Columns “prob” and “y” will be automatically normalized to represent proper distribution: sum of “prob” will be 1 and total square under graph of piecewise-linear function will be 1.

We will use the following data frame inputs in examples:

This vignette is organized as follows:

• Four sections about how to create p-, d-, q-, and r-functions (both from sample and data frame).
• Section “Special cases”, which describes two special cases of pdqr-functions: dirac-like and boolean.
• Section “Using density() arguments” describes how to use density() arguments to tweak smoothing during creation of “continuous” pdqr-functions.

## 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 data frame

d_df_dis <- new_d(dis_df, type = "discrete")
d_df_dis
#> Probability mass function of discrete type
#> Support: [1, 4] (4 elements)

d_df_con <- new_d(con_df, type = "continuous")
d_df_con
#> Density function of continuous type
#> Support: [1, 4] (3 intervals)

op <- par(mfrow = c(1, 2))
plot(d_df_con, main = '"continuous" d-function\nfrom data frame')
plot(d_df_dis, main = '"discrete" d-function\nfrom data frame', col = "blue")

par(op)

## Q-functions

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

### From data frame

q_df_dis <- new_q(dis_df, type = "discrete")
q_df_dis
#> Quantile function of discrete type
#> Support: [1, 4] (4 elements)

q_df_con <- new_q(con_df, type = "continuous")
q_df_con
#> Quantile function of continuous type
#> Support: [1, 4] (3 intervals)

plot(q_df_con, main = "Q-functions from data frame")
lines(q_df_dis, col = "blue")

## 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 data frame

r_df_dis <- new_r(dis_df, type = "discrete")
r_df_dis
#> Random generation function of discrete type
#> Support: [1, 4] (4 elements)

r_df_con <- new_r(con_df, type = "continuous")
r_df_con
#> Random generation function of continuous type
#> Support: [1, 4] (3 intervals)

op <- par(mfrow = c(1, 2))
plot(r_df_con, main = '"continuous" r-function\nfrom data frame')
plot(r_df_dis, main = '"discrete" r-function\nfrom data frame', col = "blue")

par(op)

## 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: