potpie
potpie

Reputation: 135

Winbugs to Rjags beta binomial model translation

I am working through the textbook "Bayesian Ideas and Data Analysis" by Christensen et al.

There is a simple exercise in the book that involves cutting and pasting the following code to run in Winbugs:

model{ y ~ dbin(theta, n) # Model the data 
ytilde ~ dbin(theta, m) # Prediction of future binomial 
theta ~ dbeta(a, b) # The prior 
prob <- step(ytilde - 20) # Pred prob that ytilde >= 20 } 
list(n=100, m=100, y=10, a=1, b=1) # The data 
list(theta=0.5, ytilde=10) # Starting/initial values

I am trying to translate the following into R2jags code and am running into some trouble. I thought I could fairly directly write my R2Jags code in this fashion:

model {
    #Likelihoods
    y ~ dbin(theta,n)
    yt ~ dbin(theta,m)
    #Priors
    theta ~ dbeta(a,b)
    prob <- step(yt - 20)
}

with the R code:

library(R2jags)

n <- 100
m <- 100
y <- 10
a <- 1
b <- 1

jags.data <- list(n = n,
                  m = m,
                  y = y,
                  a = a,
                  b = b)

jags.init <- list(
                 list(theta = 0.5, yt = 10), #Chain 1 init
                 list(theta = 0.5, yt = 10), #Chain 2 init
                 list(theta = 0.5, yt = 10) #Chain 3 init
                 ) 

jags.param <- c("theta", "yt")

jags.fit <- jags.model(data = jags.data,
                     inits = jags.inits,
                     parameters.to.save = jags.param,
                     model.file = "hw21.bug",
                     n.chains = 3,
                     n.iter = 5000,
                     n.burnin = 100)

print(jags.fit)

However, calling the R code brings about the following error:

Error in jags.model(data = jags.data, inits = jags.inits, parameters.to.save = jags.param,  : 
  unused arguments (parameters.to.save = jags.param, model.file = "hw21.bug", n.iter = 5000, n.burnin = 100)

Is it because I am missing a necessary for loop in my R2Jags model code?

Upvotes: 0

Views: 771

Answers (2)

John K. Kruschke
John K. Kruschke

Reputation: 535

This old blog post has an extensive example of converting BUGS to JAGS accessed via package rjags not R2jags. (I like the package runjags even better.) I know we're supposed to present self-contained answers here, not just links, but the post is rather long. It goes through each logical step of a script, including:

  • loading the package
  • specifying the model
  • assembling the data
  • initializing the chains
  • running the chains
  • examining the results

Upvotes: 0

Matt Denwood
Matt Denwood

Reputation: 2583

The error is coming from the R function jags.model (not from JAGS) - you are trying to use arguments parameters.to.save etc to the wrong function.

If you want to keep the model as similar to WinBUGS as possible, there is an easier way than specifying the data and initial values in R. Put the following into a text file called 'model.txt' in your working directory:

model{
  y ~ dbin(theta, n) # Model the data 
  ytilde ~ dbin(theta, m) # Prediction of future binomial 
  theta ~ dbeta(a, b) # The prior 
  prob <- step(ytilde - 20) # Pred prob that ytilde >= 20
} 

data{
  list(n=100, m=100, y=10, a=1, b=1) # The data 
}

inits{
  list(theta=0.5, ytilde=10) # Starting/initial values
}

And then run this in R:

library('runjags')
results <- run.jags('model.txt', monitor='theta')
results
plot(results)

For more information on this method of translating WinBUGS models to JAGS see: http://runjags.sourceforge.net/quickjags.html

Matt

Upvotes: 2

Related Questions