Question

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

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:

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.

Était-ce utile?

La solution

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[i] ~ dnorm(x[i], w[i])

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

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top