Nothing
test_that("valid inputs preserve the released finite-sample convention", {
z <- small_inputs()
for (cluster in list(NULL, z$cluster, seq_len(ncol(z$ll)))) {
result <- ijse_draws(z$draws, z$ll, cluster_var = cluster, save_influence = TRUE)
expect_s3_class(result, "ijse")
expect_equal(result$se, upstream_se(z$draws, z$ll, cluster), tolerance = 1e-12)
k <- result$n_units
expect_equal(vcov(result), cov(t(result$influence)) / k, tolerance = 1e-12)
}
})
test_that("missing IDs are rejected rather than silently omitted", {
z <- small_inputs()
ids <- z$cluster
ids[2] <- NA_character_
expect_error(ijse_draws(z$draws, z$ll, ids), "cluster_var.*non-missing")
expect_error(ijse_draws(z$draws, z$ll, factor(ids)), "non-missing")
expect_error(ijse_draws(z$draws, z$ll, rep("a", 6)), "two clusters")
expect_error(ijse_draws(z$draws, z$ll[, 1, drop = FALSE]), "n_obs.*>= 2")
})
test_that("block sizes and streaming callbacks preserve full covariance", {
z <- small_inputs()
for (cluster in list(NULL, z$cluster)) {
reference <- ijse_draws(z$draws, z$ll, cluster, save_influence = TRUE)
for (size in c(1L, 2L, 5L, 6L, .Machine$integer.max)) {
calls <- integer()
callback <- function(ii) {
expect_lte(length(ii), size)
calls <<- c(calls, ii)
z$ll[, ii, drop = FALSE]
}
value <- ijse_draws(z$draws, callback, cluster, n_obs = 6,
block_size = size, save_influence = TRUE)
expect_identical(calls, seq_len(6))
expect_equal(vcov(value), vcov(reference), tolerance = 1e-12)
expect_equal(value$influence, reference$influence, tolerance = 1e-12)
}
}
})
test_that("cluster labels and joint permutations do not alter inference", {
z <- small_inputs()
original <- ijse_draws(z$draws, z$ll, z$cluster)
perm <- c(6, 1, 4, 2, 5, 3)
reordered <- ijse_draws(z$draws, z$ll[, perm], z$cluster[perm], block_size = 2)
expect_equal(vcov(reordered), vcov(original), tolerance = 1e-12)
factor_ids <- factor(z$cluster, levels = c("unused", "b", "a", "c"))
expect_equal(vcov(ijse_draws(z$draws, z$ll, factor_ids)), vcov(original))
expect_equal(vcov(ijse_draws(z$draws, z$ll, seq_len(6))),
vcov(ijse_draws(z$draws, z$ll)))
})
test_that("likelihood constants and coefficient ordering are respected", {
z <- small_inputs()
baseline <- ijse_draws(z$draws, z$ll)
shifted <- sweep(z$ll, 2, c(2, -3, 4, 9, -1, 7) * 1e5, "+")
expect_equal(vcov(ijse_draws(z$draws, shifted, block_size = 1)),
vcov(baseline), tolerance = 1e-10)
reordered <- ijse_draws(z$draws, z$ll, variables = c("b", "a"))
expect_equal(vcov(reordered), vcov(baseline)[c("b", "a"), c("b", "a")])
single <- ijse_draws(z$draws, z$ll, variables = "a")
expect_equal(dim(vcov(single)), c(1L, 1L))
expect_equal(single$se, baseline$se["a"])
})
test_that("transformed quantities target expectations of transformed draws", {
z <- small_inputs()
transformed <- ijse_draws(z$draws, z$ll, transform = function(d) {
cbind(indirect = d[, "a"] * d[, "b"], total = d[, "a"] + d[, "b"])
})
direct <- ijse_draws(cbind(indirect = z$draws[, 1] * z$draws[, 2],
total = rowSums(z$draws)), z$ll)
expect_equal(coef(transformed), coef(direct))
expect_equal(vcov(transformed), vcov(direct))
expect_gt(abs(coef(transformed)[1] - prod(colMeans(z$draws))), 0.01)
expect_equal(dim(vcov(ijse_draws(z$draws, z$ll,
transform = function(d) rowSums(d)))), c(1L, 1L))
df <- ijse_draws(as.data.frame(z$draws), as.data.frame(z$ll))
expect_equal(vcov(df), vcov(ijse_draws(z$draws, z$ll)))
})
test_that("influence agrees with independent weighted posterior differentiation", {
z <- small_inputs(80L)
result <- ijse_draws(z$draws, z$ll, save_influence = TRUE)
h <- 1e-5
weighted_mean <- function(log_weights) {
weights <- exp(log_weights - max(log_weights))
colSums(z$draws * weights) / sum(weights)
}
for (i in seq_len(ncol(z$ll))) {
derivative <- (weighted_mean(h * z$ll[, i]) -
weighted_mean(-h * z$ll[, i])) / (2 * h)
# Finite-sample covariance uses S-1; normalized empirical weights use S.
expected <- result$influence[, i] / result$n_units * (nrow(z$draws) - 1) / nrow(z$draws)
expect_equal(derivative, expected, tolerance = 1e-7)
}
})
test_that("normal mean and nonlinear transforms agree with analytic IJ targets", {
set.seed(6243)
y <- c(-0.2, 0.5, 1, 1.4, 1.7, 2.1)
v <- 1 / (length(y) + 1 / 4)
m <- v * sum(y)
mu <- rnorm(200000, m, sqrt(v))
ll <- vapply(y, function(yi) dnorm(yi, mu, log = TRUE), numeric(length(mu)))
result <- ijse_draws(cbind(mu = mu, squared = mu^2), ll)
target_se <- v * sqrt(length(y) * var(y)) * c(1, 2 * m)
expect_lt(max(abs(result$se / target_se - 1)), 0.02)
expect_lt(abs(coef(result)[2] / (m^2 + v) - 1), 0.01)
expect_lt(max(abs(vcov(result) / outer(target_se, target_se) - 1)), 0.03)
})
test_that("constant quantities have zero sampling variation", {
z <- small_inputs()
result <- ijse_draws(cbind(constant = rep(3, nrow(z$draws))), z$ll)
expect_identical(unname(result$se), 0)
expect_identical(unname(result$posterior_sd), 0)
expect_equal(unname(confint(result)), matrix(c(3, 3), nrow = 1))
})
test_that("cluster influence agrees with weighted cluster perturbations", {
z <- small_inputs(80L)
result <- ijse_draws(z$draws, z$ll, cluster_var = z$cluster, save_influence = TRUE)
h <- 1e-5
weighted_mean <- function(x) {
w <- exp(x - max(x))
colSums(z$draws * w) / sum(w)
}
for (id in unique(z$cluster)) {
contribution <- rowSums(z$ll[, z$cluster == id, drop = FALSE])
derivative <- (weighted_mean(h * contribution) - weighted_mean(-h * contribution)) / (2 * h)
expected <- result$influence[, id] / result$n_units * (nrow(z$draws) - 1) / nrow(z$draws)
expect_equal(derivative, expected, tolerance = 1e-7)
}
})
test_that("covariance is symmetric and positive semidefinite across dimensions", {
set.seed(88412)
for (p in c(1L, 2L, 7L)) {
for (n in c(2L, 5L, 11L)) {
d <- matrix(rnorm(30 * p), nrow = 30)
colnames(d) <- paste0("p", seq_len(p))
ll <- matrix(rnorm(30 * n), nrow = 30)
clusters <- rep(c("a", "b"), length.out = n)
for (ids in list(NULL, clusters)) {
result <- ijse_draws(d, ll, cluster_var = ids, block_size = 2)
expect_equal(result$se, upstream_se(d, ll, ids), tolerance = 1e-11)
expect_equal(vcov(result), t(vcov(result)))
expect_gte(min(eigen(vcov(result), symmetric = TRUE, only.values = TRUE)$values), -1e-10)
}
}
}
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.