代码如下:
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)
我不知道为什么,当我替换为时程序会正确theta[j] <- b*x[j-1]
,theta[j] <- 1*x[j-1]
但我已经定义了b ~ dunif(-1, 1)
. 确实,我需要theta[j] <- a - b*x[j-1]
在最终模型中进行设置,当我尝试添加a
并b
进入它时结果是错误的。有谁发现问题出在哪里?