loo_moment_match.brmsfit: Moment matching for efficient approximate leave-one-out...

View source: R/loo_moment_match.R

loo_moment_match.brmsfitR Documentation

Moment matching for efficient approximate leave-one-out cross-validation

Description

Moment matching for efficient approximate leave-one-out cross-validation (LOO-CV). See loo_moment_match for more details.

Usage

## S3 method for class 'brmsfit'
loo_moment_match(
  x,
  loo = NULL,
  k_threshold = 0.7,
  newdata = NULL,
  resp = NULL,
  check = TRUE,
  recompile = FALSE,
  ...
)

## S3 method for class 'loo'
loo_moment_match(x, fit, ...)

Arguments

x

An R object of class brmsfit or loo depending on the method.

loo

An R object of class loo. If NULL, brms will try to extract a precomputed loo object from the fitted model, added there via add_criterion.

k_threshold

The Pareto k threshold for which observations moment matching is applied. Defaults to 0.7. See pareto_k_ids for more details.

newdata

An optional data.frame for which to evaluate predictions. If NULL (default), the original data of the model is used. NA values within factors (excluding grouping variables) are interpreted as if all dummy variables of this factor are zero. This allows, for instance, to make predictions of the grand mean when using sum coding. NA values within grouping variables are treated as a new level.

resp

Optional names of response variables. If specified, predictions are performed only for the specified response variables.

check

Logical; If TRUE (the default), some checks check are performed if the loo object was generated from the brmsfit object passed to argument fit.

recompile

Logical, indicating whether the Stan model should be recompiled. This may be necessary if you are running moment matching on another machine than the one used to fit the model. No recompilation is done by default.

...

Further arguments passed to the underlying methods. Additional arguments initially passed to loo, for example, newdata or resp need to be passed again to loo_moment_match in order for the latter to work correctly.

fit

An R object of class brmsfit.

Details

The moment matching algorithm requires draws of all variables defined in Stan's parameters block to be saved. Otherwise loo_moment_match cannot be computed. Thus, please set save_pars = save_pars(all = TRUE) in the call to brm, if you are planning to apply loo_moment_match to your models.

Value

An updated object of class loo.

References

Paananen, T., Piironen, J., Buerkner, P.-C., Vehtari, A. (2021). Implicitly Adaptive Importance Sampling. Statistics and Computing.

Examples

## Not run: 
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
            data = epilepsy, family = poisson(),
            save_pars = save_pars(all = TRUE))

# throws warning about some pareto k estimates being too high
(loo1 <- loo(fit1))

# no more warnings after moment matching
(mmloo1 <- loo_moment_match(fit1, loo = loo1))

## End(Not run)


brms documentation built on Sept. 23, 2024, 5:08 p.m.