This is a general wrapper function for fitting a model to data using the JAGS MCMC algorithm.
The function uses code adapted from the rjags
package. Options include
calculating the Deviance Information Criterion (DIC) and running sampling chains in parallel.
fit_jags( jags_data, jags_model, params, n_chain = 2, n_burn = 1000, n_samp = 1000, n_thin = 1, DIC = FALSE, parallel = FALSE )
jags_data | a list of data objects |
---|---|
jags_model | character string giving the model specification in JAGS/BUGS syntax |
params | vector of parameters to monitor |
n_chain | number of MCMC sampling chains |
n_burn | number of iterations to discard before sampling of chains begins (burn in) |
n_samp | number of iterations to sample each chain |
n_thin | interval to thin samples |
DIC | logical indicating whether or not to calculate the Deviance Information Criterion (DIC) |
parallel | logical indicating whether or not to run MCMC chains in parallel or sequentially (default = FALSE). An even number of chains is recommended when running in parallel. |
an mcmc.list
object
If JAGS library not already installed, install here: https://sourceforge.net/projects/mcmc-jags/files/JAGS/3.x/
John Giles
N <- 100 lambda <- 10 y <- rpois(n=N, lambda=lambda) jags_data <- list(N=N, y=y) jags_model <- " model { for(i in 1:N){ y[i] ~ dpois(lambda) } lambda ~ dgamma(1, 0.01) }" params <- c('lambda') mod <- fit_jags(jags_data=jags_data, jags_model=jags_model, params=params)#> Compiling model graph #> Resolving undeclared variables #> Allocating nodes #> Graph information: #> Observed stochastic nodes: 100 #> Unobserved stochastic nodes: 1 #> Total graph size: 104 #> #> Initializing model #>