Question

For my research I need to estimate variance, and insert these estimates into the scale matrix (called R in my JAGS-code) of my wishart distribution in JAGS. I use R2JAGS (on a mac) and I would like to know how I could do this with R/JAGS code? I want to do this automatically because I need to run a simulation study.

So I got the following R-code for running my model:

Res.NoCorr <- jags(data, model.file = "LVL_3_MODEL_SD_NoRanEff.bug", parameters = 
c("sigma.mean.mu", "sigma.mean.beta", "sigma.mean.phi"), n.chains = 1, n.iter = itt, 
n.burnin = bi, DIC = FALSE)

Res.NoCorr

Inference for Bugs model at "LVL_3_MODEL_SD_NoRanEff.bug", fit using jags,
 1 chains, each with 1e+05 iterations (first 10000 discarded), n.thin = 90
 n.sims = 1000 iterations saved
                mu.vect sd.vect  2.5%   25%   50%   75% 97.5%
sigma.mean.beta   0.121   0.054 0.021 0.082 0.123 0.160 0.222
sigma.mean.mu     1.345   0.608 0.070 0.955 1.398 1.800 2.386
sigma.mean.phi    0.096   0.014 0.069 0.087 0.095 0.106 0.126

This is my JAGS code for inserting the estimates manually:

Prec.Mat[1:3, 1:3] ~ dwish(R[,], 3) # prior for precision matrix random effects

est.sd.mu <- 1.345
est.sd.beta <- 0.121
est.sd.phi <- 0.096

est.var.mu <- est.sd.mu * est.sd.mu
est.var.beta <- est.sd.beta * est.sd.beta
est.var.phi <- est.sd.phi * est.sd.phi

variance.estimation.mu <- est.var.mu * 3
variance.estimation.beta <- est.var.beta * 3
variance.estimation.phi <- est.var.phi * 3

R[1,1] <- variance.estimation.mu
R[2,2] <- variance.estimation.beta
R[3,3] <- variance.estimation.phi
R[1,2] <- 0
R[1,3] <- 0
R[2,3] <- 0
R[2,1] <- 0
R[3,1] <- 0
R[3,2] <- 0

So in this case, I need to program the insertion of the numbers of est.sd.mu, est.sd.beta and est.sd.phi.

Était-ce utile?

La solution

If you write your JAGS file as a brew package template you can do something like this:

est.sd.mu <- <%= Xmu %>
est.sd.beta <- <%= Xbeta %>
est.sd.phi <- <%= Xphi %>
[etc]

Then then you process this with the brew function to generate the JAGS file, with the R variables Xmu, Xbeta, and Xphi set to the values you want to replace.

As an illustration using text for the template:

> Xmu=-1.23
> brew(text="est.sd.mu <- <%= Xmu %>")
est.sd.mu <- -1.23
> 

But you should use a file for the template using the file= and output= args to specify input template and output JAGS files.

You could do it by using paste to assemble a character version of the file in R which you then save as a JAGS file, but that's just duplicating what brew is designed for.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top