winbugs中节点错误的多个定义。

时间:2021-08-16 17:08:45

I try to use WinBUGS from R via BRugs and R2WinBUGS, the code is followinig:

我试着用R通过BRugs和R2WinBUGS来使用WinBUGS,代码如下:

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)

The result didn't come out, and I find out part of the log.txt of WinBUGS:

结果没有出来,我发现了部分日志。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)

it is obvious that the error is multiple definitions of node y[1], but what does it mean? I don't think y[1] has multiple definitions since I use y[i] but not y in the loop.

显然,该错误是节点y[1]的多个定义,但它是什么意思呢?我不认为y[1]有多个定义,因为我在循环中使用y[I]而不是y。

2 个解决方案

#1


2  

You tend to get the multiple definitions error when you have not correctly defined the likelihood of your model. If you have y in your data, you will need to state a distribution for y in your model. At the moment your y in the model is set up as a deterministic (rather than random) node. Depending on what your actual model is you could set

当您没有正确定义模型的可能性时,您倾向于得到多个定义错误。如果你的数据中有y,你需要在你的模型中说明y的分布。此时,模型中的y被设置为一个确定性(而不是随机)节点。取决于你的实际模型是什么。

y[i] ~ dnorm(x[i], w[i])

You would then have to have a different prior distribution (something only positive) on each tolerance w[i].

然后你必须有一个不同的先验分布(只有正的)在每个公差上w[i]。

#2


1  

In your model y[i] has a normal distribution, with mean x[i], and variance defined by the variance of v[i] plus the variance of w[i]. So that will give you the appropriate parameters to use in y[i] ~ dnorm(x[i] , prec[i]). Note that the normal distribution in BUGS is defined by the precision = 1 / variance, so prec[i] <- 1 / (1/sigma.y + 1/sigma.x). Assuming sigma is the precision - though that's confusing notation because sigma is usually a standard deviation.

在你的模型中,y[i]有一个正态分布,均值为x[i],方差为v[i]的方差加上w[i]的方差。因此,这将给出在y[i] ~ dnorm(x[i], prec[i])中使用的适当参数。注意,bug中的正态分布是由精度= 1/方差定义的,所以prec[i] <- 1/ (1/sigma)。y + 1 / sigma.x)。假设是精确的,尽管这是一种混乱的符号,因为标准差通常是标准差。

#1


2  

You tend to get the multiple definitions error when you have not correctly defined the likelihood of your model. If you have y in your data, you will need to state a distribution for y in your model. At the moment your y in the model is set up as a deterministic (rather than random) node. Depending on what your actual model is you could set

当您没有正确定义模型的可能性时,您倾向于得到多个定义错误。如果你的数据中有y,你需要在你的模型中说明y的分布。此时,模型中的y被设置为一个确定性(而不是随机)节点。取决于你的实际模型是什么。

y[i] ~ dnorm(x[i], w[i])

You would then have to have a different prior distribution (something only positive) on each tolerance w[i].

然后你必须有一个不同的先验分布(只有正的)在每个公差上w[i]。

#2


1  

In your model y[i] has a normal distribution, with mean x[i], and variance defined by the variance of v[i] plus the variance of w[i]. So that will give you the appropriate parameters to use in y[i] ~ dnorm(x[i] , prec[i]). Note that the normal distribution in BUGS is defined by the precision = 1 / variance, so prec[i] <- 1 / (1/sigma.y + 1/sigma.x). Assuming sigma is the precision - though that's confusing notation because sigma is usually a standard deviation.

在你的模型中,y[i]有一个正态分布,均值为x[i],方差为v[i]的方差加上w[i]的方差。因此,这将给出在y[i] ~ dnorm(x[i], prec[i])中使用的适当参数。注意,bug中的正态分布是由精度= 1/方差定义的,所以prec[i] <- 1/ (1/sigma)。y + 1 / sigma.x)。假设是精确的,尽管这是一种混乱的符号,因为标准差通常是标准差。