R/loo_approximate_posterior.R
loo_approximate_posterior.Rd
Efficient approximate leaveoneout crossvalidation (LOO) for posterior approximations
loo_approximate_posterior(x, log_p, log_g, ...) # S3 method for array loo_approximate_posterior( x, log_p, log_g, ..., save_psis = FALSE, cores = getOption("mc.cores", 1) ) # S3 method for matrix loo_approximate_posterior( x, log_p, log_g, ..., save_psis = FALSE, cores = getOption("mc.cores", 1) ) # S3 method for `function` loo_approximate_posterior( x, ..., data = NULL, draws = NULL, log_p = NULL, log_g = NULL, save_psis = FALSE, cores = getOption("mc.cores", 1) )
x  A loglikelihood array, matrix, or function. The Methods (by class) section, below, has detailed descriptions of how to specify the inputs for each method. 

log_p  The logposterior (target) evaluated at S samples from the proposal distribution (g). A vector of length S. 
log_g  The logdensity (proposal) evaluated at S samples from the proposal distribution (g). A vector of length S. 
save_psis  Should the 
cores  The number of cores to use for parallelization. This defaults to
the option

data, draws, ...  For the 
The loo_approximate_posterior()
methods return a named list with
class c("psis_loo_ap", "psis_loo", "loo")
. It has the same structure
as the objects returned by loo()
but with the additional slot:
posterior_approximation
A list with two vectors, log_p
and log_g
of the same length
containing the posterior density and the approximation density
for the individual draws.
The loo_approximate_posterior()
function is an S3 generic and
methods are provided for 3D pointwise loglikelihood arrays, pointwise
loglikelihood matrices, and loglikelihood functions. The implementation
works for posterior approximations where it is possible to compute the log
density for the posterior approximation.
array
: An \(I\) by \(C\) by \(N\) array, where \(I\)
is the number of MCMC iterations per chain, \(C\) is the number of
chains, and \(N\) is the number of data points.
matrix
: An \(S\) by \(N\) matrix, where \(S\) is the size
of the posterior sample (with all chains merged) and \(N\) is the number
of data points.
function
: A function f()
that takes arguments data_i
and draws
and returns a
vector containing the loglikelihood for a single observation i
evaluated
at each posterior draw. The function should be written such that, for each
observation i
in 1:N
, evaluating
f(data_i = data[i,, drop=FALSE], draws = draws)
results in a vector of length S
(size of posterior sample). The
loglikelihood function can also have additional arguments but data_i
and
draws
are required.
If using the function method then the arguments data
and draws
must also
be specified in the call to loo()
:
data
: A data frame or matrix containing the data (e.g.
observed outcome and predictors) needed to compute the pointwise
loglikelihood. For each observation i
, the i
th row of
data
will be passed to the data_i
argument of the
loglikelihood function.
draws
: An object containing the posterior draws for any
parameters needed to compute the pointwise loglikelihood. Unlike
data
, which is indexed by observation, for each observation the
entire object draws
will be passed to the draws
argument of
the loglikelihood function.
The ...
can be used if your loglikelihood function takes additional
arguments. These arguments are used like the draws
argument in that they
are recycled for each observation.
Magnusson, M., Riis Andersen, M., Jonasson, J. and Vehtari, A. (2019). LeaveOneOut CrossValidation for Large Data. In International Conference on Machine Learning
Magnusson, M., Riis Andersen, M., Jonasson, J. and Vehtari, A. (2019). LeaveOneOut CrossValidation for Model Comparison in Large Data.