[R] generate multiple probability distributions

Michael Friendly friendly at yorku.ca
Mon Dec 2 20:20:05 CET 2013


On 12/2/2013 10:12 AM, Duncan Murdoch wrote:
> dbinom can take vector inputs for the parameters, so this would be a 
> bit simpler:
>
> x <- seq(0,12)
> x <- rep(x, 4)
> p <- rep(c(1/6, 1/3, 1/2, 2/3), each=13)
> bin.df <- data.frame(x, prob = dbinom(x, 12, p), p)
>
Thanks, Duncan

What I was missing was how dbinom() could use vector inputs for both x & p.
Using expand.grid() gives me what I want and is more
general for my purposes.  The main purpose of making p a factor is to 
facilitate
plots of prob ~ x|p.


XP <-expand.grid(x=0:12, p=c(1/6, 1/3, 1/2, 2/3))
bin.df <- data.frame(XP, prob=dbinom(XP[,"x"], 12, XP[,"p"]))
bin.df$p <- factor(bin.df$p, labels=c("1/6", "1/3", "1/2", "2/3"))
str(bin.df)

-Michael


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list