Reputation: 1409
The code is following:
require(BRugs)
require(R2WinBUGS)
model<-function(){
for(i in 1:N){
y[i] ~ dnorm(x[i], sigma.y)
}
x[1] ~ dnorm(theta[1], sigma.y)
theta[1] <- 0
for(j in 2:N){
x[j] ~ dnorm(theta[j], sigma.x)
theta[j] <- b*x[j-1] # this row wrong,
# it would be right when I set theta[j] <- 1*x[j-1]
}
a ~ dunif(0, 1)
b ~ dunif(-1, 1)
sigma.y ~ dgamma(0.1, 0.1)
sigma.x ~ dgamma(0.1, 0.1)
}
data <- list( N <- 100, y <- rnorm(100))
data=list(N=100,y=rnorm(100))
inits=function(){
list(sigma.x = rgamma(1,0.1,0.1), sigma.y = rgamma(1, 0.1, 0.1), a = dnorm(1, 0, 1), b = dnorm(1, -1, 1))
}
parameters=c("a", "b", "x")
write.model(model, con = "model.bug")
modelCheck("model.bug")
# model is syntactically correct
ret.sim <- bugs(data, inits, parameters, "model.bug",
n.chains = 1, n.iter = 1000,
program= "winbugs",
working.directory = NULL,
debug = T)
I don't know why, the program will be correct when I replace theta[j] <- b*x[j-1]
with theta[j] <- 1*x[j-1]
, but I have defined b ~ dunif(-1, 1)
. Indeed, I need to set theta[j] <- a - b*x[j-1]
in the final model, and it turns out to be wrong when I try to add a
and b
into it. Anyone find where the problem is ?
Upvotes: 2
Views: 324
Reputation: 8366
The problems is in your priors for b (and most likely a). I don't know your data but perhaps the range of your current priors do not include true values of a and b. I would think that if you use a continuous distribution(s):
a ~ dnorm(0,1)
b ~ dnorm(0,1)
your problem might be solved?
n.b. If you are trying to create a AR(1) model for WinBUGS you might want to check out the tsbugs package.
Upvotes: 2