Learn R Programming

mc2d (version 0.2.0)

rtrunc: Random Truncated Distributions

Description

Provides samples from classical R distributions and mc2d specific distributions truncated between linf (excluded) and lsup (included).

Usage

rtrunc(distr=runif, n, linf=-Inf, lsup=Inf, ...)

Value

A vector of n values.

Arguments

distr

A function providing random data or its name as character. The function 'rdistr' should have a 'qdistr' form (with argument 'p') and a 'pdistr' form (with argument 'q'). Example : 'rnorm' (has a 'qnorm' and a 'pnorm' form), 'rbeta', 'rbinom', 'rgamma', ...

n

The size of the sample.

.

linf

A vector of lower bounds.

lsup

A vector of upper bounds, with lsup < linf (strictly).

...

All arguments to be passed to pdistr and qdistr.

Details

The function 1) evaluates the p values corresponding to linf and lsup using pdistr; 2) samples n values using runif(n, min=pinf, max=psup), and 3) takes the n corresponding quantiles from the specified distribution using qdistr.

All distributions (but sample) implemented in the stats library could be used. The arguments in ... should be named. Do not use 'log' or 'log.p' or 'lower.tail'. For discrete distribution, rtrunc sample within (linf, lsup]. See example.

Examples

Run this code
rtrunc("rnorm", n=10, linf=0)
range(rtrunc(rnorm, n=1000, linf=3, lsup=5, sd=10))
## Discrete distributions
range(rtrunc(rpois, 1000, linf=2, lsup=4, lambda=1))
##Examples of rounding problems. 
##The first one will provide a warning while the results are unexpected, 
##The second will provide an error.
if (FALSE) {
table(rtrunc(rbinom, n=1000, size=10, prob=1-1E-20, lsup=9))
table(rtrunc(rbinom, n=1000, size=10, prob=1E-14, linf=0))
}

Run the code above in your browser using DataLab