Skip to contents

The log density of each observation under each posterior draw, as brms::log_lik.brmsfit() returns it. loo.mvgam(), waic.mvgam(), lfo_cv.mvgam() and kfold.mvgam() score models from it.

Usage

# S3 method for class 'mvgam'
log_lik(
  object,
  newdata = NULL,
  re_formula = NULL,
  resp = NULL,
  ndraws = NULL,
  draw_ids = NULL,
  incl_autocor = TRUE,
  process_error = NULL,
  allow_new_levels = FALSE,
  sample_new_levels = "uncertainty",
  ...
)

Arguments

object

A fitted mvgam object.

newdata

Optional data frame holding the response and every covariate of the model. NULL (the default) takes the training data.

re_formula

Group-level terms to include: NULL (the default) for every one, NA for 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.

resp

Optional name of one response of a multivariate model.

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).

incl_autocor

Logical. TRUE (the default) evaluates each density under the latent trend state the model inferred at that time, the state hindcast.mvgam() returns. The ELPD then describes the series that was observed. FALSE keeps only the trend's deterministic terms and leaves the autocorrelation out.

process_error

Deprecated alias for incl_autocor.

allow_new_levels

Logical. TRUE scores rows whose grouping level the training data lacked, as kfold.mvgam() needs when a fold holds out whole groups. Defaults to FALSE.

sample_new_levels

How a new level's coefficients are drawn: "uncertainty" (the default), "gaussian" or "old_levels".

...

Unused. Anything passed here is refused.

Value

A numeric [ndraws, nobs] matrix of log densities. A missing response gives a column of NA. For a multivariate fit with resp = NULL, each column sums the densities of the responses measured in that row, as brms does. An occupancy, N-mixture or composition family has one density per site, carried on the site's first row.

Details

Each family uses the brms parameterisation. For example, negbinomial() takes a mean and shape, Beta() a mean and precision. Hurdle and zero-inflated families mix a mass at zero with their base family. The weights(), cens() and trunc() addition terms change each row's contribution as they do in the Stan model.

The posterior_epred.mvgam() family of methods uses the covariate part of the model by default. A covariate effect then has the same meaning at every time. Those predictions describe no particular observed series and do not give an ELPD.

See also

loo.mvgam() and waic.mvgam() estimate the ELPD from this matrix. hindcast.mvgam() returns the latent states it conditions on. The forecast evaluation article compares models by cross-validation.

Other model comparison: bridge_sampler.mvgam(), compare_elpds(), kfold.mvgam(), lfo_cv.mvgam(), loo.mvgam(), loo_compare.mvgam_lfo(), mvgam_loo_extras, posterior_average.mvgam(), waic.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.

# One row per draw, one column per observation
ll <- log_lik(mod)
dim(ll)
#> [1] 2000   90

# Without the latent trend, the densities are far lower. The trend
# explains most of the series
ll_fixed <- log_lik(mod, incl_autocor = FALSE)
c(with_trend = mean(rowSums(ll)), without = mean(rowSums(ll_fixed)))
#> with_trend    without 
#>  -197.6027  -301.2529 

# Next, loo() estimates the ELPD from these densities
# }