jags.samples: Generate posterior samples

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

View source: R/jags.R

Description

Function to extract random samples from the posterior distribution of the parameters of a jags model.

Usage

1
2
jags.samples(model, variable.names, n.iter, thin = 1,
             type="trace", force.list=FALSE, ...)

Arguments

model

a jags model object

variable.names

a character vector giving the names of variables to be monitored

n.iter

number of iterations to monitor

thin

thinning interval for monitors

type

type of monitor (can be vectorised)

force.list

option to consistently return a named list of monitor types even if a single monitor type is requested

...

optional arguments passed to the update method for jags model objects

Details

The jags.samples function creates monitors for the given variables, runs the model for n.iter iterations and returns the monitored samples.

Value

A list of mcarray objects, with one element for each element of the variable.names argument. If more than one type of monitor is requested (or if force.list is TRUE) then the return value will be a (named) list of lists of mcarray objects, with one element for each monitor type.

Author(s)

Martyn Plummer

See Also

jags.model, coda.samples

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  data(LINE)
  LINE$recompile()
  LINE.samples <- jags.samples(LINE, c("alpha","beta","sigma"),
  n.iter=1000)
  LINE.samples
  LINE.samples <- jags.samples(LINE, c("alpha","beta","sigma"),
  force.list=TRUE, n.iter=1000)
  LINE.samples
  LINE.samples <- jags.samples(LINE, c("alpha","alpha"),
  n.iter=1000, type=c("trace","mean"))
  LINE.samples$trace
  LINE.samples$mean

Example output

Loading required package: coda
Linked to JAGS 4.2.0
Loaded modules: basemod,bugs
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 5
   Unobserved stochastic nodes: 3
   Total graph size: 40

Initializing model

$alpha
mcarray:
[1] 3.006265

Marginalizing over: iteration(1000),chain(2) 

$beta
mcarray:
[1] 0.7949662

Marginalizing over: iteration(1000),chain(2) 

$sigma
mcarray:
[1] 1.006746

Marginalizing over: iteration(1000),chain(2) 

$alpha
mcarray:
[1] 2.994909

Marginalizing over: iteration(1000),chain(2) 

$beta
mcarray:
[1] 0.7942458

Marginalizing over: iteration(1000),chain(2) 

$sigma
mcarray:
[1] 1.016406

Marginalizing over: iteration(1000),chain(2) 

Warning message:
In jags.samples(LINE, c("alpha", "alpha"), n.iter = 1000, type = c("trace",  :
  Failed to set trace monitor for alpha
Monitor already exists and cannot be duplicated

NULL
NULL

rjags documentation built on Nov. 6, 2019, 5:07 p.m.