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

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

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

> 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
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 - 18:34:50 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 - 19:10:24 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