fit_jags: Fit Bayesian model using MCMC

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/models.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fit_jags(
  jags_data,
  jags_model,
  params,
  n_chain = 2,
  n_burn = 1000,
  n_samp = 1000,
  n_thin = 1,
  DIC = FALSE,
  parallel = FALSE
)

Arguments

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.

Details

If JAGS library not already installed, install here: https://sourceforge.net/projects/mcmc-jags/files/JAGS/3.x/

Value

an mcmc.list object

Author(s)

John Giles

See Also

Other model: check(), compare(), fit_prob_travel(), mobility(), predict(), residuals(), summary()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)

COVID-19-Mobility-Data-Network/mobility documentation built on Nov. 22, 2021, 12:17 a.m.