
Posterior predictive errors on a fitted mvgam model
Source:R/brms_wrappers.mvgam.R
predictive_error.mvgam.RdPosterior draws of the prediction error, observed minus predicted,
of each observation on the response scale. The arguments are those
of brms::predictive_error.brmsfit().
Usage
# S3 method for class 'mvgam'
predictive_error(
object,
newdata = NULL,
re_formula = NULL,
re.form = NULL,
method = "posterior_predict",
resp = NULL,
ndraws = NULL,
draw_ids = NULL,
sort = FALSE,
...
)Arguments
- object
A fitted mvgam object from
mvgam().- newdata
Optional data frame with covariates for prediction. If NULL, uses original training data stored in the model object.
- re_formula
Group-level terms to include:
NULL(the default) for every one,NAfor none, or a formula naming the terms to keep, such as~ (1 | g). A formula applies to the observation and the trend formulas alike. Any term it does not name is dropped.- re.form
The lme4 name for
re_formula.- method
"posterior_predict"(the default) takes the errors from predictive draws,"posterior_epred"from expected values.- resp
Character specifying which response variable for multivariate models. NULL (default) returns predictions for all responses.
- ndraws
Positive integer specifying number of posterior draws to use. NULL (default) uses all available draws.
- draw_ids
Optional integer vector selecting a subset of posterior draw indices to use. NULL (default) uses all draws (subject to
ndraws).- sort
Logical.
TRUEorders the columns by the observed response. Defaults toFALSE.- ...
Further arguments of the
methodfunction, such asincl_autocororallow_new_levels.
Details
The errors are those residuals.mvgam() returns with
type = "ordinary". In sample, each prediction carries the latent
trend state the model inferred at that time. A prediction for
newdata uses the covariate part of the model, without the trend's
innovations.
See also
brms::predictive_error.brmsfit(), residuals.mvgam(),
posterior_predict.mvgam(), posterior_epred.mvgam().
Other predictions:
conditional_effects.mvgam(),
conditional_smooths.mvgam(),
fitted.mvgam(),
hindcast(),
posterior_epred.mvgam(),
posterior_linpred.mvgam(),
posterior_predict.mvgam(),
posterior_smooths.mvgam(),
predict.mvgam(),
predictive_interval.mvgam()
Examples
# \dontrun{
set.seed(13)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
n_timepoints = 120L, trend_model = AR())
mod <- mvgam(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1),
data = simdat$data_train, family = poisson(),
chains = 2, silent = 2)
#> Warning: 8 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.
# Observed minus predicted counts, one row per draw
err <- predictive_error(mod, draw_ids = 1:200)
dim(err)
#> [1] 200 90
# The three observations with the largest mean absolute error
mae <- colMeans(abs(err))
simdat$data_train[order(mae, decreasing = TRUE)[1:3], c("time", "y")]
#> time y
#> 84 84 23
#> 89 89 28
#> 71 71 30
# Next: residuals(mod) returns randomised quantile residuals
# }