Or we could expand a bit more: require(TeachingDemos) require(gtools)
n <- 1000 rtrg <- matrix(NA, n, 3) for (i in 1:n) rtrg[i,] <- diff(c(0, sort(runif(2)), 1)) rtrg2 <- matrix(NA, n, 3) for (i in 1:n) { tmp <- runif(3) rtrg2[i, ] <- tmp/sum(tmp) } rtrg3 <- matrix( rexp(n*3), ncol=3 ) rtrg3 <- rtrg3/rowSums(rtrg3) rtrg4 <- rdirichlet(n, rep(1,3)) par(mfrow=c(2,2)) triplot(rtrg, pch='.') # Looks more uniformly distributed triplot(rtrg2, col=2, pch='.') # Corners are sparsely populated triplot(rtrg3, col=3, pch='.') triplot(rtrg4, col=4, pch='.') What could also be interesting in using vis.test (also TeachingDemos) to see which can be told apart from each other. My guess is that rtrg2 method will be visible different from the other 3, but the other 3 will be indistinguishable from each other. The last 2 have the advantage (the 2nd could be rewritten to have the same advantage) of being much quicker, not sure how to speed up the 1st noticibly. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -----Original Message----- > From: Ravi Varadhan [mailto:rvarad...@jhmi.edu] > Sent: Tuesday, March 29, 2011 12:59 PM > To: Ravi Varadhan > Cc: Greg Snow; r-help@r-project.org > Subject: Re: [R] Creating 3 vectors that sum to 1 > > > Here is an exploration of two different 3-tuple generators (that sum to > 1) based on Greg's triplot function: > > require(TeachingDemos) > > n <- 1000 > rtrg <- matrix(NA, n, 3) > for (i in 1:n) rtrg[i,] <- diff(c(0, sort(runif(2)), 1)) > > rtrg2 <- matrix(NA, n, 3) > for (i in 1:n) { > tmp <- runif(3) > rtrg2[i, ] <- tmp/sum(tmp) > } > > par(mfrow=c(2,1)) > triplot(rtrg) # Looks more uniformly distributed > triplot(rtrg2, col=2) # Corners are sparsely populated > > Ravi. > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvarad...@jhmi.edu > > > ----- Original Message ----- > From: Ravi Varadhan <rvarad...@jhmi.edu> > Date: Tuesday, March 29, 2011 2:33 pm > Subject: Re: [R] Creating 3 vectors that sum to 1 > To: Greg Snow <greg.s...@imail.org> > Cc: "r-help@r-project.org" <r-help@r-project.org> > > > > The following one-liner generates uniformly distributed 3-tuples that > > sum to 1: > > > > diff(c(0, sort(runif(2)), 1)) > > > > More, generally you can generate n-tuples that sum to unity as: > > > > diff(c(0, sort(runif(n-1)), 1)) > > > > > > Ravi. > > > > ____________________________________________________________________ > > > > Ravi Varadhan, Ph.D. > > Assistant Professor, > > Division of Geriatric Medicine and Gerontology > > School of Medicine > > Johns Hopkins University > > > > Ph. (410) 502-2619 > > email: rvarad...@jhmi.edu > > > > > > ----- Original Message ----- > > From: Greg Snow <greg.s...@imail.org> > > Date: Tuesday, March 29, 2011 1:02 pm > > Subject: Re: [R] Creating 3 vectors that sum to 1 > > To: Christopher Desjardins <cddesjard...@gmail.com>, > > "r-help@r-project.org" <r-help@r-project.org> > > > > > > > Do a search for Dirichlet, that may give you the tools you need. > > Also > > > for plotting 3 vectors that sum to 1, instead of a 3d scatter plot > > you > > > should look into a triangle or trilinear plot, see ?triplot in the > > > TeachingDemos package (the see also for that help page lists > several > > > > > other implementations in other packages as well). > > > > > > -- > > > Gregory (Greg) L. Snow Ph.D. > > > Statistical Data Center > > > Intermountain Healthcare > > > greg.s...@imail.org > > > 801.408.8111 > > > > > > > > > > -----Original Message----- > > > > From: r-help-boun...@r-project.org [ > > > > project.org] On Behalf Of Christopher Desjardins > > > > Sent: Tuesday, March 29, 2011 10:20 AM > > > > To: r-help@r-project.org > > > > Subject: [R] Creating 3 vectors that sum to 1 > > > > > > > > I have 3 vectors: p1, p2, and p3. I would like each vector to be > any > > > > possible value between 0 and 1 and p1 + p2 + p3 = 1. I want to > graph > > > > these > > > > and I've thought about using scatterplot3d(). Here's what I have > so > > > > far. > > > > > > > > library(scatterplot3d) > > > > p1 <- c(1,0,0,.5,.5,0,.5,.25,.25,.34,.33,.33,.8,.1,.1,.9,.05,.05) > > > > p2 <- c(0,1,0,.5,0,.5,.25,.5,.25,.33,.34,.33,.1,.8,.1,.05,.9,.05) > > > > p3 <- c(0,0,1,0,.5,.5,.25,.25,.5,.33,.33,.34,.1,.1,.8,.05,.05,.9) > > > > scatterplot3d(p1,p2,p3) > > > > > > > > > > > > However, I wonder if there is an easy way to create vectors p1, > > p2, > > > and > > > > p3. > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help@r-project.org mailing list > > > > > > > > PLEASE do read the posting guide > > > > guide.html > > > > and provide commented, minimal, self-contained, reproducible > code. > > > > > > ______________________________________________ > > > R-help@r-project.org mailing list > > > > > > PLEASE do read the posting guide > > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > > R-help@r-project.org mailing list > > > > PLEASE do read the posting guide > > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ 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.