A wrapper for creating a vector of distributions.
distr6::Distribution
-> distr6::DistributionWrapper
-> VectorDistribution
modelTable
Returns reference table of wrapped Distributions.
distlist
Returns list of constructed wrapped Distributions.
ids
Returns ids of constructed wrapped Distributions.
new()
Creates a new instance of this R6 class.
VectorDistribution$new( distlist = NULL, distribution = NULL, params = NULL, shared_params = NULL, name = NULL, short_name = NULL, decorators = NULL, vecdist = NULL, ids = NULL, ... )
distlist
(list())
List of Distributions.
distribution
(character(1))
Should be supplied with params
and optionally shared_params
as an alternative to distlist
.
Much faster implementation when only one class of distribution is being wrapped. distribution
is the full name of one of the distributions in listDistributions()
, or "Distribution"
if
constructing custom distributions. See examples in VectorDistribution.
params
(list()|data.frame())
Parameters in the individual distributions for use with distribution
. Can be supplied as a list,
where each element is the list of parameters to set in the distribution, or as an object
coercable to data.frame
, where each column is a parameter and each row is a distribution.
See examples in VectorDistribution.
shared_params
(list())
If any parameters are shared when using the distribution
constructor, this provides a much faster
implementation to list and query them together. See examples in VectorDistribution.
name
(character(1))
Optional name of wrapped distribution.
short_name
(character(1))
Optional short name/ID of wrapped distribution.
decorators
(character())
Decorators to add to the distribution during construction.
vecdist
VectorDistribution Alternative constructor to directly create this object from an object inheriting from VectorDistribution.
ids
(character())
Optional ids for wrapped distributions in vector, should be unique and of same length as
the number of distributions.
...
Unused
\dontrun{ VectorDistribution$new( distribution = "Binomial", params = list( list(prob = 0.1, size = 2), list(prob = 0.6, size = 4), list(prob = 0.2, size = 6) ) )
VectorDistribution$new( distribution = "Binomial", params = data.table::data.table(prob = c(0.1, 0.6, 0.2), size = c(2, 4, 6)) )
# Alternatively VectorDistribution$new( list( Binomial$new(prob = 0.1, size = 2), Binomial$new(prob = 0.6, size = 4), Binomial$new(prob = 0.2, size = 6) ) ) }
getParameterValue()
Returns the value of the supplied parameter.
VectorDistribution$getParameterValue(id, ...)
id
character()
id of parameter value to return.
...
Unused
wrappedModels()
Returns model(s) wrapped by this wrapper.
VectorDistribution$wrappedModels(model = NULL)
model
(character(1))
id of wrapped Distributions to return. If NULL
(default), a list of all wrapped
Distributions is returned; if only one Distribution is matched then this is returned,
otherwise a list of Distributions.
strprint()
Printable string representation of the VectorDistribution
. Primarily used internally.
VectorDistribution$strprint(n = 10)
n
(integer(1))
Number of distributions to include when printing.
mean()
Returns named vector of means from each wrapped Distribution.
VectorDistribution$mean(...)
...
Passed to CoreStatistics$genExp
if numeric.
mode()
Returns named vector of modes from each wrapped Distribution.
VectorDistribution$mode(which = "all")
which
(character(1) | numeric(1)
Ignored if distribution is unimodal. Otherwise "all"
returns all modes, otherwise specifies
which mode to return.
median()
Returns named vector of medians from each wrapped Distribution.
VectorDistribution$median()
variance()
Returns named vector of variances from each wrapped Distribution.
VectorDistribution$variance(...)
...
Passed to CoreStatistics$genExp
if numeric.
skewness()
Returns named vector of skewness from each wrapped Distribution.
VectorDistribution$skewness(...)
...
Passed to CoreStatistics$genExp
if numeric.
kurtosis()
Returns named vector of kurtosis from each wrapped Distribution.
VectorDistribution$kurtosis(excess = TRUE, ...)
excess
(logical(1))
If TRUE
(default) excess kurtosis returned.
...
Passed to CoreStatistics$genExp
if numeric.
entropy()
Returns named vector of entropy from each wrapped Distribution.
VectorDistribution$entropy(base = 2, ...)
base
(integer(1))
Base of the entropy logarithm, default = 2 (Shannon entropy)
...
Passed to CoreStatistics$genExp
if numeric.
mgf()
Returns named vector of mgf from each wrapped Distribution.
VectorDistribution$mgf(t, ...)
t
(integer(1))
t integer to evaluate function at.
...
Passed to CoreStatistics$genExp
if numeric.
cf()
Returns named vector of cf from each wrapped Distribution.
VectorDistribution$cf(t, ...)
t
(integer(1))
t integer to evaluate function at.
...
Passed to CoreStatistics$genExp
if numeric.
pgf()
Returns named vector of pgf from each wrapped Distribution.
VectorDistribution$pgf(z, ...)
z
(integer(1))
z integer to evaluate probability generating function at.
...
Passed to CoreStatistics$genExp
if numeric.
pdf()
Returns named vector of pdfs from each wrapped Distribution.
VectorDistribution$pdf(..., log = FALSE, simplify = TRUE, data = NULL)
...
(numeric())
Points to evaluate the function at Arguments do not need
to be named. The length of each argument corresponds to the number of points to evaluate,
the number of arguments corresponds to the number of variables in the distribution.
See examples.
log
(logical(1))
If TRUE
returns the logarithm of the probabilities. Default is FALSE
.
simplify
logical(1)
If TRUE
(default) simplifies the return if possible to a numeric
, otherwise returns a
data.table::data.table.
data
array Alternative method to specify points to evaluate. If univariate then rows correspond with number of points to evaluate and columns correspond with number of variables to evaluate. In the special case of VectorDistributions of multivariate distributions, then the third dimension corresponds to the distribution in the vector to evaluate.
vd <- VectorDistribution$new( distribution = "Binomial", params = data.frame(size = 9:10, prob = c(0.5,0.6)))
vd$pdf(2) # Equivalently vd$pdf(2, 2)
vd$pdf(1:2, 3:4) # or as a matrix vd$pdf(data = matrix(1:4, nrow = 2))
# when wrapping multivariate distributions, arrays are required vd <- VectorDistribution$new( distribution = "Multinomial", params = list( list(size = 5, probs = c(0.1, 0.9)), list(size = 8, probs = c(0.3, 0.7)) ) )
# evaluates Multinom1 and Multinom2 at (1, 4) vd$pdf(1, 4)
# evaluates Multinom1 at (1, 4) and Multinom2 at (5, 3) vd$pdf(data = array(c(1,4,5,3), dim = c(1,2,2)))
# and the same across many samples vd$pdf(data = array(c(1,2,4,3,5,1,3,7), dim = c(2,2,2)))
cdf()
Returns named vector of cdfs from each wrapped Distribution.
Same usage as $pdf.
VectorDistribution$cdf( ..., lower.tail = TRUE, log.p = FALSE, simplify = TRUE, data = NULL )
...
(numeric())
Points to evaluate the function at Arguments do not need
to be named. The length of each argument corresponds to the number of points to evaluate,
the number of arguments corresponds to the number of variables in the distribution.
See examples.
lower.tail
(logical(1))
If TRUE
(default), probabilities are X <= x
, otherwise, P(X > x)
.
log.p
(logical(1))
If TRUE
returns the logarithm of the probabilities. Default is FALSE
.
simplify
logical(1)
If TRUE
(default) simplifies the return if possible to a numeric
, otherwise returns a
data.table::data.table.
data
array Alternative method to specify points to evaluate. If univariate then rows correspond with number of points to evaluate and columns correspond with number of variables to evaluate. In the special case of VectorDistributions of multivariate distributions, then the third dimension corresponds to the distribution in the vector to evaluate.
quantile()
Returns named vector of quantiles from each wrapped Distribution.
Same usage as $cdf.
VectorDistribution$quantile( ..., lower.tail = TRUE, log.p = FALSE, simplify = TRUE, data = NULL )
...
(numeric())
Points to evaluate the function at Arguments do not need
to be named. The length of each argument corresponds to the number of points to evaluate,
the number of arguments corresponds to the number of variables in the distribution.
See examples.
lower.tail
(logical(1))
If TRUE
(default), probabilities are X <= x
, otherwise, P(X > x)
.
log.p
(logical(1))
If TRUE
returns the logarithm of the probabilities. Default is FALSE
.
simplify
logical(1)
If TRUE
(default) simplifies the return if possible to a numeric
, otherwise returns a
data.table::data.table.
data
array Alternative method to specify points to evaluate. If univariate then rows correspond with number of points to evaluate and columns correspond with number of variables to evaluate. In the special case of VectorDistributions of multivariate distributions, then the third dimension corresponds to the distribution in the vector to evaluate.
rand()
Returns data.table::data.table of draws from each wrapped Distribution.
VectorDistribution$rand(n, simplify = TRUE)
n
(numeric(1))
Number of points to simulate from the distribution. If length greater than \(1\), then
n <- length(n)
,
simplify
logical(1)
If TRUE
(default) simplifies the return if possible to a numeric
, otherwise returns a
data.table::data.table.
clone()
The objects of this class are cloneable with this method.
VectorDistribution$clone(deep = FALSE)
deep
Whether to make a deep clone.
A vector distribution is intented to vectorize distributions more efficiently than storing a list of distributions. To improve speed and reduce memory usage, distributions are only constructed when methods (e.g. d/p/q/r) are called.
Other wrappers:
Convolution
,
DistributionWrapper
,
HuberizedDistribution
,
MixtureDistribution
,
ProductDistribution
,
TruncatedDistribution
# NOT RUN {
## ------------------------------------------------
## Method `VectorDistribution$new`
## ------------------------------------------------
# }
# NOT RUN {
VectorDistribution$new(
distribution = "Binomial",
params = list(
list(prob = 0.1, size = 2),
list(prob = 0.6, size = 4),
list(prob = 0.2, size = 6)
)
)
VectorDistribution$new(
distribution = "Binomial",
params = data.table::data.table(prob = c(0.1, 0.6, 0.2), size = c(2, 4, 6))
)
# Alternatively
VectorDistribution$new(
list(
Binomial$new(prob = 0.1, size = 2),
Binomial$new(prob = 0.6, size = 4),
Binomial$new(prob = 0.2, size = 6)
)
)
# }
# NOT RUN {
## ------------------------------------------------
## Method `VectorDistribution$pdf`
## ------------------------------------------------
vd <- VectorDistribution$new(
distribution = "Binomial",
params = data.frame(size = 9:10, prob = c(0.5,0.6)))
vd$pdf(2)
# Equivalently
vd$pdf(2, 2)
vd$pdf(1:2, 3:4)
# or as a matrix
vd$pdf(data = matrix(1:4, nrow = 2))
# when wrapping multivariate distributions, arrays are required
vd <- VectorDistribution$new(
distribution = "Multinomial",
params = list(
list(size = 5, probs = c(0.1, 0.9)),
list(size = 8, probs = c(0.3, 0.7))
)
)
# evaluates Multinom1 and Multinom2 at (1, 4)
vd$pdf(1, 4)
# evaluates Multinom1 at (1, 4) and Multinom2 at (5, 3)
vd$pdf(data = array(c(1,4,5,3), dim = c(1,2,2)))
# and the same across many samples
vd$pdf(data = array(c(1,2,4,3,5,1,3,7), dim = c(2,2,2)))
# }
Run the code above in your browser using DataLab