tests/testthat/test-bugs-033.R

test_that("used explicit NA factor levels are rejected as missing IDs", {
  z <- small_inputs(24)
  bad <- factor(c("a", "b", NA, "a", "b", NA), exclude = NULL)
  expect_false(anyNA(bad))
  expect_true(anyNA(as.character(bad)))
  expect_error(ijse_draws(z$draws, z$ll, bad), "cluster_var.*non-missing")
  chain <- factor(rep(c("a", NA), each = 3), exclude = NULL)
  for (mode in c("all", "posterior", "none")) {
    expect_error(ijse_draws(z$draws[1:6, , drop = FALSE],
                           z$ll[1:6, , drop = FALSE], chain_id = chain, diagnostics = mode),
                 "chain_id.*non-missing")
  }
  # An unused NA level and the literal string 'NA' are not missing values.
  valid <- factor(z$cluster, levels = c("a", "b", "c", NA), exclude = NULL)
  expect_equal(vcov(ijse_draws(z$draws, z$ll, valid)),
               vcov(ijse_draws(z$draws, z$ll, z$cluster)))
  strings <- ifelse(z$cluster == "a", "NA", z$cluster)
  expect_equal(vcov(ijse_draws(z$draws, z$ll, strings)),
               vcov(ijse_draws(z$draws, z$ll, z$cluster)))
})

test_that("callback row labels must agree across blocks even with unlabeled draws", {
  z <- small_inputs()
  rownames(z$ll) <- paste0("s", seq_len(nrow(z$draws)))
  callback <- function(ii) {
    x <- z$ll[, ii, drop = FALSE]
    if (min(ii) > 2) x <- x[nrow(x):1, , drop = FALSE]
    x
  }
  for (ids in list(NULL, z$cluster)) {
    expect_error(ijse_draws(z$draws, callback, ids, n_obs = 6, block_size = 2),
                 "Row names.*agree")
    good <- ijse_draws(z$draws, function(ii) z$ll[, ii, drop = FALSE],
                       ids, n_obs = 6, block_size = 2)
    expect_equal(vcov(good), vcov(ijse_draws(z$draws, z$ll, ids)))
  }
  # The first labeled block may come after one or more unlabeled blocks.
  late_labels <- function(ii) {
    x <- callback(ii)
    if (min(ii) == 1) rownames(x) <- NULL
    if (min(ii) == 3) x <- x[nrow(x):1, , drop = FALSE]
    x
  }
  expect_error(ijse_draws(z$draws, late_labels, n_obs = 6, block_size = 2),
               "Row names.*agree")
})

test_that("the first scatter merge cannot overflow a discarded zero term", {
  d <- cbind(theta = c(-1, 1))
  # Each observation has identical covariance 1e155. Centered scatter is zero;
  # the unneeded square of the first block mean exceeds double precision.
  ll <- matrix(rep(c(-5e154, 5e154), 4), 2)
  reference <- ijse_draws(d, ll, cluster_var = seq_len(ncol(ll)),
                          save_influence = TRUE)
  expect_identical(unname(reference$se), 0)
  plain <- ijse_draws(d, ll, save_influence = TRUE)
  expect_equal(plain$se, reference$se)
  expect_equal(vcov(plain), vcov(reference))
  expect_equal(influence(plain), influence(reference))
  for (block in c(1, 2, 4)) {
    stream <- ijse_draws(d, function(ii) ll[, ii, drop = FALSE],
                         n_obs = 4, block_size = block, save_influence = TRUE)
    expect_equal(vcov(stream), vcov(reference))
    expect_equal(influence(stream), influence(reference))
  }
})

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.