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

From: William Dunlap <wdunlap_at_tibco.com>
Date: Tue, 29 Mar 2011 12:07:25 -0700

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Bert Gunter
> Sent: Tuesday, March 29, 2011 10:07 AM
> To: Petr Savicky; cddesjardins_at_gmail.com
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Creating 3 vectors that sum to 1
>
> So you want to generate random multinomials ? ...
> Just generate random uniforms and divide by their sum!
>
> z <- runif(3); z/sum(z)

Wouldn't rexp() be better than runif() here?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

>
> ## This can easily be modified to generate lots of random multinomial
> 3 vectors, e.g.
>
> z <- matrix( runif(3*100), ncol=3)
> z/rowSums(z) ## each row is a random multinomial
>
>
> -- Bert
>
>
>
> On Tue, Mar 29, 2011 at 9:34 AM, Petr Savicky
> <savicky_at_praha1.ff.cuni.cz> wrote:
> > 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.
> >
> > ______________________________________________
> > 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.
> >
>
>
>
> --
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 467-7374
> http://devo.gene.com/groups/devo/depts/ncb/home.shtml
>
> ______________________________________________
> 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.
>



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:09:29 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