% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/fit.R
\name{extract_quantity.stanfit}
\alias{extract_quantity.stanfit}
\title{Extract estimated quantities from fit objects.}
\usage{
\method{extract_quantity}{stanfit}(fit, pars, reduce = NULL,
  combine = NULL, chain = "all", ...)
}
\arguments{
\item{fit}{An object returned by \code{\link{fit}}, \code{\link{draw_samples}}, or \code{\link{optimizing}}.}

\item{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: \emph{?} matches a single character, while \emph{*} matches any character string including an empty one. For example, \emph{?pred} will match both ypred and zpred, and \emph{z*} will match zstar and zpred.}

\item{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 \emph{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 \code{\link{optimizing}} since there is only one scalar per quantity.}

\item{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 (\code{\link{do.call}}). Useful when all the elements of the list have the same dimension, possibly because it is used in conjunction with the \emph{chain} and \emph{reduce} arguments.}

\item{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.}

\item{...}{Arguments to be passed to rstan's \code{\link[rstan]{extract}} if the object \emph{fit} was returned by \code{\link{fit}} or \code{\link{draw_samples}}.}
}
\value{
A list of named elements with type and dimension depending on the characteristics of the quantity. If the argument \emph{combine} is set, the returned value equals to the return value of the function given (e.g. a matrix if \code{\link{cbind}}) is used).
}
\description{
This function extracts estimated quantities from fit objects. It works transparently with both Markov-chain Monte Carlo samples returned by \code{\link{draw_samples}} and maximum a posteriori estimates returned by \code{\link{optimizing}}. Most of the user-friendly extractors (e.g. \code{\link{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.
}
\details{
Note that \emph{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. \emph{extract_quantity} solves these two inconvenients. Additionally, it allows for regex expressions in the name of the quantities to be extracted.
}
\examples{
\dontrun{
  extract_quantity(myFit, c("z*"), reduce = mean, chain = 1, combine = cbind))
}
}
\keyword{internal}
