Skip to content

Help - caling OpenBugs thourgh R2OpenBugs problem

11 messages · James Rooney, Virgilio Gómez-Rubio, Yuster Ronoh

#
Hi all,

I am working on a Bayesian spatial smoothing project - and have implemented the BYM model, similar to the description in Chapter 11 of the ASDAR book. However, I have been having a huge problem calling OpenBugs from within R and I cannot figure out why. I would veyr much liek to do this as it would speed up the whole process. I'm stared at this for a omnth or two now to no avail so I'm hoping that somebody might spot a mistake. Basically when I call OpenBugs from R using the bugs command through R2OpenBugs - it throws a non-specific error and tells me to heck the bugs log file. The problem is Bugs does not generate one. This is made all the weirder by the fact that if I manually start up bugs and start it running using the data and inits files gnerated by R2OpenBugs - it runs fine without complaint. This is how I have been working so far. But I want to parallelize Bugs using Snow - so I need R2OpenBugs to work properly. I'm hoping someone here can help!!!

I've made a simulation to recreate the problem. My apologies it is a little complex, but I wanted to use the shapefile I'm actually working with which is freely downloadable for those willing to take a look.
So here goes.

1. Go here for the shapefile:
http://www.cso.ie/en/census/census2011boundaryfiles/

2. Click on the word "Disclaimer" to access the shapefiles (should you agree with the disclaimer!!). After clicking through the Disclaimer stuff, the file to download is: "Census2011_Electoral_Divisions_generalised20m.zip"

3. Set up a directory for this and save the shapefiles there.

4. You will need a bugs model. Cut and paste the below into a txt file and save it to your directory under the name "spatial_CAR.txt"

model
{

  for(i in 1:N)
  {
    observed[i]~dpois(mu[i])    
    log(theta[i])<-alpha + u[i] + v[i]
    mu[i]<-expected[i]*theta[i]
    u[i] ~dnorm(0,tau)
  }

v[1:N]~car.normal(adj[],weights[],num[],precv)

  alpha ~dflat()
  tau ~dgamma(0.001,0.001)
  precv ~dgamma(0.1,0.1)
}

5. Now open R and paste in the R code below. Remember to set your working directory to the same directory you saved the shapefiles and bugs model into. Also you might need to modify the path to OpenBugs on your machine.

6. Run the code. For me the final line throws the error:
"Error in bugs.run(n.burnin, OpenBUGS.pgm, debug = debug, WINE = WINE,  : 
  Look at the log file in  C:......<SNIP>................./R2OpenBugs Simulation  and
try again with 'debug=TRUE' to figure out what went wrong within OpenBUGS."

I cannot figure out why!!! The log file does not exist!! If you manually open bugs and use the data and init files it works. Why ? No idea!!!!
Any and all help much appreciated!!!
Apologies for the length of this!!

James


R Code:

library(rgdal)
library(spdep)
library(R2OpenBUGS)

setwd("....your working directory here......")

#Read in Spatial Polygon Data Frame
SPolyDF<-readOGR(".","Census2011_Electoral_Divisions_generalised20m",TRUE)

# Detect neighbours
nb<-poly2nb(SPolyDF,queen=T,snap=0.1) # snap at 100m distance

# Create nb weights
nb.w<-nb2listw(nb,zero.policy=T,style="B")

# Create BUGS CAR weights
nbBugs<-listw2WB(nb.w)

# Simulate some count data for observed and expected cases
N<-nrow(SPolyDF)
SPolyDF$obs<-sample.int(100, N, TRUE)
SPolyDF$exp<-sample.int(100, N, TRUE)

# Prep Data for OpenBugs
d<-list(N = N, observed = SPolyDF$obs, expected = SPolyDF$exp, adj=nbBugs$adj, weights=nbBugs$weights, num=nbBugs$num)

# Specify dir where bugs lives
BugsDir<-"C:/Program Files (x86)/OpenBUGS/OpenBUGS322"

# Define initial values for bugs
inits<-list(
   list(u=rep(0,N),v=rep(0,N),alpha=0,tau=0.0001,precv=0.002)
   ,list(u=rep(0,N),v=rep(0,N),alpha=0.1,tau=0.0002,precv=0.001))

# Define working directory as current directory
wdir=getwd()

# Openbugs call
MCMCres<-bugs(data=d,inits=inits,parameters.to.save =c("theta","alpha","tau","precv"),
              n.iter=1000,model.file="spatial_CAR.txt",n.chains=2,n.burnin=100,n.thin=1, 
              OpenBUGS.pgm=BugsDir,working.directory=wdir,codaPkg=F,debug=T)
4 days later
#
Hi everyone,

Has anyone looked at this problem below ?
Even if nobody has an answer I'd be keen to know if the problem replicated for others.

Many thanks,

James
#
Dear James,

I'll be happy to look into this but I will need more information about
your system. Also, could you run the code using 'debug=TRUE' as seen in
the error message?

Best wishes,

Virgilio
On mi?, 2013-11-27 at 20:13 +0000, James Rooney wrote:
#
Dear Virgilio,

Many thanks for taking a look at it!

That  'debug = TRUE' is already set!!!! It doesn't make any difference!!

I'm running R in Windows 7 through Parallels on a Macbook Pro  (OSx Mavericks). I have shut down OsX and booted purely into windows and it doesn't make any difference.
I've experienced this problem under both R 2.15 and R 3.01. It also happens both in R Studio and in the standard R interface. I have Openbugs version 3.2.2. Running latest version of the packages used.

My thinking is that something is crashing OpenBugs when it attempts to open - but I cant' figure out how or why. I wonder is there a file size limit for passing things into OpenBugs ?

Appreciate any help!
James
#
Hi Yuster,

Did you download the correct shapefile into "C:/Users/Public/shapefile" ?? There are alot of files there and I could not link directly to the file due to the "Disclaimer" page.

The other thing I find if I am having these kinds of errors from readOGR, is to paste the full pathway in quote into the readOGR instead of "."
In other words for your pathway if you are sure the right file is in the folder try this:

SPolyDF<-readOGR("C:/Users/Public/shapefile","Census2011_Electoral_Divisions_generalised20m",TRUE)

James
#
Hi Yuster,

Did you unzip the file? Sorry I forgot to say that in instructions. You need to put the unzipped file into that directory.
James
#
I don't know then Yuster. Is your rgdal package up to date ?
#
Dear James,

I have been able to run your code. See attached files. Not sure why
OpenBUGS crashed but I found a few strange symbols (vertical bars) when
I copied and pasted your model from your e-mail. OpenBUGS complained and
it worked fine after I had these symbols removed.

I have run it on an Ubuntu Linux, with R version 3.0.2 Patched
(2013-10-25 r64109). I have used wine to run OpenBUGS, so you may need
to change that in my code. Please, try the code attached on the latest R
you can find.

If you have any particular problem when running my code, please, contact
me off-list so that we can reduce the noise of this list.

Best wishes,

Virgilio
-------------- next part --------------
model
{

for(i in 1:N)
{
 observed[i]~dpois(mu[i])
 log(theta[i])<-alpha + u[i] + v[i]
 mu[i]<-expected[i]*theta[i]
 u[i] ~dnorm(0,tau)
 }

v[1:N]~car.normal(adj[],weights[],num[],precv)

alpha ~dflat()
tau ~dgamma(0.001,0.001)
precv ~dgamma(0.1,0.1)
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testOpenBUGS.R
Type: text/x-r-source
Size: 1372 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20131129/6a126a3b/attachment.bin>