[R] Random sample from truncated distributions

Petr Savicky savicky at cs.cas.cz
Wed Feb 8 18:21:13 CET 2012

On Wed, Feb 08, 2012 at 06:17:40AM -0800, n wrote:
> Hi,
> How can I draw a random sample from a truncated distribution (especially
> lognormal)?
> I found the functions for truncated normal but not for many other
> distributions.


A variable Y with a log-normal distribution may be obtained as
Y = exp(X), where X has a normal distribution. Since exp() is
monotone, a truncated Y may be generated as exp(X'), where X' has
an appropriate truncated normal distribution.

A general method for continuous distributions uses the inverse of
the distribution function. If p(x) is the distribution function
of some distribution and f(x) is its inverse function, then the
truncated distribution to an interval [a, b] may be obtained as

   f(runif(n, min=p(a), max=p(b)))

Hope this helps.

Petr Savicky.

More information about the R-help mailing list