我尝试使用WinBUGS
from R
via BRugs
and R2WinBUGS
,代码如下:
require(R2WinBUGS)
require(BRugs)
model<-function(){
for(i in 1:N){
y[i] <- x[i] + w[i]
w[i] ~ dnorm(0, sigma.y)
x[i] <- a - b*5 + v[i]
v[i] ~ dnorm(0, sigma.x)
}
a ~ dunif(0, 1)
b ~ dunif(-1, 1)
sigma.y ~ dgamma(0.1, 0.1)
sigma.x ~ dgamma(0.1, 0.1)
}
write.model(model, con = "model.bug")
modelCheck("model.bug")
# model is syntactically correct
N = 10
y = rnorm(100)
data = list(N = N, y = y)
inits = function(){
list(a = runif(1, 0, 1), b = runif(1, -1, 1), sigma.x= rgamma(1, 0.1, 0.1),
sigma.y = rgamma(1, 0.1, 0.1))
}
parameters = c("a", "b", "sigma.x", "sigma.y")
result.sim <- bugs(data, inits, parameters, "model.bug",
n.chains = 1, n.iter = 1000,
program= "winbugs",
working.directory = NULL,
debug = T)
结果没有出来,我发现了部分log.txt
的WinBUGS
:
display(log)
check(C:/Users/ADMINI~1.PC-/AppData/Local/Temp/RtmpkrnOoc/model.bug.txt)
model is syntactically correct
data(C:/Users/ADMINI~1.PC-/AppData/Local/Temp/RtmpkrnOoc/data.txt)
data loaded
compile(1)
multiple definitions of node y[1]
inits(1,C:/Users/ADMINI~1.PC-/AppData/Local/Temp/RtmpkrnOoc/inits1.txt)
command #Bugs:inits cannot be executed (is greyed out)
gen.inits()
command #Bugs:gen.inits cannot be executed (is greyed out)
thin.updater(1)
update(500)
command #Bugs:update cannot be executed (is greyed out)
set(a)
很明显error
is multiple definitions of node y[1]
,但它是什么意思?我不认为y[1]
有多个定义,因为我使用y[i]
但y
不在loop
.