简体   繁体   中英

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.

Error in jags.model(file = "RhoModeldef.txt", data = ModelData, inits = ModelInits, : Error parsing model file: syntax error on line 4 near "~"

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? Please let me know if more details are needed.

Thanks!

Line 4 of the file 'RhoModeldef.txt' is most likely this one:

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. 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:

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.

Does that solve your problem? Your example is not self-contained so I can't check myself, but I guess it should work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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