[R] analytical solution to Sum of binominal distributed random numbers?
Rainer M. Krug
rainer.r at krugs.de
Wed Oct 24 12:53:59 CEST 2007
Hi Jay & Matthias
That's exactly what I was looking for.
Thanks a lot
Rainer
G. Jay Kerns wrote:
> Hi Rainer,
>
> The distr package can calculate the distribution for you:
>
> library(distr)
> X <- Binom(size = 7, prob = 0.3)
> Y <- Binom(size = 11, prob = 0.5)
> Z <- X + Y
>
> d(Z)( 0:18 ) # the pmf
> r(Z)( n = 5 ) # random variates
>
> Please note, however, that size and prob must be of length 1.
>
> Best,
> Jay
>
> On 10/24/07, Rainer M. Krug <rainer.r at krugs.de> wrote:
>> Frede Aakmann Tøgersen wrote:
>>> Perhaps
>>>
>>> http://stinet.dtic.mil/cgi-bin/GetTRDoc?AD=ADA266969&Location=U2&doc=GetTRDoc.pdf
>>>
>>> is something that you can use?
>> Thanks a lot - that might help.
>>
>> Rainer
>>
>>>
>>>
>>> Best regards
>>>
>>> Frede Aakmann Tøgersen
>>> Scientist
>>>
>>>
>>> UNIVERSITY OF AARHUS
>>> Faculty of Agricultural Sciences
>>> Dept. of Genetics and Biotechnology
>>> Blichers Allé 20, P.O. BOX 50
>>> DK-8830 Tjele
>>>
>>> Phone: +45 8999 1900
>>> Direct: +45 8999 1878
>>>
>>> E-mail: FredeA.Togersen at agrsci.dk
>>> Web: http://www.agrsci.org
>>>
>>> This email may contain information that is confidential.
>>> Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed.
>>> If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email.
>>>
>>>
>>>
>>>
>>>
>>>> -----Oprindelig meddelelse-----
>>>> Fra: r-help-bounces at r-project.org
>>>> [mailto:r-help-bounces at r-project.org] På vegne af Rainer M Krug
>>>> Sendt: 24. oktober 2007 09:11
>>>> Til: Charles C. Berry
>>>> Cc: r-help
>>>> Emne: Re: [R] analytical solution to Sum of binominal
>>>> distributed random numbers?
>>>>
>>>> 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.
>>>> ______________________________________________
>>>> 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.
>>>>
>> ______________________________________________
>> 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