简体   繁体   English

尝试使用 RJAGS 解析贝叶斯 model 时出现语法错误

[英]Syntax error when trying to parse Bayesian model using RJAGS

I'm running the following code to attempt Bayesian modelling using rjags but coming up with the syntax error below.我正在运行以下代码以尝试使用 rjags 进行贝叶斯建模,但出现以下语法错误。

Error in jags.model(file = "RhoModeldef.txt", data = ModelData, inits = ModelInits, : Error parsing model file: syntax error on line 4 near "~" jags.model(file = "RhoModeldef.txt", data = ModelData, inits = ModelInits, : Error in parsing model 文件错误:第 4 行“~”附近的语法错误

RhoModel.def <- function() {
  for (s in 1:S) {
    log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))
    rho[s] ~ dgamma(Kappa,Beta)
  }
  Kappa ~ dt(0,2.5,1) # dt(0, pow(2.5,-2), 1) https://stackoverflow.com/questions/34935606/cauchy-prior-in-jags https://arxiv.org/pdf/0901.4011.pdf 
  sig.k <- abs(Kappa)
  Beta ~ dt(0,2.5,1)
  sig.b <- abs(Beta)
}

S <- length(africasad21)-1 # integer
Rhohat <- afzip30$Rho # vector
Rhovar <- afzip30$RhoVar # vector

ModelData <-list(S=S,rhohat=Rhohat,rhovar=Rhovar)

ModelInits <-  list(list(rho = rep(1,S),Kappa=0.1,Beta=0.1))

Model.1 <- jags.model(file = 'RhoModeldef.txt',data = ModelData,inits=ModelInits,
                              n.chains = 4, n.adapt = 100)

Does anyone have any ideas how I might be able to fix this?有谁知道我如何解决这个问题? I'm thinking it might have something to do with my attempts to fit a logged model?我在想这可能与我尝试安装已记录的 model 有关吗? Please let me know if more details are needed.如果需要更多详细信息,请告诉我。

Thanks!谢谢!

Line 4 of the file 'RhoModeldef.txt' is most likely this one:文件“RhoModeldef.txt”的第 4 行很可能是这一行:

log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))

JAGS does not allow log transformations on the left hand side of stochastic relations, only deterministic ones. JAGS 不允许在随机关系左侧进行对数转换,只允许确定性关系。 Given that you are providing rhohat as data, the easiest solution is to do the log transformation in R and drop that part in JAGS ie:鉴于您将 rhohat 作为数据提供,最简单的解决方案是在 R 中进行对数转换并将该部分放入 JAGS,即:

log_rhohat[s] ~ dnorm(log(rho[s]), log(rhovar[s]))

ModelData <-list(S=S, log_rhohat=log(Rhohat), rhovar=Rhovar)

Alternatively, you could use dlnorm rather than dnorm in JAGS.或者,您可以在 JAGS 中使用 dlnorm 而不是 dnorm。

Does that solve your problem?这能解决你的问题吗? Your example is not self-contained so I can't check myself, but I guess it should work now.你的例子不是独立的,所以我无法检查自己,但我想它现在应该可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM