tests/testthat/test-bootstrap.R

test_that("regsen_boot returns valid percentile CI containing point estimate", {
    skip_on_cran()  # bootstrap is slow
    set.seed(1)
    res <- regsen_boot(bfg_formula(), bfg(),
                      compare = bfg_compare(),
                      cbar = 1,
                      R = 49,
                      show_progress = FALSE)
    expect_s3_class(res, "regsensitivity_boot")
    # Point estimate must lie between the bootstrap quantiles (almost
    # always for percentile CIs with R >= 25).
    expect_true(abs(res$point) >= abs(res$ci[1]) - 1e-6)
    expect_true(abs(res$point) <= abs(res$ci[2]) + 1e-6)
    expect_length(res$replicates, 49)
})

test_that("regsen_boot with cluster argument uses cluster resampling", {
    skip_on_cran()
    set.seed(2)
    res <- regsen_boot(bfg_formula(), bfg(),
                      compare = bfg_compare(),
                      cbar = 1,
                      cluster = "km_grid_cel_code",
                      R = 19,
                      show_progress = FALSE)
    expect_equal(res$cluster, "km_grid_cel_code")
    # Cluster bootstrap should give a slightly wider CI than the i.i.d.
    # bootstrap when there's clustering -- at minimum it should have run
    # without error.
    expect_true(is.finite(res$ci[1]))
    expect_true(is.finite(res$ci[2]))
})

test_that("regsen_boot accepts Oster analysis", {
    skip_on_cran()
    set.seed(3)
    res <- regsen_boot(bfg_formula(), bfg(),
                      compare = bfg_compare(),
                      analysis = "oster",
                      R = 19,
                      show_progress = FALSE)
    expect_s3_class(res, "regsensitivity_boot")
    expect_true(is.finite(res$point))
})

test_that("print method runs without error", {
    skip_on_cran()
    set.seed(4)
    res <- regsen_boot(bfg_formula(), bfg(),
                      compare = bfg_compare(),
                      cbar = 1,
                      R = 19,
                      show_progress = FALSE)
    expect_output(print(res), "Bootstrap confidence interval")
})

test_that("a seed gives identical replicates regardless of ncores", {
    skip_on_cran()
    # The property that matters for a replication package: results must not
    # depend on how the work was divided across cores.
    args <- list(bfg_formula(), bfg(), compare = bfg_compare(),
                 cbar = 1, R = 12, seed = 99, show_progress = FALSE)
    b1 <- do.call(regsen_boot, c(args, list(ncores = 1)))
    b2 <- do.call(regsen_boot, c(args, list(ncores = 2)))

    expect_equal(b1$replicates, b2$replicates)
    expect_equal(b1$ci, b2$ci)
})

test_that("a different seed gives different replicates", {
    skip_on_cran()
    a <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                      cbar = 1, R = 12, seed = 1, show_progress = FALSE)
    b <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                      cbar = 1, R = 12, seed = 2, show_progress = FALSE)
    expect_false(isTRUE(all.equal(a$replicates, b$replicates)))
})

## The RNG-state cases below run everywhere, unlike the rest of this
## file. `type = "perc"` skips the jackknife, which is what makes a
## bootstrap slow -- one breakdown computation per row -- and leaves a
## call that costs about a tenth of a second. What they check is a
## contract about the session, so it is worth checking on every platform.

test_that("a seeded run leaves the session stream where it found it", {
    # A bootstrap called from inside someone else's simulation must not
    # redirect their draws: with an explicit seed the call is transparent.
    set.seed(7)
    before <- runif(2)
    state <- get_random_seed()

    regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                cbar = 1, R = 4, type = "perc", seed = 123,
                show_progress = FALSE)

    expect_identical(get_random_seed(), state)

    # And the draws that follow are the ones that would have followed
    # anyway.
    after <- runif(2)
    set.seed(7)
    expect_equal(c(before, after), runif(4))
})

test_that("a seeded run leaves no RNG state where there was none", {
    # A session that has never drawn a random number has no .Random.seed.
    # Restoring that means leaving it absent, not leaving ours behind.
    old <- get_random_seed()
    on.exit(restore_random_seed(old), add = TRUE)
    restore_random_seed(NULL)

    regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                cbar = 1, R = 4, type = "perc", seed = 5,
                show_progress = FALSE)

    expect_false(exists(".Random.seed", envir = globalenv(),
                        inherits = FALSE))
})

test_that("an unseeded run advances the stream by one draw, not to a seed", {
    # Without a seed the call consumes randomness like any random
    # function, so consecutive calls must differ ...
    set.seed(8)
    a <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                      cbar = 1, R = 4, type = "perc",
                      show_progress = FALSE)
    b <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                      cbar = 1, R = 4, type = "perc",
                      show_progress = FALSE)
    expect_false(isTRUE(all.equal(a$replicates, b$replicates)))

    # ... and the stream is left exactly where drawing the replicate seeds
    # leaves it, not on the last replicate's seed. Nothing else in the
    # package draws, so that one call to sample.int() is the whole of it.
    set.seed(9)
    regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                cbar = 1, R = 4, type = "perc", show_progress = FALSE)
    got <- get_random_seed()

    set.seed(9)
    invisible(sample.int(.Machine$integer.max, 4))
    expect_identical(got, get_random_seed())
})

test_that("ncores is validated and capped at R", {
    skip_on_cran()
    expect_error(regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                              cbar = 1, R = 5, ncores = 0),
                 "positive integer")
    # More cores than replicates must not spawn idle workers. Kept at two
    # replicates so the request stays inside the two-process ceiling that
    # R CMD check --as-cran enforces.
    b <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                      cbar = 1, R = 2, ncores = 8, seed = 1,
                      show_progress = FALSE)
    expect_equal(b$ncores, 2L)
    expect_length(b$replicates, 2L)
})

test_that("regsen_boot reports both interval types", {
    skip_on_cran()
    res <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                       cbar = 1, R = 49, seed = 7, show_progress = FALSE)
    expect_equal(res$type, "bca")
    expect_equal(res$ci, res$ci_bca)
    expect_true(is.finite(res$z0))
    expect_true(is.finite(res$acceleration))
    expect_length(res$jackknife, res$point_res$n)
    # The two intervals read the same replicates at different quantiles.
    expect_true(all(res$ci_bca >= min(res$replicates, na.rm = TRUE) - 1e-9))
    expect_true(all(res$ci_bca <= max(res$replicates, na.rm = TRUE) + 1e-9))
    expect_false(isTRUE(all.equal(res$ci_bca, res$ci_perc)))

    perc <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                        cbar = 1, R = 49, seed = 7, type = "perc",
                        show_progress = FALSE)
    expect_equal(perc$ci, perc$ci_perc)
    # Same seed, same replicates: only the interval read off them differs.
    expect_equal(perc$replicates, res$replicates)
})

test_that("BCa constants have the meaning the interval assumes", {
    skip_on_cran()
    res <- regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                       cbar = 1, R = 99, seed = 11, show_progress = FALSE)
    share <- mean(res$replicates[is.finite(res$replicates)] < res$point)
    expect_equal(res$z0, stats::qnorm(share), tolerance = 1e-10)
    # The bootstrap distribution of the breakdown point sits below the point
    # estimate on this data, so the correction moves the interval up.
    expect_gt(res$z0, 0)
    expect_gt(res$ci_bca[1], res$ci_perc[1])
})

test_that("a degenerate jackknife falls back to the percentile interval", {
    reps <- rep(0.5, 50)                 # no spread at all
    out <- regsensitivity:::bca_interval(0.5, reps, rep(0.3, 20), 0.95)
    expect_true(out$fellback)
    expect_true(is.na(out$acceleration))
    expect_equal(out$ci, c(0.5, 0.5))
})

test_that("the caller's RNG state is not disturbed by seeding", {
    skip_on_cran()
    set.seed(123)
    before <- runif(1)
    set.seed(123)
    invisible(regsen_boot(bfg_formula(), bfg(), compare = bfg_compare(),
                           cbar = 1, R = 4, show_progress = FALSE))
    # No seed argument: the bootstrap consumes draws from the caller's
    # stream, so the next value must differ from the un-consumed one.
    after <- runif(1)
    expect_false(isTRUE(all.equal(before, after)))
})

Try the regsensitivity package in your browser

Any scripts or data that you put into this service are public.

regsensitivity documentation built on Nov. 23, 2026, 9:08 a.m.