R - OpenBugs - 节点错误的多重定义 - 自定义分布

时间:2016-09-28 01:59:57

标签: r bayesian winbugs openbugs

我对R和OpenBugs比较陌生,并花了很多时间对这个模型进行故障排除。我已经能够通过在线资源自己找出相当数量的它们,但我仍然坚持这个错误。它说是"节点dummyy的多个定义[1]"。我在网上看到,这个错误通常是由于尝试在没有索引的for循环中定义变量而引起的,但我的变量确实如此。我基于资源here创建了这个模型。

我努力找到错误。下面列出的代码应该产生我看到的相同错误。我还包括我在OpenBugs上看到的日志错误。感谢您抽出宝贵时间来帮助我。

数据:

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) 

型号:

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)

日志报告:

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 个答案:

答案 0 :(得分:2)

您已将m1循环的右括号放在模型的末尾,而不是在m2循环开始之前。这意味着所有虚拟,loglikey以及b和c都被定义了m1次。

编辑:为了清楚起见,您的模型应该是:

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)

而不是你现在所拥有的:

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)
}

希望有所帮助,

马特