jags.fit: Fit JAGS models with cloned data

Description Usage Arguments Value Author(s) See Also Examples

View source: R/jags.fit.R

Description

Convenient functions designed to work well with cloned data arguments and JAGS.

Usage

1
2
3
jags.fit(data, params, model, inits = NULL, n.chains = 3, 
    n.adapt = 1000, n.update = 1000, thin = 1, n.iter = 5000, 
    updated.model = TRUE, ...)

Arguments

data

A named list or environment containing the data. If an environment, data is coerced into a list.

params

Character vector of parameters to be sampled.

model

Character string (name of the model file), a function containing the model, or a or custommodel object (see Examples).

inits

Optional specification of initial values in the form of a list or a function (see Initialization at jags.model). If NULL, initial values will be generated automatically. It is an error to supply an initial value for an observed node.

n.chains

Number of chains to generate.

n.adapt

Number of steps for adaptation.

n.update

Number of updates before iterations. It is usually a bad idea to use n.update=0 if n.adapt>0, so a warning is issued in such cases.

thin

Thinning value.

n.iter

Number of iterations.

updated.model

Logical, if the updated model should be attached as attribute (this can be used to further update if convergence was not satisfactory, see updated.model and update.mcmc.list).

...

Further arguments passed to coda.samples, and update.jags (e.g. the progress.bar argument).

Value

An mcmc.list object. If data cloning is used via the data argument, summary returns a modified summary containing scaled data cloning standard errors (scaled by sqrt(n.clones), see dcsd), and R_{hat} values (as returned by gelman.diag).

Author(s)

Peter Solymos, solymos@ualberta.ca

See Also

Underlying functions: jags.model, update.jags, coda.samples

Parallel chain computations: jags.parfit

Methods: dcsd, confint.mcmc.list.dc, coef.mcmc.list, quantile.mcmc.list, vcov.mcmc.list.dc

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
## Not run: 
if (require(rjags)) {
## simple regression example from the JAGS manual
jfun <- function() {
    for (i in 1:N) {
        Y[i] ~ dnorm(mu[i], tau)
        mu[i] <- alpha + beta * (x[i] - x.bar)
    }
    x.bar <- mean(x[])
    alpha ~ dnorm(0.0, 1.0E-4)
    beta ~ dnorm(0.0, 1.0E-4)
    sigma <- 1.0/sqrt(tau)
    tau ~ dgamma(1.0E-3, 1.0E-3)
}
## data generation
set.seed(1234)
N <- 100
alpha <- 1
beta <- -1
sigma <- 0.5
x <- runif(N)
linpred <- crossprod(t(model.matrix(~x)), c(alpha, beta))
Y <- rnorm(N, mean = linpred, sd = sigma)
## list of data for the model
jdata <- list(N = N, Y = Y, x = x)
## what to monitor
jpara <- c("alpha", "beta", "sigma")
## fit the model with JAGS
regmod <- jags.fit(jdata, jpara, jfun, n.chains = 3)
## model summary
summary(regmod)
## data cloning
dcdata <- dclone(jdata, 5, multiply = "N")
dcmod <- jags.fit(dcdata, jpara, jfun, n.chains = 3)
summary(dcmod)
}

## End(Not run)

Example output

Loading required package: coda
Loading required package: parallel
Loading required package: Matrix
dclone 2.1-2 	 2016-03-11
Loading required package: rjags
Linked to JAGS 4.2.0
Loaded modules: basemod,bugs
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 100
   Unobserved stochastic nodes: 3
   Total graph size: 515

Initializing model

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 500
   Unobserved stochastic nodes: 3
   Total graph size: 1315

Initializing model


Iterations = 1001:6000
Thinning interval = 1 
Number of chains = 3 
Sample size per chain = 5000 
Number of clones = 5

1. Empirical mean and standard deviation for each variable,
   plus standard error of the mean:

         Mean      SD   DC SD  Naive SE Time-series SE R hat
alpha  0.6026 0.02136 0.04776 0.0001744      0.0001764     1
beta  -1.0226 0.07613 0.17022 0.0006216      0.0006185     1
sigma  0.4746 0.01504 0.03364 0.0001228      0.0001245     1

2. Quantiles for each variable:

         2.5%     25%     50%     75%   97.5%
alpha  0.5607  0.5882  0.6026  0.6170  0.6447
beta  -1.1696 -1.0732 -1.0222 -0.9722 -0.8734
sigma  0.4462  0.4641  0.4739  0.4844  0.5054

dclone documentation built on May 2, 2019, 6:08 p.m.