From: Greg Snow <Greg.Snow_at_imail.org>

Date: Tue, 29 Mar 2011 13:42:18 -0600

Date: Tue, 29 Mar 2011 13:42:18 -0600

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.snow_at_imail.org 801.408.8111Received on Tue 29 Mar 2011 - 19:44:37 GMT

> -----Original Message-----

> From: Ravi Varadhan [mailto:rvaradhan_at_jhmi.edu]> Sent: Tuesday, March 29, 2011 12:59 PM> To: Ravi Varadhan> Cc: Greg Snow; r-help_at_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: rvaradhan_at_jhmi.edu>>> ----- Original Message -----> From: Ravi Varadhan <rvaradhan_at_jhmi.edu>> Date: Tuesday, March 29, 2011 2:33 pm> Subject: Re: [R] Creating 3 vectors that sum to 1> To: Greg Snow <Greg.Snow_at_imail.org>> Cc: "r-help_at_r-project.org" <r-help_at_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: rvaradhan_at_jhmi.edu> >> >> > ----- Original Message -----> > From: Greg Snow <Greg.Snow_at_imail.org>> > Date: Tuesday, March 29, 2011 1:02 pm> > Subject: Re: [R] Creating 3 vectors that sum to 1> > To: Christopher Desjardins <cddesjardins_at_gmail.com>,> > "r-help_at_r-project.org" <r-help_at_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.snow_at_imail.org> > > 801.408.8111> > >> > >> > > > -----Original Message-----> > > > From: r-help-bounces_at_r-project.org [> > > > project.org] On Behalf Of Christopher Desjardins> > > > Sent: Tuesday, March 29, 2011 10:20 AM> > > > To: r-help_at_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_at_r-project.org mailing list> > > >> > > > PLEASE do read the posting guide> > > > guide.html> > > > and provide commented, minimal, self-contained, reproducible> code.> > >> > > ______________________________________________> > > R-help_at_r-project.org mailing list> > >> > > PLEASE do read the posting guide> > > and provide commented, minimal, self-contained, reproducible code.> >> > ______________________________________________> > R-help_at_r-project.org mailing list> >> > PLEASE do read the posting guide> > and provide commented, minimal, self-contained, reproducible code.

______________________________________________ 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.

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Wed 30 Mar 2011 - 10:50:26 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*