[R] analytical solution to Sum of binominal distributed random numbers?

Rainer M Krug Rainer+R-help at krugs.de
Wed Oct 24 09:11:15 CEST 2007


Hi Charles

thanks for the pointing out that size and prob can be vectors as well - 
I tried it out but used 1 as the number of observations, assuming that 
and it only gave me one randon mumbewr (as it should be but not expected).

But I was more looking at a analytical solution, as I have to sum up a 
huge number of random numbers. But I am going to try your solution as it 
should be much faster already.

Thanks

Rainer


Charles C. Berry wrote:
> 
>  	?rbinom
> 
> only says:
> 
>      size: number of trials (zero or more).
> 
>      prob: probability of success on each trial.
> 
> 
> But they can be vectors.
> 
> BTW, you were aked to "PLEASE ... provide  minimal, self-contained, 
> reproducible code."
> 
> What you show cannot run without correction.
> 
> Most likely, you intended size(n) to be the n-th element of the vector 
> 'size', which in R is written 'size[ n ]' .
> 
> In which case
> 
>  	sum (rbinom( length(prob) , size, prob ) )
> 
> works.
> 
> Chuck
> 
> On Tue, 23 Oct 2007, Rainer M Krug wrote:
> 
>> Hi
>>
>> I have two vectors, prob and size, and I want to add the random deviates
>>  of these two, i.e.
>>
>> sum(
>>   sapply(
>>          1:length(prob),
>>          function(n){ rbinom(1, size(n), prob(n) }
>>         )
>> )
>>
>> My problem is that I have to do this for a large number of value
>> combinations. Is there a faster way of doing this?
>>
>> Rainer
>>
>> ______________________________________________
>> 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.
>>
> 
> Charles C. Berry                            (858) 534-2098
>                                              Dept of Family/Preventive Medicine
> E mailto:cberry at tajo.ucsd.edu	            UC San Diego
> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
> 
> ______________________________________________
> 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.



More information about the R-help mailing list