Sunday, February 08, 2009

Our new R package: R2jags

I have got emails occasionally from JAGS users, asking about our new R package: R2jags. Basically, R2jags runs JAGS via R and makes postanalysis easier to be done in R. Taking advantage of the functions provided by JAGS, rjags and R2WinBUGS, R2jags allows users to run BUGS in the same way as they would do it in R2WinBUGS. Nonetheless, R2jags has some powerful features that facilitate the BUGS model fitting:

  1. If your model does not converge, update it. If you used to be a R2WinBUGS user, you must feel frustrated that your model does not converge. The best thing you can do is to use your current states of parameters as the starting values for the next MCMC. On the other hand, in R2jags, you just type in the R console:

    fit <- update(fit)
    This will update the model.
  2. If you have to shutdown your machine but the model is still not converged.... In R2jags, you can go ahead and save the R workspace and shutdown your machine. When you are ready to run the model again, load the workspace in R and type:

    fit.upd <- update(fit)
    This will recompile the model, which means you can update the model again!!

If you want to explore more features of the R2jags, just type ?jags in the R console. The example code contains all the functions in the R2jags.

Installing JAGS and rjags can be tricky in the Mac OS or in the Linux system. I have a blog entry here that shows how this can be done. If you are not a Window user, this post might help you.


Will said...

Hi Yu-Sung,

Is there a reason why I might get an error when using the jags() function even if I can estimate the same model using the bugs() function (R2WinBUGS)? I know I've installed JAGS correctly because the schools example works for me.

The error is:

Error in jags.model(model.file, data = data, inits = inits, n.chains = n.chains, :

Parse error on line 1

And if I use the jags2() function, I get a seemingly more helpful error:

Welcome to JAGS 1.0.3 on Wed Feb 25 13:44:31 2009
JAGS is free software and comes with ABSOLUTELY NO WARRANTY
Loading module: basemod
Loading module: bugs
Deleting model
Clearing data table
syntax error, unexpected ',', expecting NAME

Parse error on line 1
Reading data file jagsdata.txt
Compiling model graph
Nothing to compile

Thanks for your help. I hope I'm not wasting your time with a user error.

Yu-Sung Su said...


Make sure you have R2jags_0.0-16 and
rjags_1.0.3-5. I was informed by Dr Plummer that he made changed in rjags_1.0.3-5. So I updated R2jags_0.0-16 to fit the change. So based on the error message, it seems you don't have the most updated packages.

Will said...

Unfortunately it doesn't appear so simple.

I'm running ‘rjags’ version 1.0.3-5 and ‘R2jags’ version 0.01-16

If I do find out what the problem, I'll be sure to post an update.


Yu-Sung Su said...

By the way, there indess exist some differnence between BUGS model code and JAGS code. The most obvious differnece is the way to speicify constraint. If you can post your model file here, maybe I can find out what the problem is.

Will said...

Below is the model code. Given your hunch, it's mostly my about setting the constraints on the cutpoints for the ordered logit.

model {
for (i in 1:n){
y[i] ~ dcat(a[i,])
a[i, 1] <- max( min(A[i, 1] ,1), 0)
a[i, 2] <- max( min(A[i, 2] - A[i, 1],1), 0)
a[i, 3] <- max( min(A[i, 3] - A[i, 2],1), 0)
a[i, 4] <- max( min(A[i, 4] - A[i, 3],1), 0)
a[i, 5] <- max( min(1 - A[i, Ncut], 1), 0)

for (j in 1:Ncut){
logit(A[i, j]) <- z[j] - mu[i]

mu[i] <- (b.0 +[fr[i]] + a.age[age[i]] +[edu[i]] +[inc[i]] + a.district[district[i]])

z[1] ~ dnorm(0, 0.0001)I( , z[2])
z[2] ~ dnorm(0, 0.0001)I(z[1], z[3])
z[3] ~ dnorm(0, 0.0001)I(z[2], z[4])
z[4] ~ dnorm(0, 0.0001)I(z[3], )

b.0 ~ dnorm (0, .0001)
for (j in {[j] ~ dnorm(0,}
for (j in 1:n.age) {a.age[j] ~ dnorm(0, tau.age)}
for (j in {[j] ~ dnorm(0,}
for (j in {[j] ~ dnorm(0,}

b.PrezVotePercRepub ~ dnorm (0, .0001)

for (j in 1:n.district) {
a.district[j] ~ dnorm(a.district.hat[j], tau.district)
a.district.hat[j] <- (a.state[state[j]] + b.PrezVotePercRepub*PrezVotePercRepub[j])}
for (j in 1:n.state) {
a.state[j] ~ dnorm(a.state.hat[j], tau.state)
a.state.hat[j] <- (a.div[div[j]])}
for (j in 1:n.div) {
a.div[j] ~ dnorm(a.div.hat[j], tau.div)
a.div.hat[j] <- a.region[region[j]]}
for (j in 1:n.region) {a.region[j] ~ dnorm(0, tau.region)} <- pow(, -2)
tau.age <- pow(sigma.age, -2) <- pow(, -2) <- pow(, -2)
tau.district <- pow(sigma.district, -2)
tau.state <- pow(sigma.state, -2)
tau.div <- pow(sigma.div, -2)
tau.region <- pow(sigma.region, -2) ~ dunif(0, 100)
sigma.age ~ dunif(0, 100) ~ dunif(0, 100) ~ dunif(0, 100)
sigma.district ~ dunif(0,100)
sigma.state ~ dunif(0,100)
sigma.div ~ dunif(0, 100)
sigma.region ~ dunif(0, 100)

Yu-Sung Su said...

It is this part:

z[1] ~ dnorm(0, 0.0001)I( , z[2])
z[2] ~ dnorm(0, 0.0001)I(z[1], z[3])
z[3] ~ dnorm(0, 0.0001)I(z[2], z[4])
z[4] ~ dnorm(0, 0.0001)I(z[3], )

It should be

for (i in 1:4) {
z0[i] ~ dnorm(0, .0001)
z[1:4] <- sort(z0)

Please check p32-33 of JAGS manual for details:

ofir said...
This comment has been removed by the author.
lx said...

Hi Yu-Sung,

I was installing R2jags on linux system where I don't have full authority. I installed JAGS under my local folder, and installed rjags with command from your blog, install.packages("rjags", configure.args="--with-jags-include=/usr/local/include/JAGS --with-jags-lib=/usr/local/lib/JAGS --with-jags-modules=/usr/local/lib/JAGS/modules"), with /usr/local being replaced by my own folder.

Then when I was installing R2jags, I got the error that cannot be found. Does R2jags has any option like the --with-jags-lib option? Thank you very much!

lx said...

Hi Yu-Sung,

After I posted my previous question, I just found out that library(rjags) is not working because cannot find file, although it looked like I had successfully installed rjags... Does that mean I must install the JAGS .so files in /usr/local/lib64 (which I don't have full authority)? Thank you again.

Yu-Sung Su said...


I think the problem is your installation of rjags and JAGS. R2jags is just a wrapper of these two. I think you need to find the missing .so file and specify it correctly when installing rjags.

唐文涛 said...

Hi Yu-Sung,

Thanks for your great work on "R2jags". I have two small questions about the difference between the 2 packages:"rjags" and "R2jags" I found these two packages use different functions to create a jags model. "jags.model()" in "rjags" and "jags()" in "R2jags". I tried to just use one set of initial values and set "n.chains=2" when running the model with both "jags.model()" and "jags()". It seems OK with "jags.model()". But when using "jags()", the error message says: Number of initialized chains (length(inits)) != n.chains. Can you tell me why this difference happened? Do I have to use 2 sets of initials when setting n.chains=2?

And also I wonder whether there is any obvious difference in the performance or speed of convergence between "jags.model()" and "jags()" and even "bugs"? I'm trying to run a complicated model with a large amout of data.I dont know which package to choose, "R2jags", "rjags", "R2WinBugs". Can you kindly give me some advice?