Both shape parameters of rbeta can be vectors; for x <- rbeta(n, shape1, shape2)
x[i] ~ Beta(shape1[i], shape2[i]) so bbsim <- function(m=1000, num.post.draws=1e4, size.a=100, prob.a=.27, prior.count=1) { data.count <- rbinom(m, size.a, prob.a) shape1 <- rep(prior.count + data.count, each=num.post.draws) shape2 <- rep(prior.count + size.a - data.count, each=num.post.draws) matrix(rbeta(m * num.post.draws, shape1, shape2), num.post.draws, m) } Then you can do beta.draws <- bbsim() means <- apply(beta.draws, 2, mean) medians <- apply(beta.draws, 2, median) etc Dan On Wed, Sep 17, 2008 at 11:56:36AM -0700, Juancarlos Laguardia wrote: > I have a problem in where i generate m independent draws from a binomial > distribution, > say > > draw1 = rbinom( m , size.a, prob.a ) > > > then I need to use each draw to generate a beta distribution. So, like > using a beta prior, binomial likelihood, and obtain beta posterior, m > many times. I have not found out a way to vectorize draws from a beta > distribution, so I have an explicit for loop within my code > > > > for( i in 1: m ) { > > beta.post = rbeta( 10000, draw1[i] + prior.constant , prior.constant + > size.a - draw1[i] ) > > beta.post.mean[i] = mean(beta.post) > beta.post.median[i] = median(beta.post) > > etc.. for other info > > } > > Is there a way to vectorize draws from an beta distribution? > > UC Slug > > ______________________________________________ > R-help@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. -- http://www.stats.ox.ac.uk/~davison ______________________________________________ R-help@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.