A collection of S4 classes that provide a flexible and structured way to work with probability distributions.
Value
Each type of function returns a different type of object:
Distribution Functions: When supplied with one argument (
distr), thed(),p(),q(),r(),ll()functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (distrandx), they evaluate the aforementioned functions directly.Moments: Returns a numeric, either vector or matrix depending on the moment and the distribution. The
moments()function returns a list with all the available methods.Estimation: Returns a list. The estimator of the unknown parameters. Note that in distribution families like the binomial, multinomial, and negative binomial, the size is not returned, since it is considered known.
Variance: Returns a named matrix. The asymptotic covariance matrix of the estimator.
Details
These S4 generic methods can work both as functions and as functionals
(functions that return functions). The available distribution families are
coded as S4 classes, specifically subclasses of the Distribution
superclass. The methods can be used in two ways:
Option 1: If both the distr argument and x or n are supplied, then the
function is evaluated directly, as usual.
Option 2: If only the distr argument is supplied, the method returns a
function that takes as input the missing argument x or n, allowing the
user to work with the function object itself. See examples.
Looking for a specific distribution family? This help page is general. Use the help page of each distribution to see the available methods for the class, details, and examples. Check the See Also section.
Functions
d(): density functionp(): cumulative distribution functionqn(): generalized inverse distribution functionr(): random sample generator function
Examples
# -----------------------------------------------------
# Beta Distribution Example
# -----------------------------------------------------
# Create the distribution
D <- Beta(3, 5)
x <- c(0.3, 0.8, 0.5)
n <- 5
# Density function
df <- d(D) ; df(x) # df is a function itself
#> [1] 2.268945 0.107520 1.640625
d(D, x) # alternative way to use the function
#> [1] 2.268945 0.107520 1.640625
# Distribution function
pf <- p(D) ; pf(x)
#> [1] 0.3529305 0.9953280 0.7734375
p(D, x)
#> [1] 0.3529305 0.9953280 0.7734375
# Inverse distribution function
qf <- qn(D) ; qf(x)
#> [1] 0.2763397 0.5167578 0.3641161
qn(D, x)
#> [1] 0.2763397 0.5167578 0.3641161
# Random Generator function
rf <- r(D) ; rf(n)
#> [1] 0.4485475 0.1568602 0.3561605 0.2105236 0.3718898
r(D, n)
#> [1] 0.2277145 0.1299583 0.5743616 0.4101034 0.4237988