Re: [R] make sample() faster

From: Rainer M. Krug <Rainer_at_krugs.de>
Date: Thu, 14 Jun 2007 14:02:15 +0200

Thanks for the info - so the solution would (likely) be to draw several samples and use these whenever I need a new one.

tHANKS, i'LL TRY IT OUT,

rAINER

P.S: somebody said once that the caps-lock key is the most useless key on the keyboard and he is right...

Prof Brian Ripley wrote:

> I think the problem is not sample (which is written in C), but that you 
> are calling it with size=1.  Taking one sample with probabilities from a 
> large discrete distribution is necessarily slow, but you can take a large 
> sample for little more cost.
> 
> On Thu, 14 Jun 2007, Rainer M. Krug wrote:
> 

>> Hi
>>
>> I have a simulation which is relatively slow. I used Rprofile() and
>> identified the calls to sample() as the culprit is sample():
>>
>>> summaryRprof("Documents/PostDoc/Aloe_Pillansii/R/create.out")
>> $by.self
>> self.time self.pct total.time total.pct
>> "sample" 1.30 44.2 1.52 51.7
>> "ifelse" 0.46 15.6 2.44 83.0
>> .
>> .
>> .
>>
>> I am using sample() as follow:
>> result <- sample(
>> x=d.growth.seedling$growth,
>> size=1,
>> prob=d.growth.seedling$p,
>> replace
>> )
>>
>> d.growth.seedling$p and d.growth.seedling$growth have a length of 1024
>> and are calculated initially by using density().
>>
>> My question: is there any way to make this faster, i.e. replace sample()
>> as I use it with another faster algorithm (if necessary implemented in C)?
>>
>> Thanks in advance,
>>
>> Rainer
>>
>>
>
-- 
NEW EMAIL ADDRESS AND ADDRESS:

Rainer.Krug_at_uct.ac.za

RKrug_at_sun.ac.za WILL BE DISCONTINUED END OF MARCH

Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Leslie Hill Institute for Plant Conservation
University of Cape Town
Rondebosch 7701
South Africa

Fax:		+27 - (0)86 516 2782
Fax:		+27 - (0)21 650 2440 (w)
Cell:		+27 - (0)83 9479 042

Skype:		RMkrug

email:	Rainer.Krug_at_uct.ac.za
       	Rainer_at_krugs.de

______________________________________________
R-help_at_stat.math.ethz.ch 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 Thu 14 Jun 2007 - 12:19:46 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 Thu 14 Jun 2007 - 13:31:57 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.