knitr::opts_chunk$set(
  fig.path = "README_figs/README-"
)

jagsUI: Run JAGS from R

CRAN status R build status

This package runs JAGS (Just Another Gibbs Sampler) analyses from within R. It acts as a wrapper and alternative interface for the functions in the rjags package and adds some custom output and graphical options. It also makes running chains in parallel quick and easy.

Installation

You can install the package from CRAN, or get the development version from Github:

devtools::install_github('kenkellner/jagsUI')

You will also need to separately install JAGS, which you can download here.

Example

library(jagsUI)

Format data:

jags_data <- list(
  gnp = longley$GNP,
  employed = longley$Employed,
  n = nrow(longley)
)

Write BUGS model file:

modfile <- tempfile()
writeLines("
model{

  # Likelihood
  for (i in 1:n){ 
    # Model data
    employed[i] ~ dnorm(mu[i], tau)
    # Calculate linear predictor
    mu[i] <- alpha + beta*gnp[i]
  }

  # Priors
  alpha ~ dnorm(0, 0.00001)
  beta ~ dnorm(0, 0.00001)
  sigma ~ dunif(0,1000)
  tau <- pow(sigma,-2)

}
", con=modfile)

Set initial values and parameters to save:

inits <- function(){  
  list(alpha=rnorm(1,0,1),
       beta=rnorm(1,0,1),
       sigma=runif(1,0,3)
  )  
}

params <- c('alpha','beta','sigma')

Run JAGS:

out <- jags(data = jags_data,
            inits = inits,
            parameters.to.save = params,
            model.file = modfile,
            n.chains = 3,
            n.adapt = 100,
            n.iter = 1000,
            n.burnin = 500,
            n.thin = 2)

View output:

out

Acknowledgments



kenkellner/jagsUI documentation built on Feb. 4, 2024, 5:20 a.m.