ijse: Infinitesimal jackknife inference for brms models

View source: R/brms.R

ijseR Documentation

Infinitesimal jackknife inference for brms models

Description

Extract matched draws and likelihood contributions from a brmsfit object and compute IJ inference with ijse_draws.

Usage

ijse(
  fit,
  cluster_var = NULL,
  variables = NULL,
  transform = NULL,
  draw_ids = NULL,
  block_size = NULL,
  save_influence = FALSE,
  diagnostics = c("all", "posterior", "none")
)

IJ_se(fit, cluster_var = NULL)

Arguments

fit

A fitted brmsfit object containing posterior draws.

cluster_var

Optional cluster IDs in the order of the likelihood columns returned by brms::log_lik(fit). IDs must refer to the observations actually used in fitting, after any omissions or subsetting. IDs are not inferred from the formula. Save them separately: brms can drop unused data columns, so an absent fit$data$group is not a safe source of IDs.

variables

Optional exact parameter names. Defaults to parameters starting with b_, preserving IJ_se() behavior.

transform

Optional function applied to the selected draws; see ijse_draws. Select every required input with variables.

draw_ids

Optional strictly increasing indices of post-warmup draws, ordered by iteration within chain, then chain. The same indices are passed to likelihood extraction. Each retained chain requires at least two draws when diagnostics = "all".

block_size

NULL (default) for ordinary likelihood extraction, or a positive integer for pointwise streaming. Streaming currently supports univariate models and requires brms pointwise-likelihood support. Unsupported models fail explicitly; use NULL for ordinary extraction.

save_influence

Whether to retain empirical influence values.

diagnostics

One of "all", "posterior", or "none"; see ijse_draws. The legacy IJ_se() wrapper uses "none" to avoid computing discarded diagnostics.

Details

MCMC chain diagnostics are retained for the selected or transformed quantities. The likelihood uses the brms default of conditioning on fitted group effects. This extraction choice does not establish IJ validity for arbitrary hierarchical models. For clustered working-independence models, supply cluster IDs explicitly; they are never inferred from a model formula.

Streaming respects observation permutations in brms prediction metadata. The tested brms release does not support pointwise evaluation for regression ARMA models; use ordinary extraction for those models. Covariance ARMA models are rejected because brms returns conditional LOO densities instead of joint likelihood factors. Supply one joint likelihood per independent series to ijse_draws(). Discrete truncated models also require explicit matrix input because the backend lower-bound normalization is not validated. Multivariate streaming is rejected because response-specific orderings may differ; the default dense path uses brms's combined likelihood.

Measurement-error predictors require saved latent draws, for example brms::save_pars(latent = TRUE) when fitting. Otherwise brms may resample prediction latents, which do not match the fitted posterior draws. Even with saved latents, the native likelihood conditions on them; it is not an observed-data likelihood integrating measurement error. Missing-response models may return non-finite likelihoods, which are rejected. Use explicitly derived observed-data likelihoods with ijse_draws() when appropriate.

Value

ijse() returns the structured ijse object documented under ijse_draws. IJ_se() returns the named numeric vector of standard errors, as in previous releases.

Choosing an interface

Use ijse() to retain estimates, both covariance types, intervals, diagnostics and optional influence. IJ_se() returns only IJ SEs with the same pooled formula. Ordinary extraction is the default. A positive block_size requests brms pointwise likelihood evaluation; backend prediction/preparation objects may still be large. This differs from ijse_draws(), whose positive block size controls a supplied callback.

Parameter extraction and likelihood extraction share draw_ids. Do not manually reorder parameter rows or pass cluster IDs for the original unfiltered dataset. Keep fitting-row keys with the actual model data and check clustered, n_obs, n_units, and unit_ids. Correct lengths alone cannot identify mismatched scientific units.

See Also

ijse_draws, summary.ijse

Examples

# Uses an installed synthetic fit: no Stan compilation or new fitting.
if (requireNamespace("brms", quietly = TRUE)) {
  examples <- readRDS(system.file("extdata", "tutorial",
    "median-examples.rds", package = "IJSE"))
  fit <- examples$clustered$fit
  ids <- examples$clustered$cluster
  result <- ijse(fit, cluster_var = ids, save_influence = TRUE)
  stopifnot(result$clustered, result$n_units == 30L)
  summary(result)
  vcov(result)
  vcov(result, type = "posterior")
  confint(result, parm = "b_x", level = 0.90)
  head(result$unit_ids)
  stopifnot(isTRUE(all.equal(IJ_se(fit, ids), result$se)))
  streamed <- ijse(fit, cluster_var = ids, block_size = 40,
                   diagnostics = "none")
  stopifnot(isTRUE(all.equal(vcov(streamed), vcov(result))))
  prediction <- ijse(fit, cluster_var = ids,
    variables = c("b_Intercept", "b_x"),
    transform = function(d) {
      cbind(median_at_x1 = d[, "b_Intercept"] + d[, "b_x"])
    }, diagnostics = "posterior")
  coef(prediction)
}
# For fitting instructions, run vignette("ijse-brms", package = "IJSE").

IJSE documentation built on Oct. 22, 2026, 5:18 p.m.