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", ...)
fit | An object returned by |
---|---|
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 |
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 ( |
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 |
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).
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.
# NOT RUN { extract_quantity(myFit, c("z*"), reduce = mean, chain = 1, combine = cbind)) # }