tests/testthat/test-brms.R

test_that("real Gaussian and quantile fits agree with released calculations", {
  for (name in c("gaussian", "quantile")) {
    fit <- load_brms_fixture(name)
    draws <- posterior::as_draws_matrix(fit, variable = "^b_", regex = TRUE)
    ll <- brms::log_lik(fit)
    expect_equal(IJ_se(fit), upstream_se(draws, ll), tolerance = 1e-11)
    result <- ijse(fit)
    expect_equal(result$se, IJ_se(fit))
    expect_equal(coef(result), colMeans(draws))
    expect_equal(result$n_draws, nrow(draws))
    expect_equal(dim(result$diagnostics$chain_se), c(2L, ncol(draws)))
    stream <- ijse(fit, block_size = 7, save_influence = TRUE)
    expect_equal(vcov(stream), vcov(result), tolerance = 1e-11)
    sampling <- load_brms_sampling_data()
    expect_equal(fit$data$x, sampling$x)
    expect_equal(fit$data$y, sampling$y)
    cluster <- sampling$school
    expect_length(cluster, ncol(ll))
    expect_length(unique(cluster), 8L)
    grouped <- ijse(fit, cluster)
    expect_true(grouped$clustered)
    expect_equal(grouped$n_units, 8L)
    expect_equal(IJ_se(fit, cluster), upstream_se(draws, ll, cluster), tolerance = 1e-11)
    expect_equal(vcov(ijse(fit, cluster, block_size = 5)),
                 vcov(ijse(fit, cluster)), tolerance = 1e-11)
  }
})

test_that("draw selection, transforms and single variables match brms likelihood rows", {
  fit <- load_brms_fixture("gaussian")
  total <- posterior::ndraws(fit)
  ids <- seq.int(1L, total, by = 3L)
  d <- posterior::as_draws_matrix(fit, variable = c("b_x", "sigma"))[ids, , drop = FALSE]
  rownames(d) <- NULL
  ll <- brms::log_lik(fit, draw_ids = ids)
  fun <- function(x) cbind(standardized = x[, "b_x"] / x[, "sigma"])
  result <- ijse(fit, variables = c("b_x", "sigma"), transform = fun,
                 draw_ids = ids, block_size = 4)
  reference <- ijse_draws(d, ll, transform = fun)
  expect_equal(vcov(result), vcov(reference), tolerance = 1e-11)
  expect_equal(coef(result), coef(reference))
  expect_named(ijse(fit, variables = "b_x")$se, "b_x")
  for (bad in list(c(2, 1), c(1, 1), 0, total + 1, 1.5, NA_real_)) {
    expect_error(ijse(fit, draw_ids = bad), "draw_ids")
  }
  cluster <- load_brms_sampling_data()$school
  expect_length(cluster, nrow(fit$data))
  expect_error(ijse(fit, cluster_var = replace(cluster, 1, NA)), "non-missing")
})

test_that("sorted AR models retain dense support and report unsupported streaming", {
  fit <- load_brms_fixture("sorted-ar")
  pointwise <- brms::log_lik(fit, pointwise = TRUE)
  old_order <- attr(pointwise, "draws")$old_order
  expect_false(identical(old_order, seq_along(old_order)))
  dense <- ijse(fit, cluster_var = fit$data$school, save_influence = TRUE)
  d <- posterior::as_draws_matrix(fit, variable = "^b_", regex = TRUE)
  expect_equal(dense$se, upstream_se(d, brms::log_lik(fit), fit$data$school),
               tolerance = 1e-11)
  expect_error(ijse(fit, block_size = 5), "Pointwise evaluation.*ARMA.*block_size = NULL")
})

test_that("multivariate fits use combined likelihoods and reject unsupported streaming", {
  fit <- load_brms_fixture("multivariate")
  d <- posterior::as_draws_matrix(fit, variable = "^b_", regex = TRUE)
  ll <- brms::log_lik(fit)
  expect_equal(IJ_se(fit), upstream_se(d, ll), tolerance = 1e-11)
  expect_error(ijse(fit, block_size = 3), "Multivariate streaming")
})

Try the IJSE package in your browser

Any scripts or data that you put into this service are public.

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