tests/testthat/test-bugs-032.R

test_that("named vector transforms cannot silently reorder draw identities", {
  z <- small_inputs()
  rownames(z$draws) <- rownames(z$ll) <- paste0("s", seq_len(nrow(z$draws)))
  identity <- ijse_draws(z$draws, z$ll, variables = "a")
  named <- ijse_draws(z$draws, z$ll, transform = function(d) d[, "a"])
  expect_equal(unname(named$se), unname(identity$se))
  expect_error(ijse_draws(z$draws, z$ll,
    transform = function(d) rev(d[, "a"])), "preserve posterior draw row order")
  row_ids <- rownames(z$draws)
  rownames(z$draws) <- NULL
  expect_error(ijse_draws(z$draws, z$ll,
    transform = function(d) rev(setNames(d[, "a"], row_ids))), "Row names.*agree")
})

test_that("automatic data-frame row names do not invent draw identities", {
  z <- small_inputs()
  rownames(z$draws) <- rownames(z$ll) <- paste0("s", seq_len(nrow(z$draws)))
  ref <- ijse_draws(z$draws, z$ll, variables = "a")
  transformed <- ijse_draws(z$draws, z$ll,
    transform = function(d) data.frame(a = unname(d[, "a"])))
  expect_equal(vcov(transformed), vcov(ref))
  plain <- as.data.frame(unname(z$draws))
  names(plain) <- colnames(z$draws)
  expect_equal(vcov(ijse_draws(plain, z$ll)), vcov(ijse_draws(z$draws, z$ll)))
  expect_error(ijse_draws(z$draws, z$ll,
    transform = function(d) as.data.frame(d[nrow(d):1, , drop = FALSE])),
    "preserve posterior draw row order")
})

test_that("valid confidence levels near one retain finite symmetric intervals", {
  z <- small_inputs()
  result <- ijse_draws(z$draws, z$ll)
  level <- 1 - .Machine$double.eps / 2
  critical <- qnorm((1 - level) / 2, lower.tail = FALSE)
  interval <- confint(result, level = level)
  expect_true(all(is.finite(interval)))
  expect_equal(unname(interval[, 2] - coef(result)), unname(result$se * critical))
  expect_equal(unname(coef(result) - interval[, 1]), unname(result$se * critical))
  constant <- ijse_draws(cbind(constant = rep(2, nrow(z$draws))), z$ll)
  expect_equal(unname(confint(constant, level = level)), matrix(2, 1, 2))
})

test_that("display labels do not lose distinct numeric sampling or chain IDs", {
  z <- small_inputs()
  cluster <- rep(c(1, 1 + 1e-15), each = 3)
  result <- ijse_draws(z$draws, z$ll, cluster, save_influence = TRUE)
  expect_false(anyDuplicated(colnames(influence(result))) > 0)
  expect_identical(unname(result$unit_ids), unique(cluster))
  expect_identical(names(result$unit_ids), colnames(influence(result)))
  expect_equal(vcov(result), vcov(ijse_draws(z$draws, z$ll, rep(1:2, each = 3))))
  ids <- rep(c(1, 1 + 1e-15), each = 3)
  diagnostic <- ijse_draws(z$draws[1:6, , drop = FALSE],
                          z$ll[1:6, , drop = FALSE], chain_id = ids)$diagnostics
  expect_false(anyDuplicated(rownames(diagnostic$chain_se)) > 0)
  expect_identical(unname(diagnostic$chain_ids), unique(ids))
  expect_identical(names(diagnostic$chain_ids), rownames(diagnostic$chain_se))
})

test_that("tiny positive confidence levels retain distinct nonzero bounds", {
  d <- cbind(theta = c(-1, 1))
  result <- ijse_draws(d, outer(d[,1], c(-1, 1), "*"))
  for (level in c(1e-20, 1e-12, 1e-9)) {
    ci <- confint(result, level = level)
    expect_lt(ci[1, 1], 0)
    expect_gt(ci[1, 2], 0)
    # Inverse central-probability derivative at zero is 1/(2*phi(0)).
    expect_equal(unname(ci[1, 2] / (result$se * level)),
                 1 / (2 * dnorm(0)), tolerance = 1e-14)
    expect_false(anyDuplicated(colnames(ci)) > 0)
  }
})

test_that("vector row-identity protection also applies to likelihood callbacks", {
  z <- small_inputs()
  row_ids <- paste0("s", seq_len(nrow(z$draws)))
  rownames(z$ll) <- rownames(z$draws) <- row_ids
  reader <- function(ii) z$ll[, ii, drop = FALSE]
  expect_error(ijse_draws(z$draws, reader, n_obs = 6, block_size = 2,
    transform = function(d) rev(d[, "a"])), "preserve posterior draw row order")
  rownames(z$draws) <- NULL
  expect_error(ijse_draws(z$draws, reader, n_obs = 6, block_size = 2,
    transform = function(d) rev(setNames(d[, "a"], row_ids))), "Row names.*agree")
})

test_that("ID metadata preserves factor and date classes without changing inference", {
  z <- small_inputs()
  for (ids in list(factor(z$cluster, levels = c("unused", "a", "b", "c")),
                   as.Date("2026-01-01") + c(1,2,1,3,3,2))) {
    result <- ijse_draws(z$draws, z$ll, ids, save_influence = TRUE)
    expect_identical(unname(result$unit_ids), unique(ids))
    expect_identical(names(result$unit_ids), colnames(influence(result)))
    expect_identical(summary(result)$unit_ids, result$unit_ids)
    expect_equal(vcov(result), vcov(ijse_draws(z$draws, z$ll, ids,
                                              save_influence = FALSE)))
  }
  independent <- ijse_draws(z$draws, z$ll)
  expect_null(independent$unit_ids)
  old <- independent
  old$unit_ids <- NULL
  expect_null(summary(old)$unit_ids)
})

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.