[R] Creating 3 vectors that sum to 1

Ravi Varadhan rvaradhan at jhmi.edu
Tue Mar 29 20:58:43 CEST 2011


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.



More information about the R-help mailing list