0

I'm new to JAGS and bayesian estimation and trying to fit a model described by Wang & McArdle (DOI: 10.1080/10705510701758265) in JAGS via R.

Here's my code:

# generate data for reprex
m1 <- matrix(runif(20000, 0, 1), ncol=40)
colnames(m1) <- paste0("m1_d", 1:40)
m2 <- matrix(runif(20000, 0, 1), ncol=40)
colnames(m2) <- paste0("m2_d", 1:40)

# define model
model.loc <- "model.txt"

cat("
model{
# likelihood for y1 and y2
    for( i in 1 : nsubj ) {
        for( j in 1 : ntime ) {

    y1[i , j] ~ dnorm(muy1[i , j],tauy1)
    muy1[i , j] <- b[i,1] + b[i,2] * x[i , j] + b[i,3] * (max(0,b[i,4]-x[i , j]))*(max(0,b[i,4]-x[i , j] ))

    y2[i , j] ~ dnorm(muy2[i , j],tauy2)
    muy2[i , j] <- b[i,5] + b[i,6] * x[i , j] + b[i,7] * (max(0,b[i,8]-x[i , j] ))*(max(0,b[i,8]-x[i , j] ))

} }

# distribution for the random-effects parameters
for( i in 1 : nsubj ) {
    b[i,1:8]~dmnorm(mub[1:8], taub[1:8,1:8])
}

# priors
# prior distribution for the inverse of the level-1 residual variance of y1 and y2
tauy1 ~ dgamma(0.1,0.001)
tauy2 ~ dgamma(0.1,0.001)

# prior distribution of the fixed parameters
mub[1:8]~dmnorm(mean[1:8],prec[1:8,1:8])

# prior distribution of the inverse of the covariance matrix of random-effects parameters
taub[1:8, 1:8] ~ dwish(R[1:8, 1:8], 8)

sigma2[1:8, 1:8] <- inverse(taub[1:8, 1:8])
for (i in 1 : 8) {sigmab[i] <- sqrt(sigma2[i, i])}
    sigmay1 <- 1 / sqrt(tauy1)
    sigmay2 <- 1 / sqrt(tauy2)
}
", file = model.loc)


# create list of data
jags.data <- list("y1" = m1,
                  "y2" = m2,
                  "ntime" = 40,
                  "nsubj" = 500)

# define inits
jags.inits <- list(list(.RNG.seed=1,
                        .RNG.name="base::Mersenne-Twister"),
                   list(.RNG.seed=2,
                        .RNG.name="base::Mersenne-Twister"),
                   list(.RNG.seed=3,
                        .RNG.name="base::Mersenne-Twister"))

# create vector of parameters to monitor in JAGS
jags.params <- c("muy1", "tauy1")

#' run model
mod_lgm_cp_as <- jags(jags.data,
                      parameters.to.save = jags.params,
                      inits = jags.inits,
                      model.file = model.loc,
                      n.chains = 3,
                      n.burnin = 5000,
                      n.thin = 1,
                      n.iter = 10000)

When I run this code I get the error message

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Compilation error on line 29.
Unknown variable R
Either supply values for this variable with the data
or define it  on the left hand side of a relation.

I'm not exactly sure how and where to provide jags with the relevant variable. Do I have to provide initial values for it or does it have to come from my data? Can anyone please give me some advice?

achmed
  • 1

0 Answers0