tests/testthat/test-brms-deep.R

test_that("weighted Bernoulli likelihood uses the actual complete fitting rows", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  z <- readRDS(test_path("fixtures", "deep-brms.rds"))$binary
  d <- posterior::as_draws_matrix(z$fit, variable = c("b_Intercept", "b_x"))
  ll <- brms::log_lik(z$fit)
  manual <- vapply(seq_len(nrow(z$data)), function(i) {
    z$data$weight[i] * dbinom(z$data$binary[i], 1,
      plogis(d[, "b_Intercept"] + d[, "b_x"] * z$data$x[i]), log = TRUE)
  }, numeric(nrow(d)))
  expect_equal(ncol(ll), 87L)
  expect_equal(unname(ll), unname(manual), tolerance = 1e-12)
  expect_error(ijse(z$fit, cluster_var = z$original_data$unit), "length 87")
  result <- ijse(z$fit, cluster_var = z$cluster)
  expect_true(result$clustered)
  expect_equal(result$n_units, 15L)
  expect_equal(result$se, upstream_se(d, manual, z$cluster), tolerance = 1e-11)
})

test_that("nonlinear distributional likelihood agrees with its direct normal density", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  z <- readRDS(test_path("fixtures", "deep-brms.rds"))$nonlinear
  d <- posterior::as_draws_matrix(z$fit, variable = "^b_", regex = TRUE)
  ll <- brms::log_lik(z$fit)
  manual <- vapply(seq_len(nrow(z$data)), function(i) {
    mu <- d[, "b_a_Intercept"] + d[, "b_b_Intercept"] * z$data$x[i]
    sigma <- exp(d[, "b_sigma_Intercept"] + d[, "b_sigma_x"] * z$data$x[i])
    dnorm(z$data$continuous[i], mu, sigma, log = TRUE)
  }, numeric(nrow(d)))
  expect_equal(unname(ll), unname(manual), tolerance = 1e-12)
  expect_equal(ijse(z$fit, z$cluster)$se, upstream_se(d, manual, z$cluster),
               tolerance = 1e-11)
})

test_that("new brms integrations preserve full covariance under selection and streaming", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  bundle <- readRDS(test_path("fixtures", "deep-brms.rds"))
  for (z in bundle) {
    all_d <- posterior::as_draws_matrix(z$fit, variable = "^b_", regex = TRUE)
    all_ll <- brms::log_lik(z$fit)
    total <- nrow(all_d)
    subset <- c(seq.int(2L, total / 2L, by = 8L),
                seq.int(total / 2L + 4L, total, by = 7L))
    for (draw_ids in list(NULL, subset)) {
      d <- if (is.null(draw_ids)) all_d else all_d[draw_ids, , drop = FALSE]
      ll <- if (is.null(draw_ids)) all_ll else all_ll[draw_ids, , drop = FALSE]
      for (ids in list(NULL, z$cluster)) {
        dense <- ijse(z$fit, cluster_var = ids, draw_ids = draw_ids,
                       diagnostics = "none", save_influence = TRUE)
        expect_equal(dense$se, upstream_se(d, ll, ids), tolerance = 1e-11)
        expect_equal(coef(dense), colMeans(d))
        expect_equal(vcov(dense, type = "posterior"), cov(d))
        expect_equal(vcov(dense), cov(t(influence(dense))) / dense$n_units,
                     tolerance = 1e-11)
        for (block in c(1L, 11L, ncol(ll) + 1L)) {
          stream <- ijse(z$fit, cluster_var = ids, draw_ids = draw_ids,
                          block_size = block, diagnostics = "none", save_influence = TRUE)
          expect_equal(vcov(stream), vcov(dense), tolerance = 1e-11)
          expect_equal(influence(stream), influence(dense), tolerance = 1e-11)
        }
      }
    }
  }
})

test_that("nonlinear distributional transforms use joint draws after selection", {
  skip_if_not_installed("brms", minimum_version = "2.20.0")
  z <- readRDS(test_path("fixtures", "deep-brms.rds"))$nonlinear
  vars <- c("b_a_Intercept", "b_b_Intercept", "b_sigma_Intercept", "b_sigma_x")
  ids <- seq.int(1L, posterior::ndraws(z$fit), by = 3L)
  d <- posterior::as_draws_matrix(z$fit, variable = vars)[ids, , drop = FALSE]
  # IJSE passes a plain matrix to transforms. posterior's '[' method keeps
  # matrix subclasses on single-column selection and changes cbind dispatch.
  class(d) <- c("matrix", "array")
  fun <- function(x) cbind(mean_at_half = x[, "b_a_Intercept"] + x[, "b_b_Intercept"] / 2,
    noise_at_half = exp(x[, "b_sigma_Intercept"] + x[, "b_sigma_x"] / 2),
    standardized_slope = x[, "b_b_Intercept"] /
      exp(x[, "b_sigma_Intercept"] + x[, "b_sigma_x"] / 2))
  result <- ijse(z$fit, cluster_var = z$cluster, variables = vars,
    transform = fun, draw_ids = ids, block_size = 7, diagnostics = "posterior")
  expect_equal(coef(result), colMeans(fun(d)))
  expect_equal(result$se, upstream_se(fun(d), brms::log_lik(z$fit, draw_ids = ids), z$cluster),
               tolerance = 1e-11)
  expect_named(result$se, c("mean_at_half", "noise_at_half", "standardized_slope"))
})

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.