tests/testthat/test-bugs-034.R

test_that("pointwise single-column matrices preserve draw and observation order", {
  d <- small_inputs()
  make <- function(shape) {
    fun <- function(data_i, draws) {
      v <- draws$ll[, data_i$i]
      switch(shape, column = matrix(v, ncol = 1), row = matrix(v, nrow = 1),
             multiple = cbind(v, v), vector = v)
    }
    attr(fun, "data") <- data.frame(i = seq_len(ncol(d$ll)))
    attr(fun, "draws") <- list(ll = d$ll, old_order = c(3, 1, 6, 2, 5, 4))
    fun
  }
  for (shape in c("column", "vector")) {
    stream <- IJSE:::brms_stream(make(shape))
    expect_equal(stream$read(c(6, 1, 4)), d$ll[, c(4, 3, 2), drop = FALSE])
  }
  for (shape in c("row", "multiple")) {
    stream <- IJSE:::brms_stream(make(shape))
    expect_error(stream$read(1:2), "one numeric vector")
  }
})

test_that("real categorical models support dense and streamed selected draws", {
  fit <- load_brms_fixture("categorical-034")
  vars <- c("b_mu2_x", "b_mu3_x")
  ids <- c(seq.int(2, 1200, by = 13), seq.int(1205, 2400, by = 17))
  d <- posterior::as_draws_matrix(fit, variable = vars)[ids, , drop = FALSE]
  ll <- brms::log_lik(fit, draw_ids = ids)
  dense <- ijse(fit, variables = vars, draw_ids = ids, diagnostics = "none")
  expect_equal(dense$se, upstream_se(d, ll), tolerance = 1e-11)
  for (block in c(1L, 7L, 43L)) {
    r <- ijse(fit, variables = vars, draw_ids = ids, block_size = block,
               save_influence = TRUE, diagnostics = "none")
    expect_equal(vcov(r), vcov(dense), tolerance = 1e-11)
    expect_equal(r$n_obs, 150L)
    expect_equal(r$n_draws, length(ids))
  }
})

test_that("unsafe automatic likelihood targets fail before extraction", {
  for (name in c("ar-covariance-034", "truncated-poisson-034", "unsaved-me-034")) {
    fit <- load_brms_fixture(name)
    message <- switch(name, "ar-covariance-034" = "conditional LOO",
      "truncated-poisson-034" = "Discrete truncated", "unsaved-me-034" = "latent draws were not saved")
    expect_error(ijse(fit, diagnostics = "none"), message)
    expect_error(ijse(fit, block_size = 7, diagnostics = "none"), message)
    expect_error(IJ_se(fit), message)
  }
})

test_that("likelihood guards inspect all multivariate responses and retain valid AR", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  f <- brms::bf(y ~ x, family = stats::gaussian()) +
    brms::bf(z | trunc(lb = 1) ~ x, family = stats::poisson()) + brms::set_rescor(FALSE)
  expect_error(IJSE:::check_brms_likelihood(list(formula = f)), "Discrete truncated")
  f <- brms::bf(y ~ x + ar(time = time, gr = group, cov = FALSE))
  expect_invisible(IJSE:::check_brms_likelihood(list(formula = f)))
  f <- brms::bf(y ~ x + arma(time = time, gr = group, p = 1, q = 1, cov = TRUE))
  expect_error(IJSE:::check_brms_likelihood(list(formula = f)), "conditional LOO")
})

test_that("measurement-error guards inspect retained draws rather than saving flags", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  f <- brms::bf(y ~ me(x, sdx))
  fit <- list(formula = f, save_pars = brms::save_pars(latent = "x"))
  expect_invisible(IJSE:::check_brms_likelihood(fit, "Xme_x[1]"))
  fit$save_pars <- brms::save_pars(manual = "Xme_1")
  expect_invisible(IJSE:::check_brms_likelihood(fit, "Xme_x[1]"))
  fit$save_pars <- brms::save_pars(all = TRUE, latent = TRUE)
  expect_error(IJSE:::check_brms_likelihood(fit, c("b_Intercept", "sigma")),
               "latent draws were not saved")
})

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.