# Re: [R] Question

From: Matt Shotwell <shotwelm_at_musc.edu>
Date: Wed, 16 Jun 2010 15:17:22 -0400

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
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
>

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 16 Jun 2010 - 19:21:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 Jun 2010 - 19:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.