0

I am relatively new to R and OpenBugs and have spent a lot of time troubleshooting this model. I have been able to figure out a fair amount of them on my own through resources online, but I am stuck on this error. It says that are "multiple definitions of node dummyy[1]". I read online that this error is often caused by trying to define a variable within a for-loop that does not have an index, but my variables do. I created this model based off the resource here.

I struggling with locating the error. The code listed below should produce the same error I am seeing. I also included the log error I saw on OpenBugs. Thank you for taking the time to assist me.

Data:

library(R2OpenBUGS)
n1=20 ; k1=1 ; m1=5; R.x=c(3,3,3,3,3); x=c(1.008195, 1.212885, 1.349857, 1.909607, 7.134668)  
n2=20 ; k2=1 ; m2=5; R.y=c(3,3,3,3,3); y=c(0.7507421, 1.3103649, 1.5022302, 1.7875087, 3.1900460) 

Model:

mtemp<-function(){ 
  for (i in 1:m1)
  {
    dummyx[i]<-0
    dummyx[i] ~ dloglik(logLikex[i])
    logLikex[i] <- -log(a)-log(c)-(c-1.0)*log(x[i])+(a*k1*(R.x[i]+1.0)+1.0)*log(1.0 + pow(x[i],c))  
  for(j in 1:m2){  
    dummyy[j]<-0
    dummyy[j] ~ dloglik(logLikey[j])
    logLikey[j] <- -log(b)-log(c)-(c-1.0)*log(y[j])+(b*k2*(R.y[j]+1.0)+1.0)*log(1.0 + pow(y[j],c))
  }
  a ~ dgamma(0.001, 0.0001)
  b ~ dgamma(0.001, 0.0001)
  c ~ dgamma(0.001, 0.0001)
  }
}

model.file <- file.path(tempdir(), "model.txt") #create temporary directory
write.model(mtemp, model.file) #write to temporary directory

file.show(model.file) #verify model was created

 datatemp<- list( "x","y","R.x","k1","m1","R.y","k2","m2")
  initstemp<-function(){list(a=7.0,b=7.0,c=4.5)}
  bugstemp = bugs(data=datatemp,inits=initstemp,parameters=c("a","b","c"),model.file=model.file,
                  n.chains=3,n.iter= 10000, n.burnin=1000,n.thin=1, debug=T)

Log Report:

model is syntactically correct
data loaded
multiple definitions of node dummyy[1]
model must have been compiled but not updated to be able to change RN generator
BugsCmds:NoCompileInits
BugsCmds:NoCompileInits
BugsCmds:NoCompileInits
model must be compiled before generating initial values
model must be initialized before updating
model must be initialized before monitors used
model must be initialized before monitors used
model must be initialized before monitors used
model must be initialized before monitors used
model must be initialized before DIC can be monitored
model must be initialized before updating
model must be initialized before monitors used
DIC monitor not set

1 Answers1

2

You have put the closing brace for the m1 loop at the end of the model, rather than before the start of the m2 loop. That means that all dummyy, loglikey, as well as a b and c are defined m1 times.

Edit: Just to be clear, your model should be:

for (i in 1:m1)
{
    dummyx[i]<-0
    dummyx[i] ~ dloglik(logLikex[i])
    logLikex[i] <- ...
}
for(j in 1:m2)
{  
    dummyy[j]<-0
    dummyy[j] ~ dloglik(logLikey[j])
    logLikey[j] <- ...
}
a ~ dgamma(0.001, 0.0001)
b ~ dgamma(0.001, 0.0001)
c ~ dgamma(0.001, 0.0001)

And not as you currently have:

for (i in 1:m1)
{
    dummyx[i]<-0
    dummyx[i] ~ dloglik(logLikex[i])
    logLikex[i] <- ...

for(j in 1:m2)
{  
    dummyy[j]<-0
    dummyy[j] ~ dloglik(logLikey[j])
    logLikey[j] <- ...
}
a ~ dgamma(0.001, 0.0001)
b ~ dgamma(0.001, 0.0001)
c ~ dgamma(0.001, 0.0001)
}

Hope that helps,

Matt

Matt Denwood
  • 2,537
  • 1
  • 12
  • 16