How to convert WINBUGS code into JAGS/R code

I am trying to rerun the WinBugs code used in the paper ‘A Bayesian Hierarchical Model for Risk Assessment of Methymercury’. I would like to be able to implement the same code but into JAGS (through the R Studio packages “rjags” and “coda”. The WinBugs code is as follows

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#Model formulation: Inverse Gaussian
model
[
for (i in 1:row)
[
y[i] <- b[i]
p.y[i] <- .1/pow(((bu[i]-b[i])/1.64),2)
p2.y[i] ~dgamma(p.y[i],.1)
y[i] ~ dnorm(mu[i], p2.y[i])
mu[i] ~ dnorm(gamma[study[i]], p.tau)
bmdso[i] <- 1/mu[i]
]
for (i in 1:nstudy) [
gamma[i] ~ dnorm(m.gam,p.gamma)
bmds[i] <- 1 / gamma[i]
]
m.gam ~ dnorm(0, .0001)
bmd <- 1 / m.gam
p.tau ~ dgamma(.001,.001)
p.gamma ~ dgamma(.001,.001)
sigo <- 1 / sqrt(p.tau)
sigs <- 1 / sqrt(p.gamma)
]
#Model formulation: Lognormal
model
[
for (i in 1:row)
[
y[i] <- log(1/b[i])
dc[i] <- .1/pow((log(y[i])-log(1/bu[i]))/1.64,2)
d[i] ~dgamma(dc[i],.1)
y[i] ~ dnorm(mu[i], d[i])
mu[i] ~ dnorm(gamma[study[i]], p.tau)
bmdso[i] <- exp(mu[i])
for (i in 1:nstudy)
[
gamma[i] ~ dnorm(m.gam,p.gamma)
bmds[i] <- exp(gamma[i])
]
m.gam ~ dnorm(3, 0.0001)
bmd <- exp( m.gam )
p.tau ~ dgamma(.001,.001)
p.gamma ~ dgamma(.001,.001)
sigo <- 1 / sqrt(p.tau)
sigs <- 1 / sqrt(p.gamma)
]
#Data input
list(b=c(
0.0081 , 0.0083 , 0.0122 , 0.0483 , 0.0091 , 0.01 ,
0.0829 , 0.0814 , 0.0786 , 0.1215 , 0.0777 ,
0.0502 , 0.0566 , 0.0353 , 0.0657 , 0.0368 ),
bu=c(
0.0402 , 0.0434 , 0.0447 , 0.0585 , 0.0426 , 0.0447 ,
0.1764 , 0.1596 , 0.165 , 0.2275 , 0.1653 ,
0.0829 , 0.0976 , 0.0677 , 0.0998 , 0.0697 ),
study=c(1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
row=16, nstudy=3)
</code>
<code>#Model formulation: Inverse Gaussian model [ for (i in 1:row) [ y[i] <- b[i] p.y[i] <- .1/pow(((bu[i]-b[i])/1.64),2) p2.y[i] ~dgamma(p.y[i],.1) y[i] ~ dnorm(mu[i], p2.y[i]) mu[i] ~ dnorm(gamma[study[i]], p.tau) bmdso[i] <- 1/mu[i] ] for (i in 1:nstudy) [ gamma[i] ~ dnorm(m.gam,p.gamma) bmds[i] <- 1 / gamma[i] ] m.gam ~ dnorm(0, .0001) bmd <- 1 / m.gam p.tau ~ dgamma(.001,.001) p.gamma ~ dgamma(.001,.001) sigo <- 1 / sqrt(p.tau) sigs <- 1 / sqrt(p.gamma) ] #Model formulation: Lognormal model [ for (i in 1:row) [ y[i] <- log(1/b[i]) dc[i] <- .1/pow((log(y[i])-log(1/bu[i]))/1.64,2) d[i] ~dgamma(dc[i],.1) y[i] ~ dnorm(mu[i], d[i]) mu[i] ~ dnorm(gamma[study[i]], p.tau) bmdso[i] <- exp(mu[i]) for (i in 1:nstudy) [ gamma[i] ~ dnorm(m.gam,p.gamma) bmds[i] <- exp(gamma[i]) ] m.gam ~ dnorm(3, 0.0001) bmd <- exp( m.gam ) p.tau ~ dgamma(.001,.001) p.gamma ~ dgamma(.001,.001) sigo <- 1 / sqrt(p.tau) sigs <- 1 / sqrt(p.gamma) ] #Data input list(b=c( 0.0081 , 0.0083 , 0.0122 , 0.0483 , 0.0091 , 0.01 , 0.0829 , 0.0814 , 0.0786 , 0.1215 , 0.0777 , 0.0502 , 0.0566 , 0.0353 , 0.0657 , 0.0368 ), bu=c( 0.0402 , 0.0434 , 0.0447 , 0.0585 , 0.0426 , 0.0447 , 0.1764 , 0.1596 , 0.165 , 0.2275 , 0.1653 , 0.0829 , 0.0976 , 0.0677 , 0.0998 , 0.0697 ), study=c(1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), row=16, nstudy=3) </code>
#Model formulation: Inverse Gaussian
model
[
for (i in 1:row)
[
y[i] <- b[i]
p.y[i] <- .1/pow(((bu[i]-b[i])/1.64),2)
p2.y[i] ~dgamma(p.y[i],.1)
y[i] ~ dnorm(mu[i], p2.y[i])
mu[i] ~ dnorm(gamma[study[i]], p.tau)
bmdso[i] <- 1/mu[i]
]
for (i in 1:nstudy) [
gamma[i] ~ dnorm(m.gam,p.gamma)
bmds[i] <- 1 / gamma[i]
]
m.gam ~ dnorm(0, .0001)
bmd <- 1 / m.gam
p.tau ~ dgamma(.001,.001)
p.gamma ~ dgamma(.001,.001)
sigo <- 1 / sqrt(p.tau)
sigs <- 1 / sqrt(p.gamma)
]
#Model formulation: Lognormal
model
[
for (i in 1:row)
[
y[i] <- log(1/b[i])
dc[i] <- .1/pow((log(y[i])-log(1/bu[i]))/1.64,2)
d[i] ~dgamma(dc[i],.1)
y[i] ~ dnorm(mu[i], d[i])
mu[i] ~ dnorm(gamma[study[i]], p.tau)
bmdso[i] <- exp(mu[i])
for (i in 1:nstudy)
[
gamma[i] ~ dnorm(m.gam,p.gamma)
bmds[i] <- exp(gamma[i])
]
m.gam ~ dnorm(3, 0.0001)
bmd <- exp( m.gam )
p.tau ~ dgamma(.001,.001)
p.gamma ~ dgamma(.001,.001)
sigo <- 1 / sqrt(p.tau)
sigs <- 1 / sqrt(p.gamma)
]
#Data input
list(b=c(
0.0081 , 0.0083 , 0.0122 , 0.0483 , 0.0091 , 0.01 ,
0.0829 , 0.0814 , 0.0786 , 0.1215 , 0.0777 ,
0.0502 , 0.0566 , 0.0353 , 0.0657 , 0.0368 ),
bu=c(
0.0402 , 0.0434 , 0.0447 , 0.0585 , 0.0426 , 0.0447 ,
0.1764 , 0.1596 , 0.165 , 0.2275 , 0.1653 ,
0.0829 , 0.0976 , 0.0677 , 0.0998 , 0.0697 ),
study=c(1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
row=16, nstudy=3)

The current JAGS/R code that I have is this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Install packages if not already installed
install.packages("rjags")
install.packages("coda")
# Load required packages
library(rjags)
library(coda)
# Data
data <- list(
b = c(0.0081, 0.0083, 0.0122, 0.0483, 0.0091, 0.01, 0.0829, 0.0814, 0.0786, 0.1215, 0.0777, 0.0502, 0.0566, 0.0353, 0.0657, 0.0368),
bu = c(0.0402, 0.0434, 0.0447, 0.0585, 0.0426, 0.0447, 0.1764, 0.1596, 0.165, 0.2275, 0.1653, 0.0829, 0.0976, 0.0677, 0.0998, 0.0697),
study = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
row = 16,
nstudy = 3
)
# JAGS model
model_string <- "
model {
for (i in 1:row) {
y[i] <- log(1 / b[i])
dc[i] <- 0.1 / pow((log(y[i]) - log(1 / bu[i])) / 1.64, 2)
d[i] ~ dgamma(dc[i], 0.1)
y[i] ~ dnorm(mu[i], d[i])
mu[i] ~ dnorm(gamma[study[i]], p.tau)
bmdso[i] <- exp(mu[i])
}
for (i in 1:nstudy) {
gamma[i] ~ dnorm(m.gam, p.gamma)
bmds[i] <- exp(gamma[i])
# No redundant definition of y[i] here
}
m.gam ~ dnorm(3, 0.0001)
bmd <- exp(m.gam)
p.tau ~ dgamma(0.001, 0.001)
p.gamma ~ dgamma(0.001, 0.001)
sigo <- 1 / sqrt(p.tau)
sigs <- 1 / sqrt(p.gamma)
}
"
# Initial values
inits <- list(
m.gam = 3,
gamma = rnorm(data$nstudy, 3, 0.0001),
p.tau = 0.001,
p.gamma = 0.001
)
# Parameters to monitor
parameters <- c("bmds", "bmd", "sigo", "sigs", "mu", "gamma", "m.gam")
# Run JAGS
jags <- jags.model(file = model_string, data = data, inits = inits, n.chains = 3, n.adapt = 5000, quiet = FALSE)
# Check convergence and summarize results
summary(jags)
</code>
<code># Install packages if not already installed install.packages("rjags") install.packages("coda") # Load required packages library(rjags) library(coda) # Data data <- list( b = c(0.0081, 0.0083, 0.0122, 0.0483, 0.0091, 0.01, 0.0829, 0.0814, 0.0786, 0.1215, 0.0777, 0.0502, 0.0566, 0.0353, 0.0657, 0.0368), bu = c(0.0402, 0.0434, 0.0447, 0.0585, 0.0426, 0.0447, 0.1764, 0.1596, 0.165, 0.2275, 0.1653, 0.0829, 0.0976, 0.0677, 0.0998, 0.0697), study = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), row = 16, nstudy = 3 ) # JAGS model model_string <- " model { for (i in 1:row) { y[i] <- log(1 / b[i]) dc[i] <- 0.1 / pow((log(y[i]) - log(1 / bu[i])) / 1.64, 2) d[i] ~ dgamma(dc[i], 0.1) y[i] ~ dnorm(mu[i], d[i]) mu[i] ~ dnorm(gamma[study[i]], p.tau) bmdso[i] <- exp(mu[i]) } for (i in 1:nstudy) { gamma[i] ~ dnorm(m.gam, p.gamma) bmds[i] <- exp(gamma[i]) # No redundant definition of y[i] here } m.gam ~ dnorm(3, 0.0001) bmd <- exp(m.gam) p.tau ~ dgamma(0.001, 0.001) p.gamma ~ dgamma(0.001, 0.001) sigo <- 1 / sqrt(p.tau) sigs <- 1 / sqrt(p.gamma) } " # Initial values inits <- list( m.gam = 3, gamma = rnorm(data$nstudy, 3, 0.0001), p.tau = 0.001, p.gamma = 0.001 ) # Parameters to monitor parameters <- c("bmds", "bmd", "sigo", "sigs", "mu", "gamma", "m.gam") # Run JAGS jags <- jags.model(file = model_string, data = data, inits = inits, n.chains = 3, n.adapt = 5000, quiet = FALSE) # Check convergence and summarize results summary(jags) </code>
# Install packages if not already installed
install.packages("rjags")
install.packages("coda")

# Load required packages
library(rjags)
library(coda)

# Data
data <- list(
  b = c(0.0081, 0.0083, 0.0122, 0.0483, 0.0091, 0.01, 0.0829, 0.0814, 0.0786, 0.1215, 0.0777, 0.0502, 0.0566, 0.0353, 0.0657, 0.0368),
  bu = c(0.0402, 0.0434, 0.0447, 0.0585, 0.0426, 0.0447, 0.1764, 0.1596, 0.165, 0.2275, 0.1653, 0.0829, 0.0976, 0.0677, 0.0998, 0.0697),
  study = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
  row = 16,
  nstudy = 3
)

# JAGS model
model_string <- "
model {
  for (i in 1:row) {
    y[i] <- log(1 / b[i])
    dc[i] <- 0.1 / pow((log(y[i]) - log(1 / bu[i])) / 1.64, 2)
    d[i] ~ dgamma(dc[i], 0.1)
    y[i] ~ dnorm(mu[i], d[i])
    mu[i] ~ dnorm(gamma[study[i]], p.tau)
    bmdso[i] <- exp(mu[i])
  }

  for (i in 1:nstudy) {
    gamma[i] ~ dnorm(m.gam, p.gamma)
    bmds[i] <- exp(gamma[i])
    # No redundant definition of y[i] here
  }

  m.gam ~ dnorm(3, 0.0001)
  bmd <- exp(m.gam)
  p.tau ~ dgamma(0.001, 0.001)
  p.gamma ~ dgamma(0.001, 0.001)

  sigo <- 1 / sqrt(p.tau)
  sigs <- 1 / sqrt(p.gamma)
}
"

# Initial values
inits <- list(
  m.gam = 3,
  gamma = rnorm(data$nstudy, 3, 0.0001),
  p.tau = 0.001,
  p.gamma = 0.001
)

# Parameters to monitor
parameters <- c("bmds", "bmd", "sigo", "sigs", "mu", "gamma", "m.gam")

# Run JAGS
jags <- jags.model(file = model_string, data = data, inits = inits, n.chains = 3, n.adapt = 5000, quiet = FALSE)

# Check convergence and summarize results
summary(jags)

But it is giving me the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>**Error in jags.model(file = textConnection(model_string), data = data, :
RUNTIME ERROR:
Compilation error on line 7.
Attempt to redefine node y[1]**
</code>
<code>**Error in jags.model(file = textConnection(model_string), data = data, : RUNTIME ERROR: Compilation error on line 7. Attempt to redefine node y[1]** </code>
**Error in jags.model(file = textConnection(model_string), data = data,  : 
  RUNTIME ERROR:
Compilation error on line 7.
Attempt to redefine node y[1]**

I don’t know how to proceed from here is there anyone that could help me out please?

New contributor

Valerie Esposito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật