Re: [R] Creating 3 vectors that sum to 1

From: Ravi Varadhan <rvaradhan_at_jhmi.edu>
Date: Tue, 29 Mar 2011 14:58:43 -0400

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

> 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. Received on Tue 29 Mar 2011 - 19:01:28 GMT

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 Tue 29 Mar 2011 - 20:00:25 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.

list of date sections of archive