Drawing from an empirical distribution

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Drawing from an empirical distribution

culpritNr1
Hi All,

Does anybody know if there is a simple way to draw numbers from an empirical distribution?

I know that I can plot the empirical cumulative distribution function this easy:
plot(ecdf(x))

Now I want to pick a number between 0 and 1 and go back to domain of x. Sounds simple to me.

Any suggestion?

Thank you,

Your culprit
(everybody needs a culprit)

Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

Antonio, Fabio Di Narzo
If the ecdf is 'ecdf(x)', do just:
> sample(x, size=whatever, replace=TRUE)

HTH,
Antonio.

2009/1/6 culpritNr1 <[hidden email]>:

>
> Hi All,
>
> Does anybody know if there is a simple way to draw numbers from an empirical
> distribution?
>
> I know that I can plot the empirical cumulative distribution function this
> easy:
> plot(ecdf(x))
>
> Now I want to pick a number between 0 and 1 and go back to domain of x.
> Sounds simple to me.
>
> Any suggestion?
>
> Thank you,
>
> Your culprit
> (everybody needs a culprit)
>
>
> --
> View this message in context: http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

jones-2
In reply to this post by culpritNr1
the empirical distribution gives probability 1/n to each of n observations.
rather than sampling the unit interval, just resample the dataset.
If x is your dataset, and you want an independent sample of size k,

        sample(x,size=k,replace=TRUE)

albyn

On Tue, Jan 06, 2009 at 02:39:17PM -0800, culpritNr1 wrote:

>
> Hi All,
>
> Does anybody know if there is a simple way to draw numbers from an empirical
> distribution?
>
> I know that I can plot the empirical cumulative distribution function this
> easy:
> plot(ecdf(x))
>
> Now I want to pick a number between 0 and 1 and go back to domain of x.
> Sounds simple to me.
>
> Any suggestion?
>
> Thank you,
>
> Your culprit
> (everybody needs a culprit)
>
>
> --
> View this message in context: http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

culpritNr1
In reply to this post by Antonio, Fabio Di Narzo
Thank you. That's exactly what I was looking for.


Antonio, Fabio Di Narzo wrote
If the ecdf is 'ecdf(x)', do just:
> sample(x, size=whatever, replace=TRUE)

HTH,
Antonio.

2009/1/6 culpritNr1 <ig2ar-saf1@yahoo.co.uk>:
>
> Hi All,
>
> Does anybody know if there is a simple way to draw numbers from an empirical
> distribution?
>
> I know that I can plot the empirical cumulative distribution function this
> easy:
> plot(ecdf(x))
>
> Now I want to pick a number between 0 and 1 and go back to domain of x.
> Sounds simple to me.
>
> Any suggestion?
>
> Thank you,
>
> Your culprit
> (everybody needs a culprit)
>
>
> --
> View this message in context: http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>



--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy

______________________________________________
R-help@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.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

RKoenker
In reply to this post by Antonio, Fabio Di Narzo
Sure, but it would be more 'fun' to modify ecdf() slightly to produce
an ecqf()  function -- essentially reversing the arguments to  
approxfun()--
and then use

        ecqf(runif(whatever))

no nit-picking about efficiency, please.


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    [hidden email]            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820



On Jan 6, 2009, at 4:42 PM, Antonio, Fabio Di Narzo wrote:

> If the ecdf is 'ecdf(x)', do just:
>> sample(x, size=whatever, replace=TRUE)
>
> HTH,
> Antonio.
>
> 2009/1/6 culpritNr1 <[hidden email]>:
>>
>> Hi All,
>>
>> Does anybody know if there is a simple way to draw numbers from an  
>> empirical
>> distribution?
>>
>> I know that I can plot the empirical cumulative distribution  
>> function this
>> easy:
>> plot(ecdf(x))
>>
>> Now I want to pick a number between 0 and 1 and go back to domain  
>> of x.
>> Sounds simple to me.
>>
>> Any suggestion?
>>
>> Thank you,
>>
>> Your culprit
>> (everybody needs a culprit)
>>
>>
>> --
>> View this message in context: http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> Antonio, Fabio Di Narzo
> Ph.D. student at
> Department of Statistical Sciences
> University of Bologna, Italy
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

RKoenker
Nit-picking about syntax does seem needed, mea culpa,  I
intended something more like:

        Qn <- ecqf(x)
        Qn(runif(whatever))

On Jan 6, 2009, at 5:06 PM, roger koenker wrote:

> Sure, but it would be more 'fun' to modify ecdf() slightly to produce
> an ecqf()  function -- essentially reversing the arguments to  
> approxfun()--
> and then use
>
> ecqf(runif(whatever))
>
> no nit-picking about efficiency, please.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    [hidden email]            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
>
>
> On Jan 6, 2009, at 4:42 PM, Antonio, Fabio Di Narzo wrote:
>
>> If the ecdf is 'ecdf(x)', do just:
>>> sample(x, size=whatever, replace=TRUE)
>>
>> HTH,
>> Antonio.
>>
>> 2009/1/6 culpritNr1 <[hidden email]>:
>>>
>>> Hi All,
>>>
>>> Does anybody know if there is a simple way to draw numbers from an  
>>> empirical
>>> distribution?
>>>
>>> I know that I can plot the empirical cumulative distribution  
>>> function this
>>> easy:
>>> plot(ecdf(x))
>>>
>>> Now I want to pick a number between 0 and 1 and go back to domain  
>>> of x.
>>> Sounds simple to me.
>>>
>>> Any suggestion?
>>>
>>> Thank you,
>>>
>>> Your culprit
>>> (everybody needs a culprit)
>>>
>>>
>>> --
>>> View this message in context: http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>>
>>
>> --
>> Antonio, Fabio Di Narzo
>> Ph.D. student at
>> Department of Statistical Sciences
>> University of Bologna, Italy
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Drawing from an empirical distribution

Prof. Dr. Matthias Kohl
In reply to this post by RKoenker
one could also use package distr; e.g.,

library(distr)
x <- 1:10
D <- DiscreteDistribution(x)
## => r, d, p and q functions (also with log-argument)
r(D)(5)
p(D)(4)
d(D)(1)
q(D)(0.3)

Best,
Matthias


roger koenker wrote:

> Sure, but it would be more 'fun' to modify ecdf() slightly to produce
> an ecqf()  function -- essentially reversing the arguments to
> approxfun()--
> and then use
>
>     ecqf(runif(whatever))
>
> no nit-picking about efficiency, please.
>
>
> url:    www.econ.uiuc.edu/~roger            Roger Koenker
> email    [hidden email]            Department of Economics
> vox:     217-333-4558                University of Illinois
> fax:       217-244-6678                Champaign, IL 61820
>
>
>
> On Jan 6, 2009, at 4:42 PM, Antonio, Fabio Di Narzo wrote:
>
>> If the ecdf is 'ecdf(x)', do just:
>>> sample(x, size=whatever, replace=TRUE)
>>
>> HTH,
>> Antonio.
>>
>> 2009/1/6 culpritNr1 <[hidden email]>:
>>>
>>> Hi All,
>>>
>>> Does anybody know if there is a simple way to draw numbers from an
>>> empirical
>>> distribution?
>>>
>>> I know that I can plot the empirical cumulative distribution
>>> function this
>>> easy:
>>> plot(ecdf(x))
>>>
>>> Now I want to pick a number between 0 and 1 and go back to domain of x.
>>> Sounds simple to me.
>>>
>>> Any suggestion?
>>>
>>> Thank you,
>>>
>>> Your culprit
>>> (everybody needs a culprit)
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Drawing-from-an-empirical-distribution-tp21320810p21320810.html 
>>>
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>>
>>
>> --
>> Antonio, Fabio Di Narzo
>> Ph.D. student at
>> Department of Statistical Sciences
>> University of Bologna, Italy
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

--
Dr. Matthias Kohl
www.stamats.de

______________________________________________
[hidden email] 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.