[R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements

David L Carlson dcarlson at tamu.edu
Wed Nov 7 22:36:00 CET 2012


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.




More information about the R-help mailing list