A wrapper for creating a vector of distributions.
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.
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.
Inherited methods
distr6::Distribution$confidence()
distr6::Distribution$correlation()
distr6::Distribution$iqr()
distr6::Distribution$liesInSupport()
distr6::Distribution$liesInType()
distr6::Distribution$parameters()
distr6::Distribution$prec()
distr6::Distribution$print()
distr6::Distribution$setParameterValue()
distr6::Distribution$stdev()
distr6::Distribution$summary()
distr6::Distribution$workingSupport()
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.
id
character()
id of parameter value to return.
...
Unused
wrappedModels()
Returns model(s) wrapped by this wrapper.
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.
mean()
Returns named vector of means from each wrapped Distribution.
...
Passed to CoreStatistics$genExp
if numeric.
mode()
Returns named vector of modes from each wrapped Distribution.
median()
Returns named vector of medians from each wrapped Distribution.
variance()
Returns named vector of variances from each wrapped Distribution.
...
Passed to CoreStatistics$genExp
if numeric.
skewness()
Returns named vector of skewness from each wrapped Distribution.
...
Passed to CoreStatistics$genExp
if numeric.
kurtosis()
Returns named vector of kurtosis from each wrapped Distribution.
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.
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.
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.
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.
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.
...
(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.
...
(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.
## ------------------------------------------------
## Method `VectorDistribution$new`
## ------------------------------------------------
if (FALSE) { # \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)
)
)
} # }
## ------------------------------------------------
## Method `VectorDistribution$pdf`
## ------------------------------------------------
vd <- VectorDistribution$new(
distribution = "Binomial",
params = data.frame(size = 9:10, prob = c(0.5,0.6)))
vd$pdf(2)
#> Binom1 Binom2
#> <num> <num>
#> 1: 0.0703125 0.01061683
# Equivalently
vd$pdf(2, 2)
#> Binom1 Binom2
#> <num> <num>
#> 1: 0.0703125 0.01061683
vd$pdf(1:2, 3:4)
#> Binom1 Binom2
#> <num> <num>
#> 1: 0.01757813 0.04246733
#> 2: 0.07031250 0.11147674
# or as a matrix
vd$pdf(data = matrix(1:4, nrow = 2))
#> Binom1 Binom2
#> <num> <num>
#> 1: 0.01757813 0.04246733
#> 2: 0.07031250 0.11147674
# 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)
#> Multinom1 Multinom2
#> <num> <num>
#> 1: 0.32805 0
# evaluates Multinom1 at (1, 4) and Multinom2 at (5, 3)
vd$pdf(data = array(c(1,4,5,3), dim = c(1,2,2)))
#> Multinom1 Multinom2
#> <num> <num>
#> 1: 0.32805 0.04667544
# and the same across many samples
vd$pdf(data = array(c(1,2,4,3,5,1,3,7), dim = c(2,2,2)))
#> Multinom1 Multinom2
#> <num> <num>
#> 1: 0.32805 0.04667544
#> 2: 0.07290 0.19765032