ijse_draws: Infinitesimal jackknife inference from posterior draws

View source: R/core.R

ijse_drawsR Documentation

Infinitesimal jackknife inference from posterior draws

Description

Compute sampling covariance of posterior expectations from matched posterior draws and pointwise log likelihoods, with optional cluster aggregation.

Usage

ijse_draws(
  draws,
  log_lik,
  cluster_var = NULL,
  variables = NULL,
  transform = NULL,
  n_obs = NULL,
  block_size = 1000L,
  save_influence = FALSE,
  chain_id = NULL,
  diagnostics = c("all", "posterior", "none")
)

Arguments

draws

A numeric matrix or data frame (draws by parameters), or a posterior draws object. Rows must correspond exactly to likelihood rows.

log_lik

A finite numeric matrix (draws by observations), or a function accepting observation indices and returning such a matrix for those columns. A callback must preserve the requested column order and draw row order.

cluster_var

Optional atomic vector of cluster IDs, in likelihood-column order. Missing IDs, including used explicit NA factor levels, are rejected. At least two clusters are required.

variables

Optional character vector of exact parameter names to select before applying transform. By default all parameters are selected.

transform

Optional function of the selected plain numeric draw matrix. Return a numeric vector, matrix, or data frame with one row per input draw. Named columns are recommended. Each returned column defines a posterior expectation of interest. Names on a returned vector are draw-row labels, not a quantity name; they are preserved and checked against available input labels. Use a one-column matrix or data frame to name a single transformed quantity.

n_obs

Number of likelihood columns. Required for a likelihood callback.

block_size

Maximum number of likelihood columns requested per callback block. Already materialized matrices use the compiled dense covariance path.

save_influence

Whether to retain the empirical influence matrix (parameters by resampling units). Default FALSE.

chain_id

Optional atomic vector identifying the chain of each draw. Within each chain, rows must be in iteration order. Inferred for posterior draws objects unless supplied explicitly. Each chain must have two draws when computing chain-specific IJ SEs. Missing chain IDs, including used explicit NA factor levels, are rejected.

diagnostics

One of "all" (chain-specific IJ SEs and posterior diagnostics), "posterior" (R-hat and ESS only), or "none". None of these options changes the pooled IJ estimate.

Details

Inputs have S posterior-draw rows, P selected or transformed quantities, and N observation-level likelihood columns. Supply log likelihoods excluding the prior, rather than a log posterior or a single joint-likelihood column. At least two draws and two sampling units are required. variables is applied before transform.

For quantity vector g(\theta), write C_i = \operatorname{Cov}_{\theta\mid y}(g(\theta),\ell_i(\theta)). Its estimate uses sample covariance over draws with denominator S-1. For independent observations, K=N and each unit contribution is C_u=C_i. For clusters, K=J and C_u=\sum_{i\in u}C_i. The returned sampling covariance is

\widehat V_{\mathrm{IJ}}= \frac{K}{K-1}\sum_{u=1}^K (\widehat C_u-\overline C)(\widehat C_u-\overline C)^{\mathsf T}.

Here \overline C is the average unit contribution. This is the finite-sample convention used by Ji et al. (2026).

The target is E\{g(\theta)\mid y\}, which can differ from g\{E(\theta\mid y)\}. Transforms use jointly sampled quantities. Fixed-reference predictors are supported; extra sampling variation from estimating the reference distribution is not included. IJ covariance is sampling covariance of posterior expectations, not posterior draw covariance. It does not remove estimator bias. Validity depends on sampling units, likelihood factorization, and model regularity. Cluster aggregation alone does not validate arbitrary latent-effects models, few clusters, time series, informative cluster sampling, or multiway dependence.

Value

An ijse list containing:

estimate

Named vector of P posterior draw means.

posterior_sd, posterior_vcov

Named posterior sample SDs and a P\times P posterior sample covariance matrix.

se, vcov

Named IJ SEs and a P\times P IJ sampling covariance matrix. Both covariance matrices have quantity dimnames.

n_draws, n_obs, n_units

Counts S, N, and K. Without clustering, n_units == n_obs.

clustered

Whether cluster IDs were supplied. Unique per-row IDs still set this flag to TRUE, with observation-equivalent covariance.

unit_ids

Exact original cluster IDs, named by unique display labels, in first-appearance order; NULL for independent observations.

influence

NULL unless requested; otherwise a P\times K matrix equal to K\widehat C_u, with quantity rows and unit-label columns. See summary.ijse for interpretation.

diagnostics

List with mode, chain_ids, chain_se (chains by quantities), chain_relative_range, rhat, ess_bulk, and ess_tail. Uncomputed entries are NULL. Posterior metrics and relative ranges are named by quantity.

call

The matched function call.

Alignment and labels

Explicit row labels are checked when available on both sides. Names of a vector returned by transform are treated as draw-row labels. Automatic data-frame row numbers are positional. With unlabeled or incorrectly relabeled inputs, equal dimensions cannot establish correct row alignment. Labeled callback blocks must also agree with one another, including when the original draws are unlabeled. The first labeled block establishes that cross-block reference; unlabeled rows still require the caller's care. A callback must preserve observation-column order; it cannot be inferred.

Cluster and chain IDs are matched in order of first appearance. Their display labels use make.unique(as.character(ids)); distinct numeric IDs can otherwise print alike. Exact original cluster IDs are in unit_ids, named by those labels. Exact chain IDs are in diagnostics$chain_ids when diagnostics are enabled. Do not parse display labels to reconstruct IDs.

Diagnostics and memory

"all" computes chain-specific IJ SEs when each chain has at least two draws. R-hat and bulk/tail ESS require equally long chains with at least four draws each. The metrics describe selected or transformed quantities, not the precision of all quantity-likelihood covariances. Missing calculations are NULL; computed metrics can be NA, for example for constants. The chain relative range is (max(SE)-min(SE))/pooled_SE; it is a stability check, not a calibrated IJ Monte Carlo SE or a universal pass rule. For actual MCMC fits, also inspect sampler and nuisance-parameter diagnostics.

block_size limits callback requests, not storage of a matrix already supplied. Dense matrices use stats::cov(). Saved influence uses P\times K storage; it is off by default. Clustered computations retain aggregated quantity-by-cluster contributions even when influence is not saved. Finite inputs can overflow covariance calculations; rescale when requested.

References

Ji, F., Lee, J., and Rabe-Hesketh, S. (2026). Valid Standard Errors for Bayesian Quantile Regression With Clustered and Independent Data. Journal of Educational and Behavioral Statistics, 51(5). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.3102/10769986251379738")}. Giordano, R. and Broderick, T. (2024). The Bayesian Infinitesimal Jackknife for Variance. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.48550/arXiv.2305.06466")}.

See Also

ijse, summary.ijse, IJSE-package. Run vignette("ijse", package = "IJSE") for a complete executable introduction.

Examples

set.seed(12)
y <- c(-1, 0, 0.5, 1, 2)
v <- 1 / (length(y) + 1 / 4)
mu <- rnorm(2000, v * sum(y), sqrt(v))
draws <- cbind(mu = mu)
ll <- vapply(y, function(yi) dnorm(yi, mu, log = TRUE),
             numeric(length(mu)))
result <- ijse_draws(draws, ll)
summary(result)
vcov(result)
confint(result)
ijse_draws(draws, ll,
  transform = function(x) cbind(mu_squared = x[, 1]^2))
# A callback avoids storing the whole likelihood matrix.
ijse_draws(draws, function(i) ll[, i, drop = FALSE],
           n_obs = length(y), block_size = 2)

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