The bugs
file is here.
## model {
## for (i in 1:N){
## y[i] ~ dnorm(pred[i],tau)
## pred[i] <- ma*a[i] + mb*b[i] + mc*c[i] + int
## }
## ma ~ dnorm(0, .0001)
## mb ~ dnorm(0, .0001)
## mc ~ dnorm(0, .0001)
## int ~ dnorm(0, .0001)
## tau ~ dgamma(.001, .001)
## }
jags1 <- jags(model.file='../code/bayes.bug',
parameters=c("ma","mb","mc","int"),
data = namedList(a, b, c, N, y),
n.chains = 4,
inits=NULL)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 40
## Unobserved stochastic nodes: 5
## Total graph size: 329
##
## Initializing model
- You can use
inits=NULL
to have JAGS pick the initial values randomly from the priors. For more complex models you might want to pick starting values for the chains yourself (see the ?jags
documentation).
Examine the chains and output
bb <- jags1$BUGSoutput ## extract the "BUGS output" component
mm <- as.mcmc.bugs(bb) ## convert it to an "mcmc" object that coda can handle
plot(jags1) ## large-format graph
## plot(mm) ## trace + density plots, same as above
xyplot(mm,layout=c(2,3)) ## prettier trace plot
densityplot(mm,layout=c(2,3)) ## prettier density plot
## print(dwplot(jags1)) ## estimate + credible interval plot
tt_jags <- tidy(jags1, conf.int = TRUE)
gg_jags <- ggplot(tt_jags, aes(estimate, term, xmin = conf.low, xmax = conf.high)) + geom_pointrange()
print(gg_jags)
The lump.mcmc.list
function from the emdbook
package can be useful for converting a set of MCMC chains into a single long chain.