Skip to contents

brms's add_criterion(): compute each criterion once and store it in x$criteria, so that a later loo(), waic() or loo_compare() without further arguments reads it instead of computing it again, as brms's do. "loo" and "waic" are computed by loo() and waic(), "bayes_R2" by frmtmb::bayes_R2() and "loo_R2" by loo_R2(), both with summary = FALSE, as brms stores them. "kfold", "loo_subsample" and "marglik" are refused with the reason their own functions give. On a maximum-likelihood fit, frmtmb::add_criterion() refuses every criterion and names AIC() and BIC().

Usage

# S3 method for class 'frmtmb_draws'
add_criterion(
  x,
  criterion,
  model_name = NULL,
  overwrite = FALSE,
  file = NULL,
  force_save = FALSE,
  ...
)

Arguments

x

A frmtmb_draws from frm_sample().

criterion

A subset of "loo", "waic", "kfold", "loo_subsample", "bayes_R2", "loo_R2" and "marglik".

model_name

The name stored with a loo-type criterion; by default the deparsed x, as in brms.

overwrite

If TRUE, recompute a criterion that is already stored; otherwise it is kept, as in brms.

file

A file name without its .rds extension. When given, the object is saved there with saveRDS() after a criterion is added, as in brms.

force_save

If TRUE, save to file even when nothing was added.

...

Passed to the criterion's function.

Value

x, with the criteria in x$criteria.

Examples

# \donttest{
if (requireNamespace("tmbstan", quietly = TRUE) &&
    requireNamespace("rstan", quietly = TRUE) &&
    requireNamespace("loo", quietly = TRUE) &&
    !frmtmb.sample:::tmbstan_build_broken()) {
  set.seed(9)
  dd <- data.frame(x = rnorm(60))
  dd$y <- rnorm(60, 1 + 0.5 * dd$x, 1)
  ds <- frm_sample(bf(y ~ x), family = gaussian(), data = dd,
                   chains = 1, iter = 500, refresh = 0)
  ds <- add_criterion(ds, "loo")
  identical(loo(ds), ds$criteria$loo)
}
#> frm_sample(): default priors (brms 2.23 defaults; prior = "flat" puts brms's (flat) in every slot)
#>   Intercept          student_t(3, 0.8, 2.5)
#>   sigma              student_t(3, 0, 2.5)  [natural scale]
#>   b                  (flat), as brms leaves slopes
#> [1] TRUE
# }