Re: [R] Inverse fuction of ecdf

From: Duncan Murdoch <>
Date: Sun 28 Jan 2007 - 23:23:44 GMT

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)


vals <- get("x", environment(e))
probs <- diff(c(0, get("y", environment(e)))) sample(vals, size, replace=TRUE, prob=probs)

Duncan Murdoch mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon Jan 29 10:29:03 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 29 Jan 2007 - 01:30:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.