Bärbel
2012-Nov-07 13:02 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com.
Albyn Jones
2012-Nov-07 18:48 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
What uniform distribution do you want for the columns? The average value of X_k given \sum X_i = 1 must be 1/n. If you start with X_i ~ U(0,1), the result must be skewed. If you generate X_i uniformly distributed on (0, 2/n), the conditional distribution given the sum is 1 will be less skewed. U <- matrix(runif(1000*100)/50,nrow=1000) s <- apply(U,1,sum) V <- U/s qqplot(U[,1],V[,1]) albyn On Wed, Nov 07, 2012 at 05:02:10AM -0800, B?rbel wrote:> Hi, > I am looking for a way to generate a matrix of random numbers in a way that > each row of the matrix would sum to 1 and that the numbers in the columns of > the matrix would have a uniform distribution. So far I have found several > ways to create random numbers that would sum to 1, but then the distribution > of the individual elements is more or less skewed - there are much more > small numbers than larger ones. > Any help and suggestions? > - B?rbel > > > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Albyn Jones Reed College jones at reed.edu
Heramb
2012-Nov-07 18:56 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Hi, This is a humble try. Matrix=function(n){Tab=cbind(runif(n,0,1)) for(i in 2:n) {x=NULL for(j in 1:n) {x=c(x,runif(1,0,1-sum(Tab[j,])))} Tab=cbind(Tab,x) } Table<<-Tab } n=1,2,3,4..... Matrix(n) 1. The columns will follow uniform distribution with respective parameters. 2. The row sums will be approximately equal to '1'. 3. The discrepancy in 2nd Point can be removed if the number of columns are increased. I hope this works Best, Heramb M. Gadgil -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of B?rbel Sent: Wednesday, November 07, 2012 6:32 PM To: r-help at r-project.org Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Heramb
2012-Nov-07 19:12 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
This is a modified code if it helps. Matrix=function(n,simulations){Tab=cbind(runif(n,0,1)) for(i in 2:simulations) {x=NULL for(j in 1:n) {x=c(x,runif(1,0,1-sum(Tab[j,])))} Tab=cbind(Tab,x) } Table<<-Tab } Matrix(n,simulations) #Matrix(5,100) colnames(Table)=NULL Best, Heramb -----Original Message----- From: Heramb [mailto:heramb.gadgil at gmail.com] Sent: Thursday, November 08, 2012 12:26 AM To: 'B?rbel'; 'r-help at r-project.org' Subject: RE: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements Hi, This is a humble try. Matrix=function(n){Tab=cbind(runif(n,0,1)) for(i in 2:n) {x=NULL for(j in 1:n) {x=c(x,runif(1,0,1-sum(Tab[j,])))} Tab=cbind(Tab,x) } Table<<-Tab } n=1,2,3,4..... Matrix(n) 1. The columns will follow uniform distribution with respective parameters. 2. The row sums will be approximately equal to '1'. 3. The discrepancy in 2nd Point can be removed if the number of columns are increased. I hope this works Best, Heramb M. Gadgil -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of B?rbel Sent: Wednesday, November 07, 2012 6:32 PM To: r-help at r-project.org Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
David L Carlson
2012-Nov-07 21:36 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
It sounds like you are trying to generate a uniform distribution on the simplex (e.g. compositional analysis where the values represent the proportion of each constituent in the whole). If so, you probably want the compositions package. Here's a simple example with just 3 parts so we can plot the results on a ternary plot: library(compositions) mat <- runif.rcomp(1000, 3) plot(mat) ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 From: Heramb heramb.gadgil at gmail.com Date: Wed Nov 7 20:12:20 CET 2012 Re: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements This is a modified code if it helps. Matrix=function(n,simulations){Tab=cbind(runif(n,0,1)) for(i in 2:simulations) {x=NULL for(j in 1:n) {x=c(x,runif(1,0,1-sum(Tab[j,])))} Tab=cbind(Tab,x) } Table<<-Tab } Matrix(n,simulations) #Matrix(5,100) colnames(Table)=NULL Best, Heramb -----Original Message----- From: Heramb [mailto:heramb.gadgil at gmail.com] Sent: Thursday, November 08, 2012 12:26 AM To: 'B?rbel'; 'r-help at r-project.org' Subject: RE: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements Hi, This is a humble try. Matrix=function(n){Tab=cbind(runif(n,0,1)) for(i in 2:n) {x=NULL for(j in 1:n) {x=c(x,runif(1,0,1-sum(Tab[j,])))} Tab=cbind(Tab,x) } Table<<-Tab } n=1,2,3,4..... Matrix(n) 1. The columns will follow uniform distribution with respective parameters. 2. The row sums will be approximately equal to '1'. 3. The discrepancy in 2nd Point can be removed if the number of columns are increased. I hope this works Best, Heramb M. Gadgil -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of B?rbel Sent: Wednesday, November 07, 2012 6:32 PM To: r-help at r-project.org Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel - View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-s um-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Greg Snow
2012-Nov-07 22:21 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
The Dirichlet distribution will give values that sum to one and with the correct conditions will have uniform margins. There are multiple packages that will generate data from Dirichlet distributions, either gtools or gmodels is one of them, but there are others as well. On Wed, Nov 7, 2012 at 6:02 AM, Bärbel <baerbel@latnet.lv> wrote:> Hi, > I am looking for a way to generate a matrix of random numbers in a way that > each row of the matrix would sum to 1 and that the numbers in the columns > of > the matrix would have a uniform distribution. So far I have found several > ways to create random numbers that would sum to 1, but then the > distribution > of the individual elements is more or less skewed - there are much more > small numbers than larger ones. > Any help and suggestions? > - Bärbel > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]
Bärbel
2012-Nov-08 21:22 UTC
[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Thanks a lot for all your input!! -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695p4648973.html Sent from the R help mailing list archive at Nabble.com.