
Compare leave-future-out evaluations across models
Source:R/lfo_cv.mvgam.R
loo_compare.mvgam_lfo.RdRanks two or more lfo_cv() results by their summed ELPD, in the
layout of loo::loo_compare(). The standard error of each
difference comes from the paired per-fold differences with the best
model. The results must share one evaluation grid, with the same
min_t, fc_horizon and data.
Usage
# S3 method for class 'mvgam_lfo'
loo_compare(x, ..., model_names = NULL)Value
A compare.loo table in the layout of loo::loo_compare(),
one row per model, best first.
modelThe model's name.
elpd_diffSummed ELPD difference from the best model, zero for the best.
se_diffStandard error of
elpd_diff,sqrt(N) * sd(fold_diff)over theNfolds.p_worseThe normal probability that the model forecasts worse than the best one (Sivula et al. 2025).
NAfor the best model and for a difference without spread.diag_diff"N < 100"for fewer than 100 folds, else"|elpd_diff| < 4"for a difference too small for its SE.diag_elpdThe number of folds whose PSIS estimate was kept with a Pareto k above the threshold.
elpd_lfoSummed per-fold ELPD.
se_elpd_lfoStandard error of
elpd_lfo.
References
Sivula, T., Magnusson, M., Matamoros, A. A. and Vehtari, A. (2025). Uncertainty in Bayesian leave-one-out cross-validation based model comparison. Bayesian Analysis. doi:10.1214/25-BA1569
Examples
# \dontrun{
set.seed(1)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 120L, trend_model = AR())
# Two models of one count series: a smooth of x with and without
# an AR(1) trend. The trend model samples with smaller steps
# (adapt_delta = 0.95). That avoids divergent transitions when
# lfo_cv() refits it to shorter stretches of the series
mod_gam <- mvgam(y ~ s(x, k = 5), data = simdat$data_train,
family = poisson(), chains = 2, silent = 2)
#> Warning: 12 of 2000 (1.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
mod_ar <- mvgam(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1),
data = simdat$data_train, family = poisson(),
chains = 2, silent = 2,
control = list(adapt_delta = 0.95))
# }
# \dontrun{
# Each origin after t = 70 forecasts the next time step. The
# pointwise log densities feed stacking weights
lfo_gam <- lfo_cv(mod_gam, min_t = 70L, save_log_lik = TRUE)
#> Warning: 21 of 2000 (1.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
#> Warning: 6 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
#> Warning: 7 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
lfo_ar <- lfo_cv(mod_ar, min_t = 70L, save_log_lik = TRUE)
#> Warning: 1 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
#> Warning: 2 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
#> Warning: 6 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
# The model with the higher summed ELPD comes first
loo_compare(lfo_gam, lfo_ar, model_names = c("GAM", "AR"))
#> model elpd_diff se_diff p_worse diag_diff diag_elpd
#> AR 0.0 0.0 NA
#> GAM -36.7 12.5 1.00 N < 100
#>
#> Diagnostic flags present.
#> See ?`loo-glossary` (sections `diag_diff` and `diag_elpd`)
#> or https://mc-stan.org/loo/reference/loo-glossary.html.
# Next, compare_elpds() shows where along the series the models
# part ways
# }