[R] truncated normal

David Brahm brahm at alum.mit.edu
Fri May 10 16:41:21 CEST 2002

Scott Summerill <scott.ctr.summerill at tc.faa.gov> wrote:
> Does anyone know of an R-function that will generate an observation from a
> truncated normal (left or right) with a mu and sigma2?  Any correspondence
> would be greatly appreciated.

I asked as similar question on S-news a while ago, and this is what I put
together from the replies.  Note "sd" is the standard deviation of the
underlying normal, not of the final truncated distribution.  "lo" and "hi" are
the cutoff points on either side.

rnbnd <- function(N, mn=0, sd=1, lo=-Inf, hi=Inf, tol=3) {
  if (hi-mn < tol*sd || mn-lo < tol*sd)               # Alan Zaslavsky's method
    return(mn + sd * qnorm(runif(N, pnorm((lo-mn)/sd), pnorm((hi-mn)/sd))))
  x <- rep(NA, N)                                     # Rejection method
  bnd <- function(z, lo, hi) ifelse(z<lo | z>hi, NA, z)
  while (any(q <- is.na(x))) x[q] <- bnd(rnorm(sum(q), mn, sd), lo, hi)

For efficiency, this function chooses between 2 methods.  If the truncation is
"severe", I generate the distribution from qnorm on a uniform distribution
(thanks originally to Alan Zaslavsky, and this was Thomas Lumley's approach as
well).  If the truncation is not severe (more than 3 sigma out), I use the
rejection method, just throwing out points in the truncated tails.  Good luck!
                              -- David Brahm (brahm at alum.mit.edu)
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list