[R-sig-Geo] Failure to call GeoBUGS's car.normal() function from R
Aquarius
yiyiwang.ut at gmail.com
Tue Nov 29 02:01:11 CET 2011
Dear Users,
I am trying to do a spatial Poisson-gamma model. I intend to read my data
into R & then call OpenBugs/GeoBUGS/WinBugs to implement the car.normal
function.
Here is a copy for my codes:
# in R:
# do Poisson-gamma spatial models using CAR
# y= number of employments x=distance to cbd
library(spdep)
library(rjags)
#Read data
taz = read.csv("file path", header=TRUE)
y=taz[,9] # number of employment
distcbd=taz[,10] # dist2cbd
# compute adjacency matrix and adj, N, sumNumNeigh required by car.normal
function in GeoBUGS
shape <- readShapePoly("file path", IDvar="FID2")
shape_nb <- poly2nb(shape)
N= length(shape_nb)
num=sapply(shape_nb,length)
adj=unlist(shape_nb)
sumNumNeigh=length(unlist(shape_nb))
# create initial values
tau.b = 0.5
tau.h = 0.2
alpha=0
beta=0
# setup and compile the jags model. I used jags to call OpenBUGS from R.
# to do adaptation for. Adaptation is a way to get better samples, you don't
# have to do this but it'll help speed convergence in some cases.
jags <- jags.model("model1.txt",
data=list("O"=y, "N"=N, "distcbd"=distcbd, "num"=num,
"adj"=adj,"sumNumNeigh"=sumNumNeigh),
init=list("tau.b"=tau.b, "tau.h"=tau.h, "alpha"=alpha, "beta"=beta),
n.chains=1,
n.adapt=0)
# Burn-in.
update(jags, 500)
# Draw samples and store them.
samples <- coda.samples(jags, c('alpha', 'beta'), 2000) # only store alpha
& beta
#### this is how the model1.txt look like (so this is OpenBUGS codes)
model {
for (i in 1 : N) {
# Likelihood
O[i] ~ dpois(mu[i])
log(mu[i]) <- alpha + beta * distcbd[i] + b[i] + h[i]
RR[i] <- exp(alpha + beta * distcbd[i] + b[i] + h[i]) # Area-specific
relative risk (for maps)
# Exchangeable prior on unstructured random effects
h[i] ~ dnorm(0, tau.h)
}
# CAR prior distribution for spatial random effects:
b[1:N] ~ car.normal(adj[], weights[], num[], tau.b)
for(k in 1:sumNumNeigh) {
weights[k] <- 1
}
# Other priors:
alpha ~ dflat()
beta ~ dnorm(0.0, 1.0E-5)
tau.b ~ dgamma(0.5, 0.0005)
sigma.b <- sqrt(1 / tau.b)
tau.h ~ dgamma(0.5, 0.0005)
sigma.h <- sqrt(1 / tau.h)
}
However, I got an error message "RUNTIME ERROR:Compilation error on line
16.Unknown distribution: dflat
". When I changed the flat prior (dflat) into a uniform prior
(dunif(-1000,1000)). I got another error message "RUNTIME ERROR:Compilation
error on line 11.Unknown distribution: car.normal."
I've been banging my head against the wall trying to solve this mistery but
to no avail. Does anyone can help me out? I appreciate very much for your
time and expertise!
Another more salient point I've been confused about is which package should
I use to do such a spatial model? WinBUGS or OpenBUGS? It seems that GeoBUGS
is built in both of them & I installed both WInBUGS 14 (& then upgrade to
1.4.3) and OpenBUGS version 3.2.1 rev 781. My R is 2.13.2. Is there anything
in particular I should do with the WInBUGS or OpenBUGS, whichever one I
should use, when running my codes in R? I just opened WinBUGS/OpenBUGS when
running the codes in R....
Thanks so much!
All the best,
Yiyi
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Failure-to-call-GeoBUGS-s-car-normal-function-from-R-tp7041139p7041139.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list