
Leave-one-out predictions, R-squared and criteria for mvgam models
Source:R/loo_extras.mvgam.R
mvgam_loo_extras.RdThe brms methods that take their weights from loo.mvgam().
loo_predict(),
loo_epred() and loo_linpred() reweight posterior predictions by
the PSIS weights of each observation, approximating the prediction
made without it. loo_predictive_interval() gives the
leave-one-out predictive interval. loo_R2() is the Bayesian R^2 of
Gelman et al. (2019) computed from leave-one-out predictions.
add_criterion() stores criteria on the fit. LOO() and WAIC()
are the brms aliases of loo.mvgam() and waic.mvgam().
Usage
# S3 method for class 'mvgam'
LOO(
x,
...,
compare = TRUE,
resp = NULL,
pointwise = FALSE,
moment_match = FALSE,
reloo = FALSE,
k_threshold = 0.7,
save_psis = FALSE,
moment_match_args = list(),
reloo_args = list(),
model_names = NULL
)
# S3 method for class 'mvgam'
WAIC(
x,
...,
compare = TRUE,
resp = NULL,
pointwise = FALSE,
model_names = NULL
)
# S3 method for class 'mvgam'
loo_R2(
object,
resp = NULL,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
seed = NULL,
args_epred = list(),
args_loglik = list(),
...
)
# S3 method for class 'mvgam'
loo_predict(
object,
type = c("mean", "var", "quantile"),
probs = 0.5,
psis_object = NULL,
resp = NULL,
...
)
# S3 method for class 'mvgam'
loo_epred(
object,
type = c("mean", "var", "quantile"),
probs = 0.5,
psis_object = NULL,
resp = NULL,
...
)
# S3 method for class 'mvgam'
loo_linpred(
object,
type = c("mean", "var", "quantile"),
probs = 0.5,
psis_object = NULL,
resp = NULL,
...
)
# S3 method for class 'mvgam'
loo_predictive_interval(object, prob = 0.9, psis_object = NULL, ...)
# S3 method for class 'mvgam'
loo_subsample(x, ..., compare = TRUE, resp = NULL, model_names = NULL)
# S3 method for class 'mvgam'
loo_moment_match(
x,
loo = NULL,
k_threshold = 0.7,
newdata = NULL,
resp = NULL,
check = TRUE,
recompile = FALSE,
...
)
# S3 method for class 'mvgam'
loo_model_weights(x, ..., model_names = NULL)
# S3 method for class 'mvgam'
add_criterion(
x,
criterion,
model_name = NULL,
overwrite = FALSE,
file = NULL,
force_save = FALSE,
...
)Arguments
- x, object
A fitted
mvgamobject.- ...
Further arguments to the method each function calls.
loo_R2()refuses them.- compare, pointwise, moment_match, reloo, k_threshold, save_psis, moment_match_args, reloo_args
As in
loo.mvgam(), which describes the two it uses and refuses the rest.- resp
Optional name of one response of a multivariate model.
- model_names
Optional character vector of model names, one per forecast. It replaces the names taken from the call.
- summary
Logical.
TRUE(the default) returns the summary matrix withEstimate,Est.Errorand interval columns.FALSEreturns the draws.- robust
Logical. If
FALSE(the default), uses mean and standard deviation for summaries. IfTRUE, uses median and median absolute deviation (MAD).- probs
Numeric vector of probabilities for quantile computation. Default is
c(0.025, 0.975)for 95% credible intervals.- seed
Optional integer seed. The session's random state is restored when the call returns.
- args_epred, args_loglik
Lists of further arguments to
posterior_epred.mvgam()andlog_lik.mvgam()forloo_R2().- type
The expectation:
"mean"(the default),"var"or"quantile".- psis_object
Optional
psisobject fromloo(x, save_psis = TRUE), reused across calls.- prob
Interval mass of
loo_predictive_interval(), 0.9 by default as in brms.- loo, newdata, check, recompile
Accepted for the brms signature of
loo_moment_match(), which mvgam refuses.- criterion
Character vector of criteria for
add_criterion():"loo","waic","bayes_R2"or"loo_R2".- model_name
Optional name stored in
x$model_name.- overwrite
Logical.
TRUErecomputes criteriax$criteriaalready holds.- file
Optional path without extension. The fit is saved to
paste0(file, ".rds")with its criteria.- force_save
Unused. Accepted for the brms signature.
Value
loo_predict(), loo_epred() and loo_linpred() return a
matrix with one row per scored observation and a column per
expectation. loo_predictive_interval() returns its two quantile
columns. loo_R2() returns a one-row summary, or its draws with
summary = FALSE. add_criterion() returns x with the criteria
in x$criteria. loo_model_weights() returns the weights of
loo::loo_model_weights().
The observations scored
The weights and the predictions describe the observation the
likelihood scored. For most families that is a row of the data. The
detection families (occ(), nmix()) write their likelihood per
closure unit, marginalising its visits. loo_predict(),
loo_epred(), loo_predictive_interval() and loo_R2() then sum a
unit's visits and return one value per unit, in the column order of
log_lik.mvgam(). loo_linpred() is refused for them. A sum of
linear predictors over visits has no interpretation.
mvn() and mvt() keep one density per row. The composition
families (diri(), multi(), categ()) hold one joint density per
site. These methods refuse them.
References
Gelman, A., Goodrich, B., Gabry, J. and Vehtari, A. (2019). R-squared for Bayesian regression models. The American Statistician, 73(3):307-309. doi:10.1080/00031305.2018.1549100
See also
loo.mvgam() estimates the ELPD these weights come from.
bayes_R2.mvgam() is the in-sample R^2. lfo_cv.mvgam() scores
forecasts of data the model has not seen.
Other model comparison:
bridge_sampler.mvgam(),
compare_elpds(),
kfold.mvgam(),
lfo_cv.mvgam(),
log_lik.mvgam(),
loo.mvgam(),
loo_compare.mvgam_lfo(),
posterior_average.mvgam(),
waic.mvgam()
Examples
# \dontrun{
# A Poisson GAM without a latent trend. PSIS approximates each
# held-out count of such a model well (see Details of ?loo.mvgam)
set.seed(13)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 120L, trend_model = "None")
mod <- mvgam(y ~ s(x), data = simdat$data_train,
family = poisson(), chains = 2, silent = 2)
#> Warning: 11 of 2000 (1.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
# }
# \dontrun{
# The leave-one-out R^2 is lower than the in-sample one
loo_R2(mod)
#> Estimate Est.Error Q2.5 Q97.5
#> R2 0.5858806 0.06563688 0.4374937 0.692272
bayes_R2(mod)
#> Estimate Est.Error Q2.5 Q97.5
#> R2 0.630727 0.04941012 0.5139853 0.7126687
# One PSIS object serves every leave-one-out prediction
psis <- loo(mod, save_psis = TRUE)$psis_object
head(loo_predict(mod, psis_object = psis))
#> mean
#> [1,] 2.727864
#> [2,] 12.898318
#> [3,] 7.103390
#> [4,] 8.620246
#> [5,] 2.916558
#> [6,] 9.095455
head(loo_predictive_interval(mod, psis_object = psis))
#> q5 q95
#> [1,] 0 6
#> [2,] 7 20
#> [3,] 3 12
#> [4,] 4 14
#> [5,] 0 6
#> [6,] 4 15
# Next, loo_compare() ranks models by their ELPD
# }