This function extracts estimated quantities from fit objects. It works transparently with both Markov-chain Monte Carlo samples returned by draw_samples and maximum a posteriori estimates returned by optimizing. Most of the user-friendly extractors (e.g. extract_alpha) are built upon this function. As such, it was not designed to be used directly by users, except perhaps for the most advanced ones.

extract_quantity(fit, pars, reduce = NULL, combine = NULL,
  chain = "all", ...)

Arguments

fit

An object returned by fit, draw_samples, or optimizing.

pars

A vector of characters with the name of the quantities to be extracted. The characters strings may include regular expressions. Further, wildcards are automatically translated into regex: ? matches a single character, while * matches any character string including an empty one. For example, ?pred will match both ypred and zpred, and z* will match zstar and zpred.

reduce

A function applied to the samples generated by one chain for each parameters (i.e. "within chain"). Useful if only one or more summary measures of the generated samples is needed (e.g. the median of the generated sample). The function may return one of more elements. In the former case, the dimension is dropped in the returned object (read the Value section below). Note that the user needs to supply a function as an argument, and not a character string with the name of the function. This argument is not used for maximum a posteriori estimates returned by optimizing since there is only one scalar per quantity.

combine

A function applied to all the extracted quantities as an ensemble. In other words, instead of returning a named list where each element is one quantity, it returns the value returned by the function applied to the whole list (do.call). Useful when all the elements of the list have the same dimension, possibly because it is used in conjunction with the chain and reduce arguments.

chain

Either "all" or any integer number between 1 and the number of chains M. In the latter case, the chain dimension in the returned object is dropped.

...

Arguments to be passed to rstan's extract if the object fit was returned by fit or draw_samples.

Value

A list of named elements with type and dimension depending on the characteristics of the quantity. If the argument combine is set, the returned value equals to the return value of the function given (e.g. a matrix if cbind) is used).

Details

Note that rstan does not provide a unified way to extract quantities from full bayesian and optimization fits, and the structure of the returned objects are not homogeneous. extract_quantity solves these two inconvenients. Additionally, it allows for regex expressions in the name of the quantities to be extracted.

Examples

# NOT RUN {
  extract_quantity(myFit, c("z*"), reduce = mean, chain = 1, combine = cbind))
# }