Hi Everyone, I want to generate some random numbers according to some empirical distribution. Therefore I am looking for the inverse of an empirical cumulative distribution function. I haven't found any in R. Can anyone give a pointer? Thanks, Geoffrey _______________________________________________________=0A=0A=0AThe information in this email or in any file attached hereto...{{dropped}}
?quantile b On Jan 28, 2007, at 5:41 PM, Geoffrey Zhu wrote:> Hi Everyone, > > I want to generate some random numbers according to some empirical > distribution. Therefore I am looking for the inverse of an empirical > cumulative distribution function. I haven't found any in R. Can anyone > give a pointer? > > Thanks, > Geoffrey
Hi Benilton, I tried this. It sort of works, but the results are not very satisfactionary. The 3rd moment and higher do not match those of the original by a large difference. Do you have any better way to do this? Thanks, Geoffrey -----Original Message----- From: Benilton Carvalho [mailto:bcarvalh at jhsph.edu] Sent: Sunday, January 28, 2007 4:45 PM To: Geoffrey Zhu Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Inverse fuction of ecdf ?quantile b On Jan 28, 2007, at 5:41 PM, Geoffrey Zhu wrote:> Hi Everyone, > > I want to generate some random numbers according to some empirical > distribution. Therefore I am looking for the inverse of an empirical > cumulative distribution function. I haven't found any in R. Can anyone> give a pointer? > > Thanks, > Geoffrey_______________________________________________________=0A=0A=0AThe information in this email or in any file attached hereto...{{dropped}}
On 1/28/2007 5:41 PM, Geoffrey Zhu wrote:> Hi Everyone, > > I want to generate some random numbers according to some empirical > distribution. Therefore I am looking for the inverse of an empirical > cumulative distribution function. I haven't found any in R. Can anyone > give a pointer?sample() works fine if you have a sample to start from. If you really need to start from an ecdf, you could generate x and prob args to sample() by looking inside the ecdf object. For example: x <- rbinom(100, 100, 0.2) e <- ecdf(x) Now either of these should give you what you want. size <- 1000 sample(x, size, replace=TRUE) or vals <- get("x", environment(e)) probs <- diff(c(0, get("y", environment(e)))) sample(vals, size, replace=TRUE, prob=probs) Duncan Murdoch