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]]
On Tue, Mar 29, 2011 at 11:20:13AM -0500, Christopher Desjardins wrote:> 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.Hi. The vectors p1, p2 and p3 are not uniquely determined. Try, for example, the following n <- 16 pp <- expand.grid(p1=0:n, p2=0:n, p3=0:n) pp <- subset(pp, p1 + p2 + p3 == n) p1 <- pp$p1/n p2 <- pp$p2/n p3 <- pp$p3/n If n is a power of 2, then p1 + p2 + p3 will be exactly all ones vector. Otherwise, there may be differences within machine rounding error. Hope this helps. Petr Savicky.
Please look at the triplot function in the klaR package. The keyword is RSiteSearch("barycentric") Rich On Tue, Mar 29, 2011 at 12:20 PM, Christopher Desjardins < cddesjardins@gmail.com> wrote:> 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html<r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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 [mailto:r-help-bounces at r- > 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
On 2011-03-29 09:20, Christopher Desjardins wrote:> 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.If all you want is a set of 3 vectors summing to 1, you could generate 3 vectors with runif() and then scale them: n <- 10 u <- runif(3*n) m <- matrix(u, nr=3) m1 <- apply(m, 2, function(x) x/sum(x)) ## or use: m1 <- sweep(m, 2, colSums(m), "/") colSums(m1) #[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Peter Ehlers