Skip to content
Prev 156317 / 398506 Next

Is there a way to not use an explicit loop?

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: