[R] RES: Sum(Random Numbers)=100

Leandro Marino leandro at cesgranrio.org.br
Tue Jul 8 14:18:11 CEST 2008


One of possible solutions is:

generate all the numbers.

num <- rpois(...)
num <- round(num/sum(num)*100,0)

sum(num)

I don't know if it is the best solution, but is one!

Leandro Marino


-----Mensagem original-----
De: r-help-bounces em r-project.org [mailto:r-help-bounces em r-project.org]Em
nome de Peter Dalgaard
Enviada em: terça-feira, 8 de julho de 2008 05:12
Para: Richard Pearson
Cc: r-help em stat.math.ethz.ch
Assunto: Re: [R] Sum(Random Numbers)=100


Richard Pearson wrote:
> Shubha
>
> Does table(ceiling(runif(100,0,50))) give you something like you want?
That's a neat(-ish) solution of Moshe's multinomial formulation.

More generally, if p is a vector of probabilities you can simulate
independent indicators with cut(runif(N), c(0,cumsum(p)) and then
multinomials by tabulation.

However, it is inefficient for large N, and awkward if you want
replicates, which is why we have rmultinom(). In the present case use
rmultinom(1, 100, rep(1/50,50))

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard em biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help em 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