relative_eff {loo} | R Documentation |
Convenience function for computing relative efficiencies
Description
relative_eff()
computes the the MCMC effective sample size divided by
the total sample size.
Usage
relative_eff(x, ...)
## Default S3 method:
relative_eff(x, chain_id, ...)
## S3 method for class 'matrix'
relative_eff(x, chain_id, ..., cores = getOption("mc.cores", 1))
## S3 method for class 'array'
relative_eff(x, ..., cores = getOption("mc.cores", 1))
## S3 method for class ''function''
relative_eff(
x,
chain_id,
...,
cores = getOption("mc.cores", 1),
data = NULL,
draws = NULL
)
## S3 method for class 'importance_sampling'
relative_eff(x, ...)
Arguments
x |
A vector, matrix, 3-D array, or function. See the Methods (by
class) section below for details on specifying |
chain_id |
A vector of length |
cores |
The number of cores to use for parallelization. |
data , draws , ... |
Same as for the |
Value
A vector of relative effective sample sizes.
Methods (by class)
-
relative_eff(default)
: A vector of lengthS
(posterior sample size). -
relative_eff(matrix)
: AnS
byN
matrix, whereS
is the size of the posterior sample (with all chains merged) andN
is the number of data points. -
relative_eff(array)
: AnI
byC
byN
array, whereI
is the number of MCMC iterations per chain,C
is the number of chains, andN
is the number of data points. -
relative_eff(`function`)
: A functionf()
that takes argumentsdata_i
anddraws
and returns a vector containing the log-likelihood for a single observationi
evaluated at each posterior draw. The function should be written such that, for each observationi
in1:N
, evaluatingf(data_i = data[i,, drop=FALSE], draws = draws)
results in a vector of length
S
(size of posterior sample). The log-likelihood function can also have additional arguments butdata_i
anddraws
are required.If using the function method then the arguments
data
anddraws
must also be specified in the call toloo()
:-
data
: A data frame or matrix containing the data (e.g. observed outcome and predictors) needed to compute the pointwise log-likelihood. For each observationi
, thei
th row ofdata
will be passed to thedata_i
argument of the log-likelihood function. -
draws
: An object containing the posterior draws for any parameters needed to compute the pointwise log-likelihood. Unlikedata
, which is indexed by observation, for each observation the entire objectdraws
will be passed to thedraws
argument of the log-likelihood function. The
...
can be used if your log-likelihood function takes additional arguments. These arguments are used like thedraws
argument in that they are recycled for each observation.
-
-
relative_eff(importance_sampling)
: Ifx
is an object of class"psis"
,relative_eff()
simply returns ther_eff
attribute ofx
.
Examples
LLarr <- example_loglik_array()
LLmat <- example_loglik_matrix()
dim(LLarr)
dim(LLmat)
rel_n_eff_1 <- relative_eff(exp(LLarr))
rel_n_eff_2 <- relative_eff(exp(LLmat), chain_id = rep(1:2, each = 500))
all.equal(rel_n_eff_1, rel_n_eff_2)