[R-sig-Geo] Help - caling OpenBugs thourgh R2OpenBugs problem

James Rooney ROONEYJ4 at tcd.ie
Sat Nov 23 17:30:43 CET 2013


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)



More information about the R-sig-Geo mailing list