Title: | Convolute Probabilistic Distributions |
---|---|
Description: | Convolute probabilistic distributions using the random generator function of each distribution. A new random number generator function is created that perform the mathematical operation on the individual random samples from the random generator function of each distribution. See the documentation for examples. |
Authors: | Aponte John [aut, cre] |
Maintainer: | Aponte John <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.6.2.9000 |
Built: | 2024-11-01 04:41:43 UTC |
Source: | https://github.com/johnaponte/convdistr |
This function returns a DISTRIBUTION
with a new dimension
created by row sum of the dimensions of the distribution.
add_total(p_distribution, p_totalname = "TOTAL")
add_total(p_distribution, p_totalname = "TOTAL")
p_distribution |
an object of class |
p_totalname |
the name of the new dimension |
Only works with multidimensional distributions.
John J. Aponte
d1 <- new_DIRICHLET(c(0.2,0.5,0.3)) d2 <- add_total(d1)
d1 <- new_DIRICHLET(c(0.2,0.5,0.3)) d2 <- add_total(d1)
Returns an BETA distribution object that produce random numbers
from a beta distribution using the rbeta
function
new_BETA(p_shape1, p_shape2, p_dimnames = "rvar") new_BETA_lci(p_mean, p_lci, p_uci, p_dimnames = "rvar") new_BETA_lci2(p_mean, p_lci, p_uci, p_dimnames = "rvar")
new_BETA(p_shape1, p_shape2, p_dimnames = "rvar") new_BETA_lci(p_mean, p_lci, p_uci, p_dimnames = "rvar") new_BETA_lci2(p_mean, p_lci, p_uci, p_dimnames = "rvar")
p_shape1 |
non-negative parameters of the Beta distribution |
p_shape2 |
non-negative parameters of the Beta distribution |
p_dimnames |
A character that represents the name of the dimension |
p_mean |
A numeric that represents the expected value of the proportion |
p_lci |
A numeric for the lower 95% confidence interval |
p_uci |
A numeric for the upper 95% confidence interval |
An object of class DISTRIBUTION
, BETA
new_BETA_lci()
: Constructor based on confidence intervals. Preserve expected value.
new_BETA_lci2()
: Constructor based on ML confidence intervals
When using confidence intervals, the shape parameters are obtained using the following formula:
new_BETA_lci2 estimate parameters using maximum likelihood myDistr <- new_BETA_lci2(0.30,0.25,0.35) myDistr$rfunc(10)
John J. Aponte
myDistr <- new_BETA(1,1) myDistr$rfunc(10) myDistr <- new_BETA_lci(0.30,0.25,0.35) myDistr$rfunc(10)
myDistr <- new_BETA(1,1) myDistr$rfunc(10) myDistr <- new_BETA_lci(0.30,0.25,0.35) myDistr$rfunc(10)
Returns an BETABINOMIAL distribution object that produce random numbers
from a betabinomial distribution using the rbbinom
function
new_BETABINOMIAL(p_size, p_shape1, p_shape2, p_dimnames = "rvar") new_BETABINOMIAL_od(p_size, p_mu, p_od, p_dimnames = "rvar") new_BETABINOMIAL_icc(p_size, p_mu, p_icc, p_dimnames = "rvar")
new_BETABINOMIAL(p_size, p_shape1, p_shape2, p_dimnames = "rvar") new_BETABINOMIAL_od(p_size, p_mu, p_od, p_dimnames = "rvar") new_BETABINOMIAL_icc(p_size, p_mu, p_icc, p_dimnames = "rvar")
p_size |
a non-negative parameter for the number of trials |
p_shape1 |
non-negative parameters of the Betabinomial distribution |
p_shape2 |
non-negative parameters of the Betabinomial distribution |
p_dimnames |
A character that represents the name of the dimension |
p_mu |
mean proportion for the binomial part of the distribution |
p_od |
over dispersion parameter |
p_icc |
intra-class correlation parameter |
An object of class DISTRIBUTION
, BETADISTRIBUION
new_BETABINOMIAL_od()
: parametrization based on dispersion
new_BETABINOMIAL_icc()
: parametrization based on intra-class correlation
There are several parametrization for the betabinomial distribution. The one based on shape1 and shape2 are parameters alpha and beta of the beta part of the distribution, but it can be parametrized as mu, and od where mu is the expected mean proportion and od is a measure of the overdispersion.
Another parametrization is based on mu and the icc where mu is the mean proportion and icc is the intra-class correlation.
John J. Aponte
myDistr <- new_BETABINOMIAL(10,1,1) myDistr$rfunc(10)
myDistr <- new_BETABINOMIAL(10,1,1) myDistr$rfunc(10)
Returns a BINOMIAL distribution object that produce random numbers
from a binomial distribution using the rbinom
function
new_BINOMIAL(p_size, p_prob, p_dimnames = "rvar")
new_BINOMIAL(p_size, p_prob, p_dimnames = "rvar")
p_size |
integer that represent the number of trials |
p_prob |
probability of success |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, BINOMIAL
John J. Aponte
myDistr <- new_BINOMIAL(1000,0.3) myDistr$rfunc(10)
myDistr <- new_BINOMIAL(1000,0.3) myDistr$rfunc(10)
Produce 5 numbers of the distribution (mean_, sd_, lci_, uci_, median_).
cinqnum(x, ...) ## S3 method for class 'DISTRIBUTION' cinqnum(x, n, ...) ## S3 method for class ''NA'' cinqnum(x, n, ...) ## S3 method for class 'DIRAC' cinqnum(x, n, ...)
cinqnum(x, ...) ## S3 method for class 'DISTRIBUTION' cinqnum(x, n, ...) ## S3 method for class ''NA'' cinqnum(x, n, ...) ## S3 method for class 'DIRAC' cinqnum(x, n, ...)
x |
an object of class |
... |
further parameters |
n |
number of drawns |
Uses the stored seed to have the same sequence always and produce the same numbers This is an internal function for the summary function
a vector with the mean, sd, lci, uci and median values
cinqnum(DISTRIBUTION)
: Generic method for a DISTRIBUTION
cinqnum(`NA`)
: Generic method for optimized for a NA distribution
cinqnum(DIRAC)
: Generic method optimized for a DIRAC distribution
John J. Aponte
DISTRIBUTION
objectsThe convolution of the simple algebraic operations is made by the operation of individual drawns of
the distributions. The DISTRIBUTION
objects must have the
same dimensions.
new_CONVOLUTION(listdistr, op, omit_NA = FALSE) new_SUM(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 + e2 new_SUBTRACTION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 - e2 new_MULTIPLICATION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 * e2 new_DIVISION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 / e2
new_CONVOLUTION(listdistr, op, omit_NA = FALSE) new_SUM(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 + e2 new_SUBTRACTION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 - e2 new_MULTIPLICATION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 * e2 new_DIVISION(..., omit_NA = FALSE) ## S3 method for class 'DISTRIBUTION' e1 / e2
listdistr |
a list of |
op |
a function to convolute '+', '-', '*', '\' |
omit_NA |
if TRUE, |
... |
|
e1 |
object of class |
e2 |
object of class |
If any of the distributions is of class NA
(NA_DISTRIBUTION
)
the result will be a new distribution of class NA
unless the
omit_NA
option is set to TRUE
and object of class CONVOLUTION
, DISTRIBUTION
new_SUM()
: Sum of distributions
new_SUBTRACTION()
: Subtraction for distributions
new_MULTIPLICATION()
: Multiplication for distributions
new_DIVISION()
: DIVISION for distributions
John J. Aponte
x1 <- new_NORMAL(0,1) x2 <- new_UNIFORM(1,2) new_CONVOLUTION(list(x1,x2), `+`) new_SUM(x1,x2) x1 + x2 new_SUBTRACTION(x1,x2) x1 - x2 new_MULTIPLICATION(list(x1,x2)) x1 * x2 new_DIVISION(list(x1,x2)) x1 / x2
x1 <- new_NORMAL(0,1) x2 <- new_UNIFORM(1,2) new_CONVOLUTION(list(x1,x2), `+`) new_SUM(x1,x2) x1 + x2 new_SUBTRACTION(x1,x2) x1 - x2 new_MULTIPLICATION(list(x1,x2)) x1 * x2 new_DIVISION(list(x1,x2)) x1 / x2
In case of different dimensions of the distribution this function perform the operation on the common distributions and add without modifications the other dimensions of the distribution.
new_CONVOLUTION_assoc(dist1, dist2, op) new_SUM_assoc(dist1, dist2) new_SUBTRACTION_assoc(dist1, dist2) new_MULTIPLICATION_assoc(dist1, dist2) new_DIVISION_assoc(dist1, dist2)
new_CONVOLUTION_assoc(dist1, dist2, op) new_SUM_assoc(dist1, dist2) new_SUBTRACTION_assoc(dist1, dist2) new_MULTIPLICATION_assoc(dist1, dist2) new_DIVISION_assoc(dist1, dist2)
dist1 |
an object of class |
dist2 |
and object of class |
op |
one of '+','-','*','/' |
If distribution A have dimensions a and b and distribution B have dimensions b and c, the A + B would produce a distribution with dimensions a, c, b+b,
an object of class DISTRIBUTION
new_SUM_assoc()
: Sum of distributions
new_SUBTRACTION_assoc()
: Subtraction of distributions
new_MULTIPLICATION_assoc()
: Multiplication of distributions
new_DIVISION_assoc()
: Division of distributions
John J. Aponte
x1 <- new_MULTINORMAL(c(0,1), matrix(c(1,0.5,0.5,1),ncol=2), p_dimnames = c("A","B")) x2 <- new_MULTINORMAL(c(10,1), matrix(c(1,0.4,0.4,1),ncol=2), p_dimnames = c("B","C")) new_CONVOLUTION_assoc(x1,x2, `+`) new_SUM_assoc(x1,x2) new_SUBTRACTION_assoc(x1,x2) new_MULTIPLICATION_assoc(x1,x2) new_DIVISION_assoc(x1,x2)
x1 <- new_MULTINORMAL(c(0,1), matrix(c(1,0.5,0.5,1),ncol=2), p_dimnames = c("A","B")) x2 <- new_MULTINORMAL(c(10,1), matrix(c(1,0.4,0.4,1),ncol=2), p_dimnames = c("B","C")) new_CONVOLUTION_assoc(x1,x2, `+`) new_SUM_assoc(x1,x2) new_SUBTRACTION_assoc(x1,x2) new_MULTIPLICATION_assoc(x1,x2) new_DIVISION_assoc(x1,x2)
In case of different dimensions of the distribution this function perform the operation on the combination of the distributions of both distribution.
new_CONVOLUTION_comb(dist1, dist2, op, p_dimnames) new_SUM_comb(dist1, dist2) new_SUBTRACTION_comb(dist1, dist2) new_MULTIPLICATION_comb(dist1, dist2) new_DIVISION_comb(dist1, dist2)
new_CONVOLUTION_comb(dist1, dist2, op, p_dimnames) new_SUM_comb(dist1, dist2) new_SUBTRACTION_comb(dist1, dist2) new_MULTIPLICATION_comb(dist1, dist2) new_DIVISION_comb(dist1, dist2)
dist1 |
an object of class |
dist2 |
and object of class |
op |
one of '+','-','*','/' |
p_dimnames |
a character vector with the name of the dimensions. If missing the combination of the individual dimensions will be used |
If distribution A have dimensions a and b and distribution B have dimensions b and c, the A + B would produce a distribution with dimensions a_b,a_c,b_b, b_c
an object of class DISTRIBUTION
new_SUM_comb()
: Sum of distributions
new_SUBTRACTION_comb()
: Subtraction of distributions
new_MULTIPLICATION_comb()
: Multiplication of distributions
new_DIVISION_comb()
: Division of distributions
In case of the same dimensions, only the first combination is taken
John J. Aponte
x1 <- new_MULTINORMAL(c(0,1), matrix(c(1,0.5,0.5,1),ncol=2), p_dimnames = c("A","B")) x2 <- new_MULTINORMAL(c(10,1), matrix(c(1,0.4,0.4,1),ncol=2), p_dimnames = c("B","C")) new_CONVOLUTION_comb(x1,x2, `+`) new_SUM_comb(x1,x2) new_SUBTRACTION_comb(x1,x2) new_MULTIPLICATION_comb(x1,x2) new_DIVISION_comb(x1,x2)
x1 <- new_MULTINORMAL(c(0,1), matrix(c(1,0.5,0.5,1),ncol=2), p_dimnames = c("A","B")) x2 <- new_MULTINORMAL(c(10,1), matrix(c(1,0.4,0.4,1),ncol=2), p_dimnames = c("B","C")) new_CONVOLUTION_comb(x1,x2, `+`) new_SUM_comb(x1,x2) new_SUBTRACTION_comb(x1,x2) new_MULTIPLICATION_comb(x1,x2) new_DIVISION_comb(x1,x2)
Returns an DIRAC distribution object that always return the same number, or the same matrix of numbers in case multiple dimensions are setup
new_DIRAC(p_scalar, p_dimnames = "rvar")
new_DIRAC(p_scalar, p_dimnames = "rvar")
p_scalar |
A numeric that set the value for the distribution |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, DIRAC
John J. Aponte
myDistr <- new_DIRAC(1) myDistr$rfunc(10)
myDistr <- new_DIRAC(1) myDistr$rfunc(10)
Returns an DIRICHLET
distribution object that draw random numbers
generated by the function rdirichlet
new_DIRICHLET(p_alpha, p_dimnames)
new_DIRICHLET(p_alpha, p_dimnames)
p_alpha |
k-value vector for concentration parameter. Must be positive |
p_dimnames |
A vector of characters for the names of the k-dimensions |
A name can be provided for the dimensions. Otherwise rvar1
,
rvar2
, ..., rvark
will be assigned
An object of class DISTRIBUTION
,
p_distribution$distribution
, TRUNCATED
John J. Aponte
myDistr <- new_DIRICHLET(c(0.3,0.2,0.5), c("a","b","c")) myDistr$rfunc(10)
myDistr <- new_DIRICHLET(c(0.3,0.2,0.5), c("a","b","c")) myDistr$rfunc(10)
Returns an DISCRETE distribution object that sample from the vector p_supp
of
options with probability the vector of probabilities p_prob
.
new_DISCRETE(p_supp, p_prob, p_dimnames = "rvar")
new_DISCRETE(p_supp, p_prob, p_dimnames = "rvar")
p_supp |
A numeric vector of options |
p_prob |
A numeric vector of probabilities. |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, DISCRETE
If the second argument is missing, all options will be sample with equal probability. If provided, the second argument would add to 1 and must be the same length that the first argument
John J. Aponte
myDistr <- new_DISCRETE(p_supp=c(1,2,3,4), p_prob=c(0.40,0.30,0.20,0.10)) myDistr$rfunc(10)
myDistr <- new_DISCRETE(p_supp=c(1,2,3,4), p_prob=c(0.40,0.30,0.20,0.10)) myDistr$rfunc(10)
DISTRIBUTION is a kind of abstract class (or interface) that the specific constructors should implement.
It contains 4 fields
A character with the name of the distribution implemented
A numerical that is used for details
to produce
reproducible details of the distribution
Observed value. Is the value expected. It is used as a number for the mathematical operations of the distributions as if they were a simple scalar
A function that generate random numbers from the distribution.
Its only parameter n
is the number of draws of the distribution.
It returns a matrix with as many rows as n, and as many columns as the
dimensions of the distributions
The DISTRIBUTION objects could support multidimensional distributions
for example DIRICHLET
. The names of the dimensions
should coincides with the names of the oval
vector.
If only one dimension, the default name is rvar
.
It is expected that the rfunc
is included in the creation of new
distributions by convolution so the environment should be carefully controlled
to avoid reference leaking that is possible within the R language. For that
reason, rfunc
should be created within a restrict_environment
function
Once the object is instanced, the fields are immutable and should not be
changed. If the seed needs to be modified, a new object can be created using
the set_seed
function
Objects are defined for the following distributions
a DISTRIBUTION object
John J. Aponte
DISTRIBUTION
classesGenerate a function that creates DISTRIBUTION
objects
DISTRIBUTION_factory(distname, rfunction, ovalfunc)
DISTRIBUTION_factory(distname, rfunction, ovalfunc)
distname |
name of the distribution. By convention they are upper case |
rfunction |
a function to generate random numbers from the distribution |
ovalfunc |
a function that calculate the oval value, should used only
the same arguments that the |
A function that is able to create DISTRIBUTION
objects.
The function return a new function, that have as arguments the formals
of the rfunction
plus a new argument dimnames
for the dimension
names. If The distribution is unidimensional, the default value
dimnames = "rvar"
will works well, but if not, the dimnames
argument should be specified when the generated function is used as in
the example for the new_MyDIRICHLET
John J. Aponte
new_MYDISTR <- DISTRIBUTION_factory("MYDISTR", rnorm, function(){mean}) d1 <- new_MYDISTR(0,1) summary(d1) require(extraDistr) new_MyDIRICHLET <- DISTRIBUTION_factory('rdirichlet', rdirichlet, function() { salpha = sum(alpha) alpha / salpha }) d2 <- new_MyDIRICHLET(c(10, 20, 70), dimnames = c("A", "B", "C")) summary(d2)
new_MYDISTR <- DISTRIBUTION_factory("MYDISTR", rnorm, function(){mean}) d1 <- new_MYDISTR(0,1) summary(d1) require(extraDistr) new_MyDIRICHLET <- DISTRIBUTION_factory('rdirichlet', rdirichlet, function() { salpha = sum(alpha) alpha / salpha }) d2 <- new_MyDIRICHLET(c(10, 20, 70), dimnames = c("A", "B", "C")) summary(d2)
Returns an EXPONENTIAL distribution object that produce random numbers
from an exponential distribution using the rexp
function
new_EXPONENTIAL(p_rate, p_dimnames = "rvar")
new_EXPONENTIAL(p_rate, p_dimnames = "rvar")
p_rate |
A numeric that represents the rate of events |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, EXPONENTIAL
John J. Aponte
myDistr <- new_EXPONENTIAL(5) myDistr$rfunc(10)
myDistr <- new_EXPONENTIAL(5) myDistr$rfunc(10)
Fits a beta distribution based on quantiles
fitbeta_ml(point, lci, uci) fitbeta(point, lci, uci)
fitbeta_ml(point, lci, uci) fitbeta(point, lci, uci)
point |
Point estimates corresponding to the median |
lci |
Lower limit (quantile 0.025) |
uci |
Upper limit (quantile 0.975) |
parameters shape1 and shape2 of a beta distribution
fitbeta_ml()
: using ML to estimate parameters
fitbeta()
: preserve the expected value
This is a wrap of the fitdist
to obtain
the best parameters for a beta distribution based
on quantiles.
When using confidence intervals (not ML), the shape parameters are obtained using the following formula:
John J. Aponte
fitbeta_ml(0.45,0.40,0.50) fitbeta(0.45,0.40,0.50)
fitbeta_ml(0.45,0.40,0.50) fitbeta(0.45,0.40,0.50)
Fits a Dirichlet distribution based on the parameters of Beta distributions
fitdirichlet(..., plotBeta = FALSE, n.fitted = "opt")
fitdirichlet(..., plotBeta = FALSE, n.fitted = "opt")
... |
named vectors with the distribution parameters shape1, shape2 |
plotBeta |
if TRUE a ggplot of the densities are plotted |
n.fitted |
Method to fit the values |
Each one of the arguments is a named vector with values for shape1, shape2.
Values from fitbeta
are suitable for this.
This is a wrap of fitDirichlet
a vector with the parameters for a Dirichlet distribution
John J. Aponte
a <- fitbeta(0.3, 0.2, 0.4) c <- fitbeta(0.2, 0.1, 0.3) b <- fitbeta(0.5, 0.4, 0.6) fitdirichlet(cat1=a,cat2=b,cat3=c)
a <- fitbeta(0.3, 0.2, 0.4) c <- fitbeta(0.2, 0.1, 0.3) b <- fitbeta(0.5, 0.4, 0.6) fitdirichlet(cat1=a,cat2=b,cat3=c)
DISTRIBUTION
objects using ggplot2
Plot of DISTRIBUTION
objects using ggplot2
ggDISTRIBUTION(x, n = 10000)
ggDISTRIBUTION(x, n = 10000)
x |
an object of class |
n |
number of observation |
a ggplot
object with the density of the distribution
x <- new_NORMAL(0,1) ggDISTRIBUTION(x) y <- new_DIRICHLET(c(10,20,70)) ggDISTRIBUTION(x)
x <- new_NORMAL(0,1) ggDISTRIBUTION(x) y <- new_DIRICHLET(c(10,20,70)) ggDISTRIBUTION(x)
Returns a LOGNORMAL distribution object that produce random numbers
from a log normal distribution using the rlnorm
function
new_LOGNORMAL(p_meanlog, p_sdlog, p_dimnames = "rvar")
new_LOGNORMAL(p_meanlog, p_sdlog, p_dimnames = "rvar")
p_meanlog |
mean of the distribution on the log scale |
p_sdlog |
A numeric that represents the standard deviation on the log scale |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, LOGNORMAL
John J. Aponte
myDistr <- new_LOGNORMAL(0,1) myDistr$rfunc(10)
myDistr <- new_LOGNORMAL(0,1) myDistr$rfunc(10)
Shows the distribution and the oval values of a DISTRIBUTION
object
metadata(x) ## S3 method for class 'DISTRIBUTION' metadata(x) ## Default S3 method: metadata(x)
metadata(x) ## S3 method for class 'DISTRIBUTION' metadata(x) ## Default S3 method: metadata(x)
x |
a |
A data.frame
with the metadata of the distributions
metadata(DISTRIBUTION)
: Metadata for DISTRIBUTION objects
metadata(default)
: Metadata for other objects
The number of columns depends on the dimensions of the distribution.
There will be one column distribution
with the name of the distribution
and one column for each dimension with the names from the oval
field.
John J. Aponte
Returns an NA distribution object that always return NA_real_
This is useful to handle NA
.
By default only one dimension rvar
is produced, but if several
names are provided more columns will be added to the return matrix
new_NA(p_dimnames = "rvar")
new_NA(p_dimnames = "rvar")
p_dimnames |
A character that represents the the names of the
dimensions. By default only one dimension with name |
An object of class DISTRIBUTION
, NA
John J. Aponte
myDistr <- new_NA(p_dimnames = "rvar") myDistr$rfunc(10)
myDistr <- new_NA(p_dimnames = "rvar") myDistr$rfunc(10)
DISTRIBUTION
objectsProduce a new distribution that obtain random drawns of the mixture
of the DISTRIBUTION
objects
new_MIXTURE(listdistr, mixture)
new_MIXTURE(listdistr, mixture)
listdistr |
a list of |
mixture |
a vector of probabilities to mixture the distributions. Must add 1 If missing the drawns are obtained from the distributions with the same probability |
an object of class MIXTURE
, DISTRIBUTION
John J. Aponte
x1 <- new_NORMAL(0,1) x2 <- new_NORMAL(4,1) x3 <- new_NORMAL(6,1) new_MIXTURE(list(x1,x2,x3))
x1 <- new_NORMAL(0,1) x2 <- new_NORMAL(4,1) x3 <- new_NORMAL(6,1) new_MIXTURE(list(x1,x2,x3))
Return a DISTRIBUTION
object that draw random numbers from a
multivariate normal distribution using the mvrnorm
function.
new_MULTINORMAL(p_mu, p_sigma, p_dimnames, tol = 1e-06, empirical = FALSE)
new_MULTINORMAL(p_mu, p_sigma, p_dimnames, tol = 1e-06, empirical = FALSE)
p_mu |
a vector of means |
p_sigma |
a positive-definite symmetric matrix for the covariance matrix |
p_dimnames |
A character that represents the name of the dimension |
tol |
tolerance (relative to largest variance) for numerical lack of positive-definiteness in p_sigma. |
empirical |
logical. If true, mu and Sigma specify the empirical not population mean and covariance matrix. |
An object of class DISTRIBUTION
, MULTINORMAL
John J. Aponte
msigma <- matrix(c(1,0,0,1), ncol=2) d1 <- new_MULTINORMAL(c(0,1), msigma) rfunc(d1, 10)
msigma <- matrix(c(1,0,0,1), ncol=2) d1 <- new_MULTINORMAL(c(0,1), msigma) rfunc(d1, 10)
Returns a NORMAL distribution object that produce random numbers
from a normal distribution using the rnorm
function
new_NORMAL(p_mean, p_sd, p_dimnames = "rvar")
new_NORMAL(p_mean, p_sd, p_dimnames = "rvar")
p_mean |
A numeric that represents the mean value |
p_sd |
A numeric that represents the standard deviation |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, NORMAL
John J. Aponte
myDistr <- new_NORMAL(0,1) myDistr$rfunc(10)
myDistr <- new_NORMAL(0,1) myDistr$rfunc(10)
NA
distributions from a list of distributionsOmit NA
distributions from a list of distributions
omit_NA(listdistr)
omit_NA(listdistr)
listdistr |
a list of |
the list without the NA_DISTRIBUTION
John J. Aponte
DISTRIBUTION
objectsPlot an histogram of the density of the distribution using random numbers from the distribution
## S3 method for class 'DISTRIBUTION' plot(x, n = 10000, ...)
## S3 method for class 'DISTRIBUTION' plot(x, n = 10000, ...)
x |
an object of class |
n |
number of observations |
... |
other parameters to the |
No return value. Side effect plot the histogram.
x <- new_NORMAL(0,1) plot(x) y <- new_DIRICHLET(c(10,20,70)) plot(x)
x <- new_NORMAL(0,1) plot(x) y <- new_DIRICHLET(c(10,20,70)) plot(x)
Returns an POISSON distribution object that produce random numbers
from a Poisson distribution using the rpois
function
new_POISSON(p_lambda, p_dimnames = "rvar")
new_POISSON(p_lambda, p_dimnames = "rvar")
p_lambda |
A numeric that represents the expected number of events |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, POISSON
John J. Aponte
myDistr <- new_POISSON(5) myDistr$rfunc(10)
myDistr <- new_POISSON(5) myDistr$rfunc(10)
As standard feature, R include in the environment of a function all the variables that are available when the function is created. This, however is prompt to leak reference when you have a factory of function and they are created within a list.. it will include all the component of the list in the function environment. To prevent that, the random generator functions are encapsulated with a restricted environment where only the variables that the function requires to work are included
restrict_environment(f, ...)
restrict_environment(f, ...)
f |
input function |
... |
define the set of variables to be included as variable = value. |
new function with a restricted environment
John J. Aponte
a = 0 b = 1 myfunc <- restrict_environment( function(n) { rnorm(meanvalue, sdvalue) }, meanvalue = a, sdvalue = b) myfunc(10) ls(envir=environment(myfunc))
a = 0 b = 1 myfunc <- restrict_environment( function(n) { rnorm(meanvalue, sdvalue) }, meanvalue = a, sdvalue = b) myfunc(10) ls(envir=environment(myfunc))
DISTRIBUTION
objectThis is a generic method that calls the rfunc
slot of the object
rfunc(x, n)
rfunc(x, n)
x |
an object |
n |
the number of random samples |
a matrix with as many rows as n
and as many columns as
dimensions have distribution
John J. Aponte
Default function
## Default S3 method: rfunc(x, n)
## Default S3 method: rfunc(x, n)
x |
an object of class different from |
n |
the number of random samples |
No return value. Raise an error message.
John J. Aponte
DISTRIBUTION
objectGeneric function for a DISTRIBUTION
object
## S3 method for class 'DISTRIBUTION' rfunc(x, n)
## S3 method for class 'DISTRIBUTION' rfunc(x, n)
x |
an object of class |
n |
the number of random samples |
a matrix with as many rows as n
and as many columns as
John J. Aponte
Check the dimensions of a list of distributions
same_dimensions(listdistr)
same_dimensions(listdistr)
listdistr |
a list of |
return TRUE
if all the dimensions are the same
This create a new DISTRIBUTION
object but with the
specified seed
set_seed(distribution, seed)
set_seed(distribution, seed)
distribution |
a |
seed |
the new seed |
a DISTRIBUTION
object of the same class
John J. Aponte
Summary of Distributions
## S3 method for class 'DISTRIBUTION' summary(object, n = 10000, ...)
## S3 method for class 'DISTRIBUTION' summary(object, n = 10000, ...)
object |
object of class |
n |
the number of random samples from the distribution |
... |
other parameters. Not used |
A data.frame
with as many rows as dimensions had the
distribution and with the following columns
distribution name
varname name of the dimension
oval value
nsample number of random samples
mean_ mean value of the sample
sd_ standard deviation of the sample
lci_ lower 95
median_ median value of the sample
uci_ upper 95
The sample uses the seed saved in the object those it will
provide the same values fir an n
value
John J. Aponte
Returns an TRIANGULAR distribution object that produce random numbers
from a triangular distribution using the rtriang
function
new_TRIANGULAR(p_min, p_max, p_mode, p_dimnames = "rvar")
new_TRIANGULAR(p_min, p_max, p_mode, p_dimnames = "rvar")
p_min |
A numeric that represents the lower limit |
p_max |
A numeric that represents the upper limit |
p_mode |
A numeric that represents the mode |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, TRIANGULAR
John J. Aponte
myDistr <- new_TRIANGULAR(-1,1,0) myDistr$rfunc(10)
myDistr <- new_TRIANGULAR(-1,1,0) myDistr$rfunc(10)
Returns an TRUNCATED distribution object that limits the values that are
generated by the distribution to be in the limits p_min, p_max
new_TRUNCATED(p_distribution, p_min = -Inf, p_max = Inf)
new_TRUNCATED(p_distribution, p_min = -Inf, p_max = Inf)
p_distribution |
An object of class DISTRIBUTION to truncate |
p_min |
A numeric that set the lower limit of the distribution |
p_max |
A numeric that set the upper limit of the distribution |
An object of class DISTRIBUTION
,
p_distribution$distribution
, TRUNCATED
The expected value of a truncated distribution could be very
different from the expected value of the unrestricted distribution. Be
careful as the oval
field is not changed and may not represent
any more the expected value of the distribution.
If the distribution is multidimensional, the limits will apply to all dimensions.
John J. Aponte
myDistr <- new_TRUNCATED(p_distribution = new_NORMAL(0,1), p_min = -1, p_max = 1) myDistr$rfunc(10)
myDistr <- new_TRUNCATED(p_distribution = new_NORMAL(0,1), p_min = -1, p_max = 1) myDistr$rfunc(10)
Returns an UNIFORM distribution object that produce random numbers
from a uniform distribution using the runif
function
new_UNIFORM(p_min, p_max, p_dimnames = "rvar")
new_UNIFORM(p_min, p_max, p_dimnames = "rvar")
p_min |
A numeric that represents the lower limit |
p_max |
A numeric that represents the upper limit |
p_dimnames |
A character that represents the name of the dimension |
An object of class DISTRIBUTION
, UNIFORM
John J. Aponte
myDistr <- new_UNIFORM(0,1) myDistr$rfunc(10)
myDistr <- new_UNIFORM(0,1) myDistr$rfunc(10)