[R] Sum of binominal distributed random numbers

Charles C. Berry cberry at tajo.ucsd.edu
Tue Oct 23 18:39:46 CEST 2007



 	?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



More information about the R-help mailing list