rng_distributions {hesim} | R Documentation |
Random number generation distributions
Description
A collection of functions for randomly generating deviates from probability
distributions with define_rng()
.
Usage
beta_rng(
shape1 = 1,
shape2 = 1,
mean = NULL,
sd = NULL,
names = NULL,
n = parent.frame()$n
)
dirichlet_rng(alpha, names = NULL, n = parent.frame()$n)
fixed(est, names = NULL, n = parent.frame()$n)
custom(x, names = NULL, n = parent.frame()$n)
gamma_rng(mean, sd, names = NULL, n = parent.frame()$n)
lognormal_rng(meanlog, sdlog, names = NULL, n = parent.frame()$n)
multi_normal_rng(mu, Sigma, names = NULL, n = parent.frame()$n, ...)
normal_rng(mean, sd, names = NULL, n = parent.frame()$n)
uniform_rng(min, max, names = NULL, n = parent.frame()$n)
Arguments
shape1 , shape2 |
Non-negative parameters of the Beta distribution. |
mean , sd |
Mean and standard deviation of the random variable. |
names |
Names for columns if an object with multiple columns is returned by the function. |
n |
The number of random samples of the parameters to draw. Default is
the value of |
alpha |
A matrix where each row is a separate vector of shape parameters. |
est |
A vector of estimates of the variable of interest. |
x |
A numeric |
meanlog , sdlog |
Mean and standard deviation of the distribution on the log scale. |
mu , Sigma |
|
... |
Additional arguments to pass to underlying random number generation functions. See "details". |
min , max |
Lower and upper limits of the distribution. Must be finite. |
Details
These functions are not exported and are meant for use with
define_rng()
. They consequently assume that the number of samples to draw, n
,
is defined in the parent environment. Convenience random number generation
functions include:
beta_rng()
If
mean
andsd
are both notNULL
, then parameters of the beta distribution are derived using the methods of moments withmom_beta()
. Beta variates are generated withstats::rbeta()
.custom()
Use previously sampled values from a custom probability distribution. There are three possibilities: (i) if
n
is equal to the number previously sampled values (sayn_samples
), thenx
is returned as is; (ii) ifn
<n_samples
, then samples fromx
are sampled without replacement; and (iii) ifn
>n_samples
, then samples fromx
are sampled with replacement and a warning is provided.dirichlet_rng()
Dirichlet variates for each row in the matrix are generated with
rdirichlet_mat()
. The sampled values are stored in adata.table
where there is a column for each element ofalpha
(with elements ordered rowwise).fixed()
This function should be used when values of the variable of interest are fixed (i.e., they are known with certainty). If
length(est) > 1
, ann
bylength(est)
data.table
is returned meaning that each element ofest
is repeatedn
times; otherwise (iflength(est) == 1
), a vector is returned whereest
is repeatedn
times.gamma_rng()
The parameters of the gamma distribution are derived using the methods of moments with
mom_gamma()
and gamma variates are generated withstats::rgamma()
.lognormal_rng()
Lognormal variates are generated with
stats::rlnorm()
.multi_normal_rng()
Multivariate normal variates are generated with
MASS::mvrnorm()
.normal_rng()
Normal variates are generated with
stats::rnorm()
.uniform_rng()
Uniform variates are generated with
stats::runif()
.
Value
Functions either return a vector of length n
or an n
by k
data.table
.
Multivariate distributions always return a data.table
. If a
univariate distribution is used, then a data.table
is returned if each
parameter is specified as a vector with length greater than 1; otherwise, if
parameters are scalars, then a vector is returned. In the data.table
case,
k
is equal to the length of the parameter vectors
entered as arguments. For example, if the probability distribution contained
mean
as an argument and mean
were
of length 3, then an n
by 3 matrix would be returned. The length of all
parameter vectors must be the same. For instance, if the vector mean
were of length 3 then all additional parameters (e.g., sd
)
must also be of length 3.
If a data.table
is returned by a distribution, then its column names are set
according to the following hierarchy:
With the
names
argument if it is notNULL
With the names of the parameter vectors if they are named vectors. If there are multiple parameter vector arguments, then the names of the first parameter vector with non
NULL
names is used. For instance, ifmean
andsd
are both arguments to a random number generation function andmean
is a named vector, then the names from the vectormean
are used.As
v1
, ...,vk
if thenames
argument isNULL
and there are no named parameter vectors.