fetchMCMC: Create a list of objects for analysis with package "coda".

View source: R/query-functions.R

fetchMCMCR Documentation

Create a list of objects for analysis with package "coda".

Description

Create an object of class "mcmc.list", or a list of objects of class "mcmc.list", which can be analysed using the diagnostic functions in package coda.

Usage

fetchMCMC(filename, where = NULL, nSample = 25, sample = NULL, thinned = TRUE)

Arguments

filename

The name of the file where the output from the estimate function is kept.

where

A character vector used to select a single parameter or batch of parameters. See below for details.

nSample

Number of parameters to be sampled from a batch of parameters. Defaults to 25. Ignored when batch has fewer than 25 parameters (including having only one parameter.)

sample

Indices of parameters to be sampled. An alternative to nSample when more control over sampling is required.

thinned

Logical. If TRUE, the default, the "mcmc.list" object or objects describe the thinned process.

Details

If no where argument is supplied, mcmc.list objects are returned for every parameter or batch of parameters that were estimated. If a where argument is supplied, it must describe the path to parameter or parameters. See fetch for more on specifying paths.

If a batch of parameters has many elements, then calculating MCMC diagnositics for all those elements can be very slow. To speed things up, when applied to a batch of parameters, fetchMCMC randomly selects only nSample of these parameters. nSample defaults to 25.

Alternatively, the indices of the parameters to be selected can be specified using the sample argument. See below for an example.

If thinned is TRUE, then the thin argument in coda function mcmc is set to 1; otherwise thin is set to nThin, extracted from object.

If the model in question contains structural zeros (see Poisson), some parameters of the model may be undefined. These parameters are omitted from the results, unless they are specifically requested via the sample argument.

Value

A single object of class "mcmc.list", or a named list of such objects.

See Also

Other functions for examining output from calls to estimateModel, estimateCounts, and estimateAccount include fetch, fetchFiniteSD, and listContents.

Examples

library(demdata)
## fit model
deaths <- Counts(round(VADeaths2))
popn <- Counts(VAPopn)
filename <- tempfile()
estimateModel(Model(y ~ Poisson(mean ~ age)),
              y = deaths,
              exposure = popn,
              filename = filename,
              nBurnin = 20,
              nSim = 20,
              nChain = 2,
              parallel = FALSE)

## create a list containing an "mcmc.list" object for
## every element of the results that was estimated
l <- fetchMCMC(filename)
names(l)
sapply(l, class)

## analyse using functions from 'coda'
## Not run: 
library(coda)
plot(l$model.likelihood.rate)
plot(l$model.likelihood.rate, ask = FALSE)
plot(l$model.likelihood.rate, ask = FALSE, smooth = FALSE)
gelman.diag(l$model.likelihood.rate)

## End(Not run)

## create a single "mcmc.list" object
mean.mcmc <- fetchMCMC(filename,
                  where = c("model", "likelihood", "rate"))

## only write part of each name
mean.mcmc <- fetchMCMC(filename, where = c("mod", "like", "r"))

## sample 6 randomly-chosen values
mean.mcmc.5 <- fetchMCMC(filename,
                    where = c("model", "likelihood", "rate"),
                    nSample = 6)

## sample the first 5 values
mean.mcmc.5 <- fetchMCMC(filename,
                    where = c("model", "likelihood", "rate"),
                    sample = 1:5)


StatisticsNZ/demest documentation built on Nov. 2, 2023, 7:56 p.m.