# [R] Question

Matt Shotwell shotwelm at musc.edu
Wed Jun 16 21:17:22 CEST 2010

```Since there is a simple closed form for the truncated exponential CDF,
you can use inverse transform sampling. I believe this is quite common
in survival analysis methods. The first step is to compute and write an
R function to compute the inverse CDF for the truncated exponential,
say

itexp <- function(u, m, t) { -log(1-u*(1-exp(-t*m)))/m }

where u is the quantile, m is the rate, and t is the level of
truncation. Next, we draw from the truncated exponential with something
like

rtexp <- function(n, m, t) { itexp(runif(n), m, t) }

Check it out with

texp <- rtexp(10000,1,pi)
hist(texp)
summary(texp)

Matt Shotwell
Div. Biostatistics and Epidemiology
Medical University of South Carolina

On Wed, 2010-06-16 at 11:11 -0400, Joris Meys wrote:
> Two possibilities : rescale your random vector, or resample to get
> numbers within the range. But neither of these solutions will give you
> a true exponential distribution. I am not aware of truncated
> exponential distributions that are available in R, but somebody else
> might know more about that.
>
> # possibility I : rescaling
> rsample <- rexp(5)
> lim <- 0.8
> rsample <- rsample*lim/max(rsample)
> rsample
>
> # possibility II : resampling
> rsample <- rexp(5)
> while(sum(rsample>lim)>0) {
>   rsample <- ifelse(rsample>lim,rexp(length(rsample)),rsample)
> }
> rsample
>
> Cheers
> Joris
>
> On Wed, Jun 16, 2010 at 12:00 PM, Assieh Rashidi
> <assiehrashidi at yahoo.com> wrote:
> >
> > Dear Mr.
> > for writing program about Gibbs sampling, i have a question.
> > if i want to generate data from Exponential distribution but range of X is restricted, how can i do?
> > regards,
> > A.Rashidi
> >
> >
> >
> >
> >
> >        [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help