Recently, I have been using jags software through the R platform to assist with my research. I am also very grateful to Professor Martyn Plummer for his prompt and thorough reply before. After solving the convergence problem, I tried to extend the model to a two-layer structure, but there was one problem that confused me for a long time.
I am really new to the nested structure, so I am really struggling to identify potential causes. the R code and corresponding data are attached. Can you give me some advice on how to modify this problem?
Thank you very much and I am very much looking forward to your reply!
}
"
writeLines( linear_regression_model, con = "E:\paper7\test3\step4\linearRegr.txt")
# Original Data------------------------------------------------------------------------------- ** ## Use test data sub1
y <- c(387.0186165,104.6276693,40.1140719,500.2035043,477.7629738,355.0654012,5.1475330,5.5493703,3.7096185,0.3787187,172.1065280)
nevents <- length(y)
nsite <- 1
DA1 <-c(9834)
Elev1 <- c(150)
prcp <- c(0.0,0.0,2.5,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.2)
et <-c(2316,2236,2055,2322,2417,1973,1810,2154,1796,1724,2504) # Prepare Data------------------------------------------------------------------------------- ## prepare the data for JAGS**
dat = dump.format(list(y=y,DA=DA1,Elev=Elev1,ntotal=ntotal,nsite=nsite,prcp=prcp,et=et))
## read the summary of the results
results
plot(results$mcmc)
The error message is as follows:
The following error occured when compiling and adapting the model using rjags:
Error in rjags::jags.model(model, data = dataenv, inits = inits, n.chains = length(runjags.object$end.state), :
RUNTIME ERROR:
Compilation error on line 30.
Attempt to redefine node mu.c[1]
And the relationship which I want to build is also attached:
Recently, I have been using jags software through the R platform to assist with my research. I am also very grateful to Professor Martyn Plummer for his prompt and thorough reply before. After solving the convergence problem, I tried to extend the model to a two-layer structure, but there was one problem that confused me for a long time.
I am really new to the nested structure, so I am really struggling to identify potential causes. the R code and corresponding data are attached. Can you give me some advice on how to modify this problem?
Thank you very much and I am very much looking forward to your reply!
R code with data:
library(runjags)
linear_regression_model = "
model {
# Prior #
mu.a ~ dnorm(0,0.00001)
mu.b ~ dnorm(0,0.00001)
mu.c ~ dnorm(0,0.00001)
tau.a ~ dunif(0.00001,1000)
tau.b ~ dunif(0.00001,1000)
tau.c ~ dunif(0.00001,1000)
a1 ~ dnorm(0,0.00001) #
b1 ~ dnorm(0,0.00001) #
c1 ~ dnorm(0,0.00001) #
a2 ~ dnorm(0,0.00001) #
b2 ~ dnorm(0,0.00001) #
c2 ~ dnorm(0,0.00001) #
a3 ~ dnorm(0,0.00001) #
b3 ~ dnorm(0,0.00001) #
c3 ~ dnorm(0,0.00001) #
for (i in 1:nsite){
** # Prior #**
A1 ~ dnorm(mu.a,tau.a) #
B1 ~ dnorm(mu.b,tau.b) #
C1 ~ dnorm(mu.c,tau.c) #
for (t in 1:ntotal) {
** # Level 1 #*
y ~ dgamma(shape, scale)
log(shape) <- A1 + B1 * log(prcp) + C1 log(et)
** # Level 2 #
mu.a <- a1 + b1 * log(DA) + c1 log(Elev)
mu.b <- a2 + b2 * log(DA) + c2 log(Elev)
mu.c <- a3 + b3 * log(DA) + c3* log(Elev)
}
}
}
"
writeLines( linear_regression_model, con = "E:\paper7\test3\step4\linearRegr.txt")
# Original Data------------------------------------------------------------------------------- **
## Use test data sub1
y <- c(387.0186165,104.6276693,40.1140719,500.2035043,477.7629738,355.0654012,5.1475330,5.5493703,3.7096185,0.3787187,172.1065280)
nevents <- length(y)
nsite <- 1
DA1 <-c(9834)
Elev1 <- c(150)
prcp <- c(0.0,0.0,2.5,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.2)
et <-c(2316,2236,2055,2322,2417,1973,1810,2154,1796,1724,2504)
# Prepare Data-------------------------------------------------------------------------------
## prepare the data for JAGS**
dat = dump.format(list(y=y,DA=DA1,Elev=Elev1,ntotal=ntotal,nsite=nsite,prcp=prcp,et=et))
# Initialize chains-------------------------------------------------------------------------------
inits1 = dump.format(list(A1 = 0, B1 = 0, C1 = 3, scale= 5,
mu.a = 0, mu.b = 0, mu.c = 3,
tau.a = 0, tau.b = 0, tau.c = 3,
a1 = 0, b1 = 0, c1 = 3,
a2 = 0, b2 = 0, c2 = 3,
a3 = 0, b3 = 0, c3= 3, .RNG.name="base::Super-Duper", .RNG.seed=99999))
inits2 = dump.format(list(A1 = 0, B1 = 0, C1 = 3, scale= 5,
mu.a = 0, mu.b = 0, mu.c = 3,
tau.a = 0, tau.b = 0, tau.c = 3,
a1 = 0, b1 = 0, c1 = 3,
a2 = 0, b2 = 0, c2 = 3,
a3 = 0, b3 = 0, c3= 3, .RNG.name="base::Wichmann-Hill", .RNG.seed=12345))
inits3 = dump.format(list(A1 = 0, B1 = 0, C1 = 3, scale= 5,
mu.a = 0, mu.b = 0, mu.c = 3,
tau.a = 0, tau.b = 0, tau.c = 3,
a1 = 0, b1 = 0, c1 = 3,
a2 = 0, b2 = 0, c2 = 3,
a3 = 0, b3 = 0, c3= 3, .RNG.name="base::Mersenne-Twister", .RNG.seed=1802))
## Tell JAGS which latent variables to monitor
monitor = c("A1", "B1", "C1",'scale',"mu.a", "mu.b", "mu.c","tau.a", "tau.b", "tau.c","a1", "b1", "c1","a2", "b2", "c2","a3", "b3", "c3")
# Run JAGS-------------------------------------------------------------------------------
results <- run.jags(model = "E:\paper7\test3\step4\linearRegr.txt",
monitor = monitor,
data = dat,
n.chains = 3,
inits = c(inits1,inits2,inits3),
plots = FALSE,
burnin = 4000,
sample = 5000,
thin = 100)
## read the summary of the results
results
plot(results$mcmc)
The error message is as follows:
The following error occured when compiling and adapting the model using rjags:
Error in rjags::jags.model(model, data = dataenv, inits = inits, n.chains = length(runjags.object$end.state), :
RUNTIME ERROR:
Compilation error on line 30.
Attempt to redefine node mu.c[1]
And the relationship which I want to build is also attached:
Last edit: Ju xiaopei 2023-05-17