tests/testthat/test-v03.R

test_that("unequal posterior data-frame chains use their actual metadata", {
  z <- small_inputs(48)
  arr <- posterior::as_draws_array(array(z$draws, c(24, 2, 2),
                    dimnames = list(NULL, NULL, c("a", "b"))))
  df <- posterior::as_draws_df(arr)[-1, ]
  ll <- z$ll[-1, , drop = FALSE]
  result <- ijse_draws(df, ll)
  reference <- ijse_draws(posterior::as_draws_matrix(df), ll, chain_id = df$.chain)
  expect_equal(result$n_draws, 47L)
  expect_equal(vcov(result), vcov(reference))
  expect_equal(result$diagnostics$chain_se, reference$diagnostics$chain_se)
  expect_null(result$diagnostics$rhat)
})

test_that("diagnostics modes preserve inference without computing unused chain SEs", {
  z <- small_inputs(48)
  ids <- rep(1:2, each = 24)
  all <- ijse_draws(z$draws, z$ll, chain_id = ids)
  for (mode in c("posterior", "none")) {
    result <- ijse_draws(z$draws, z$ll, chain_id = ids, diagnostics = mode)
    expect_equal(vcov(result), vcov(all))
    expect_equal(result$se, all$se)
    expect_equal(result$diagnostics$mode, mode)
    expect_null(result$diagnostics$chain_se)
    if (mode == "posterior") expect_equal(result$diagnostics$rhat, all$diagnostics$rhat)
    else expect_null(result$diagnostics$rhat)
  }
  sparse_ids <- c(1, rep(2, 47))
  expect_error(ijse_draws(z$draws, z$ll, chain_id = sparse_ids), "at least two")
  expect_equal(ijse_draws(z$draws, z$ll, chain_id = sparse_ids,
                         diagnostics = "none")$se, all$se)
  expect_error(ijse_draws(z$draws, z$ll, diagnostics = "typo"), "arg")
})

test_that("posterior covariance and saved influence have explicit methods", {
  z <- small_inputs()
  result <- ijse_draws(z$draws, z$ll, cluster_var = z$cluster, save_influence = TRUE)
  expect_equal(vcov(result, type = "posterior"), cov(z$draws))
  expect_equal(sqrt(diag(vcov(result, type = "posterior"))), result$posterior_sd)
  expect_equal(influence(result), result$influence)
  expect_equal(colnames(influence(result)), unique(z$cluster))
  expect_equal(cov(t(influence(result))) / result$n_units, vcov(result))
  expect_error(influence(ijse_draws(z$draws, z$ll)), "save_influence = TRUE")
  expect_error(vcov(result, type = "typo"), "arg")
  old_result <- result
  old_result$posterior_vcov <- NULL
  expect_error(vcov(old_result, type = "posterior"), "unavailable.*Recompute")
})

test_that("brms diagnostics modes and legacy wrapper retain parity", {
  fit <- load_brms_fixture("gaussian")
  all <- ijse(fit)
  for (mode in c("posterior", "none")) {
    result <- ijse(fit, diagnostics = mode)
    expect_equal(result$se, all$se)
    expect_null(result$diagnostics$chain_se)
  }
  expect_equal(IJ_se(fit), all$se)
})

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.