Nothing
# =======================================================================================
# TrexScreeningSelector - Unit tests
# =======================================================================================
test_that("TRexScreeningSelector instantiation and dimensional validation work", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- rnorm(n)
# Valid instantiation
expect_silent(TRexScreeningSelector$new(X, y, verbose = FALSE))
# Dimension mismatch
y_bad <- rnorm(n + 10)
expect_error(TRexScreeningSelector$new(X, y_bad, verbose = FALSE))
})
test_that("TRexScreeningSelector executes correctly with valid data", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- rnorm(n)
# Inject some strong signals in the first 3 variables
y <- X[, 1] * 5 + X[, 2] * -4 + X[, 3] * 3 + rnorm(n)
# Instantiate TRex selector
selector <- TRexScreeningSelector$new(X, y, verbose = FALSE)
# Execute variable selection
selector$select()
# Assert result structure and logic
res_indices <- selector$selected_indices
res_T_stop <- selector$T_stop
expect_type(res_indices, "integer")
expect_type(res_T_stop, "integer")
# Bounds check on output selected indices (1 <= index <= p)
expect_true(all(res_indices >= 1 & res_indices <= p))
})
test_that("TRexScreeningSelector behaves memory safe with memory mapping enabled", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- rnorm(n)
# use_memory_mapping = TRUE to confirm tempdir() doesn't crash
selector <- TRexScreeningSelector$new(X, y, verbose = FALSE,
control = trex_control(use_memory_mapping = TRUE))
selector$select()
res_indices <- selector$selected_indices
res_T_stop <- selector$T_stop
expect_type(res_indices, "integer")
})
test_that("TRexScreeningSelector correctly guards against invalid parameters", {
set.seed(42)
n <- 50
p <- 10
X <- matrix(rnorm(n * p), n, p)
y <- rnorm(n)
# Invalid Screening Variants that are caught by Rcpp string mapping
expect_error(
TRexScreeningSelector$new(X, y, screen_control = trex_screen_control(trex_method = "UNKNOWN_VARIANT"),
verbose = FALSE),
"Unknown ScreenTRexMethod:"
)
# rho_thr_DA is checked at construction: [0, 1], and [0, 1) for TREX_DA_AR1
guard <- function(...) {
TRexScreeningSelector$new(X, y, screen_control = trex_screen_control(...), verbose = FALSE)
}
expect_error(guard(trex_method = "TREX_DA_EQUI", rho_thr_DA = -0.5), "rho_thr_DA")
expect_error(guard(trex_method = "TREX_DA_EQUI", rho_thr_DA = 1.5), "rho_thr_DA")
expect_error(guard(trex_method = "TREX", rho_thr_DA = 1.5), "rho_thr_DA")
expect_error(guard(trex_method = "TREX_DA_AR1", rho_thr_DA = 1), "rho_thr_DA")
expect_error(guard(trex_method = "TREX_DA_NN", rho_thr_DA = 1), "rho_thr_DA")
# The bootstrap needs at least two resamples and a grid step in (0, 1], whether or not it is on
expect_error(guard(R_boot = 1), "R_boot")
expect_error(guard(R_boot = -5), "R_boot")
expect_error(guard(use_bootstrap_CI = TRUE, R_boot = 0), "R_boot")
expect_error(guard(ci_grid_step = 0), "ci_grid_step")
expect_error(guard(ci_grid_step = -0.01), "ci_grid_step")
expect_error(guard(use_bootstrap_CI = TRUE, ci_grid_step = 1.5), "ci_grid_step")
# TREX_DA_BLOCK_EQUI takes between 1 and p blocks
expect_error(guard(trex_method = "TREX_DA_BLOCK_EQUI", n_blocks = 0), "n_blocks")
expect_error(guard(trex_method = "TREX_DA_BLOCK_EQUI", n_blocks = -3), "n_blocks")
expect_error(guard(trex_method = "TREX_DA_BLOCK_EQUI", n_blocks = p + 1), "n_blocks")
})
test_that("TRexScreeningSelector replaces a cor_coef outside [-1, 1] by the estimate with a warning", {
set.seed(42)
n <- 60
p <- 30
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 8] * -4 + X[, 16] * 3 + rnorm(n)
# X + 0 hands every run its own copy of the design
run <- function(trex_method, cor_coef) {
screen_control <- trex_screen_control(trex_method = trex_method, cor_coef = cor_coef)
selector <- TRexScreeningSelector$new(X + 0, y, seed = 7, verbose = FALSE,
screen_control = screen_control, control = trex_control(K = 5))
warning_text <- capture.output(selector$select(), type = "message")
list(cor_coef = selector$estimated_correlation, warning_text = paste(warning_text, collapse = " "))
}
for (trex_method in c("TREX_DA_AR1", "TREX_DA_EQUI", "TREX_DA_BLOCK_EQUI", "TREX_DA_NN")) {
estimated <- run(trex_method, -2)
expect_false(grepl("cor_coef", estimated$warning_text), label = paste(trex_method, "sentinel is silent"))
for (unusable in c(-1.5, 1.5, -2.5)) {
replaced <- run(trex_method, unusable)
expect_equal(replaced$cor_coef, estimated$cor_coef, label = paste(trex_method, unusable, "estimate"))
expect_match(replaced$warning_text, "cor_coef", label = paste(trex_method, unusable, "warning"))
}
supplied <- run(trex_method, -0.5)
expect_equal(supplied$cor_coef, -0.5)
expect_false(grepl("cor_coef", supplied$warning_text))
}
})
test_that("TRexScreeningSelector reports an estimated FDR of 1 for an empty selection", {
set.seed(42)
n <- 80
p <- 40
X <- matrix(rnorm(n * p), n, p)
y_noise <- rnorm(n)
# A response orthogonal to the design and to the intercept leaves every variable behind
# the first dummy, so nothing is selected, with and without the bootstrap
y_orth <- as.numeric(resid(lm(y_noise ~ X)))
for (use_bootstrap in c(FALSE, TRUE)) {
selector <- TRexScreeningSelector$new(X + 0, y_orth, seed = 5, verbose = FALSE,
screen_control = trex_screen_control(use_bootstrap_CI = use_bootstrap,
R_boot = 100))
selector$select()
expect_equal(sum(selector$selected_var), 0)
expect_equal(selector$estimated_FDR, 1)
}
# With selections the estimate is one over their number
y_signal <- X[, 2] * 5 + X[, 10] * -5 + X[, 21] * 5 + 0.1 * y_noise
selector <- TRexScreeningSelector$new(X + 0, y_signal, seed = 5, verbose = FALSE)
selector$select()
expect_gt(sum(selector$selected_var), 0)
expect_equal(selector$estimated_FDR, 1 / sum(selector$selected_var))
})
test_that("TRexScreeningSelector TREX_DA_NN groups the neighbors inside the window", {
set.seed(42)
n <- 60
p <- 20
# X + 0 hands every run its own copy of the design
run <- function(X, y, trex_method, rho_thr_DA) {
screen_control <- trex_screen_control(trex_method = trex_method, cor_coef = 0.5, rho_thr_DA = rho_thr_DA)
selector <- TRexScreeningSelector$new(X + 0, y, seed = 7, verbose = FALSE,
screen_control = screen_control, control = trex_control(K = 10))
selector$select()
selector
}
# A zero threshold keeps every neighbor of the full window: the variant equals TREX_DA_AR1
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 8] * -4 + X[, 16] * 3 + rnorm(n)
nn_all <- run(X, y, "TREX_DA_NN", 0)
ar1_all <- run(X, y, "TREX_DA_AR1", 0)
expect_identical(nn_all$selected_var, ar1_all$selected_var)
expect_equal(nn_all$phi_mat, ar1_all$phi_mat)
# Centered orthonormal columns have no correlated neighbor: the variant equals the plain
# screening, and a penalty of one half for an empty group would select nothing
X_orth <- qr.Q(qr(scale(matrix(rnorm(n * p), n, p), scale = FALSE)))
y_orth <- X_orth[, 1] * 5 + X_orth[, 8] * -4 + X_orth[, 16] * 3 + rnorm(n, sd = 0.01)
nn_none <- run(X_orth, y_orth, "TREX_DA_NN", 0.5)
plain <- run(X_orth, y_orth, "TREX", 0.5)
expect_identical(nn_none$selected_var, plain$selected_var)
expect_equal(nn_none$phi_mat, plain$phi_mat)
expect_gt(sum(nn_none$selected_var), 0)
})
test_that("TRexScreeningSelector TREX_DA_AR1 handles the edges of its window", {
set.seed(42)
n <- 60
p <- 30
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 8] * -4 + X[, 16] * 3 + rnorm(n)
# X + 0 hands every run its own copy of the design
run <- function(cor_coef, rho_thr_DA) {
screen_control <- trex_screen_control(trex_method = "TREX_DA_AR1", cor_coef = cor_coef,
rho_thr_DA = rho_thr_DA)
selector <- TRexScreeningSelector$new(X + 0, y, seed = 7, verbose = FALSE,
screen_control = screen_control, control = trex_control(K = 10))
selector$select()
selector
}
# The window follows abs(cor_coef)
neg <- run(-0.5, 0.02)
pos <- run(0.5, 0.02)
expect_identical(neg$selected_var, pos$selected_var)
expect_equal(neg$phi_mat, pos$phi_mat)
# A zero threshold is the full window, as a coefficient of magnitude 1 gives it
full <- run(0.5, 0)
unit <- run(1, 0.02)
expect_true(all(is.finite(full$phi_mat)))
expect_equal(full$phi_mat, unit$phi_mat)
})
test_that("TrexScreeningSelector correctly executes confidence-interval based bootstrapping", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 2] * -4 + rnorm(n)
# Instantiate with CI Bootstrapping active
selector_boot <- expect_no_error(
TRexScreeningSelector$new(
X, y,
screen_control = trex_screen_control(use_bootstrap_CI = TRUE, R_boot = 100, ci_grid_step = 0.01),
verbose = FALSE
)
)
selector_boot$select()
# Assert result structure includes Bootstrap diagnostics
expect_true(selector_boot$used_bootstrap)
expect_type(selector_boot$confidence_level, "double")
# Ensure FDR is bounded properly
expect_true(selector_boot$estimated_FDR >= 0.0 && selector_boot$estimated_FDR <= 1.0)
})
test_that("TRexScreeningSelector handles concurrent execution properly (OpenMP)", {
set.seed(42)
n <- 50
p <- 10
X <- matrix(rnorm(n * p), n, p)
y <- rnorm(n)
# Run multithreaded (runs one sweeping K-loop for fast Screening)
selector_openmp <- expect_no_error(
TRexScreeningSelector$new(X, y,
control = trex_control(parallel_rnd_experiments = TRUE, max_inner_threads = 2,
max_outer_threads = 2),
verbose = FALSE)
)
selector_openmp$select()
res_openmp_indices <- selector_openmp$selected_indices
res_openmp_T_stop <- selector_openmp$T_stop
expect_type(res_openmp_indices, "integer")
})
test_that("TRexScreeningSelector selected_var and selected_indices are consistent (1-based)", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 2] * -4 + X[, 3] * 3 + rnorm(n)
selector <- TRexScreeningSelector$new(X, y, verbose = FALSE)
selector$select()
idx <- selector$selected_indices
sv <- selector$selected_var
expect_type(idx, "integer")
expect_length(sv, p)
expect_true(all(idx >= 1L & idx <= p))
expect_equal(sort(idx), which(sv))
})
test_that("TRexScreeningSelector phi_prime has length p and phi_mat has correct dimensions", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 2] * -4 + rnorm(n)
selector <- TRexScreeningSelector$new(X, y, verbose = FALSE)
selector$select()
phi_p <- selector$phi_prime
expect_length(phi_p, p)
expect_true(is.numeric(phi_p))
ts <- selector$T_stop
pm <- selector$phi_mat
if (ts > 0L) {
expect_gte(nrow(pm), 1L)
expect_equal(ncol(pm), p)
}
})
test_that("TRexScreeningSelector screening-specific fields have valid values", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 2] * -4 + rnorm(n)
selector <- TRexScreeningSelector$new(X, y, verbose = FALSE)
selector$select()
# estimated_correlation is a non-negative double
expect_type(selector$estimated_correlation, "double")
expect_true(selector$estimated_correlation >= 0.0)
# beta_mat and dummy_betas are matrices with positive dimensions
bm <- selector$beta_mat
db <- selector$dummy_betas
expect_true(is.matrix(bm))
expect_true(nrow(bm) > 0 && ncol(bm) > 0)
expect_true(is.matrix(db))
expect_true(nrow(db) > 0 && ncol(db) > 0)
})
test_that("TRexScreeningSelector DA-method variants run without error and return valid indices", {
set.seed(42)
n <- 50
p <- 20
X <- matrix(rnorm(n * p), n, p)
y <- X[, 1] * 5 + X[, 2] * -4 + rnorm(n)
for (tm in c("TREX_DA_AR1", "TREX_DA_EQUI", "TREX_DA_BLOCK_EQUI", "TREX_DA_NN")) {
sel <- TRexScreeningSelector$new(X, y,
screen_control = trex_screen_control(trex_method = tm),
verbose = FALSE)
sel$select()
idx <- sel$selected_indices
sv <- sel$selected_var
expect_type(idx, "integer")
expect_length(sv, p)
expect_true(all(idx >= 1L & idx <= p), label = paste(tm, "1-based in [1,p]"))
expect_equal(sort(idx), which(sv), label = paste(tm, "consistency"))
}
})
test_that("TRexScreeningSelector takes the four strategies of the core and refuses HCONCAT and SKIPL", {
set.seed(11)
n <- 100
p <- 60
X <- matrix(rnorm(n * p), n, p)
y <- as.numeric(X[, 1:5] %*% rep(3, 5) + rnorm(n))
run <- function(strategy, seed) {
sel <- TRexScreeningSelector$new(X, y, seed = seed, verbose = FALSE,
control = trex_control(K = 10, lloop_strategy = strategy))
sel$select()
sel$phi_mat
}
# The strategy of the control reaches the selector: the permutation runs differ from
# the default run, and each strategy equals its seeded twin. The relative occurrences
# of the two draws coincide for some seeds, so the test takes the first seed at which
# they differ.
seed <- 3
while (seed < 203 && identical(run("PERMUTATION", seed), run("STANDARD", seed))) {
seed <- seed + 1
}
standard <- run("STANDARD", seed)
permutation <- run("PERMUTATION", seed)
expect_false(identical(permutation, standard), info = "200 seeds in a row gave equal occurrences")
expect_identical(run("SEEDED", seed), standard)
expect_identical(run("PERMUTATION_SEEDED", seed), permutation)
# The screening is one pass at L = p, so the growth strategies are refused
for (strategy in c("HCONCAT", "SKIPL")) {
expect_error(run(strategy, seed), "only the STANDARD, SEEDED, PERMUTATION, and PERMUTATION_SEEDED", fixed = TRUE)
}
})
test_that("TRexScreeningSelector holds the fixed values of a screening run in the inherited fields", {
set.seed(11)
n <- 100
p <- 60
X <- matrix(rnorm(n * p), n, p)
y <- as.numeric(X[, 1:5] %*% rep(3, 5) + rnorm(n))
sel <- TRexScreeningSelector$new(X, y, seed = 3, verbose = FALSE, control = trex_control(K = 10))
sel$select()
n_selected <- length(sel$selected_indices)
# One pass at T = 1 and L = p, without a target and without a calibration
expect_equal(sel$T_stop, 1)
expect_equal(sel$L, 1)
expect_equal(sel$num_dummies, p)
expect_identical(sel$v_thresh, 0.5)
expect_identical(sel$tFDR, 0)
expect_equal(dim(sel$r_mat), c(1L, 1L))
expect_equal(sel$r_mat[1, 1], n_selected)
expect_equal(dim(sel$fdp_hat_mat), c(1L, 1L))
expect_identical(sel$fdp_hat_mat[1, 1], 0)
expect_equal(as.numeric(sel$phi_prime), as.numeric(sel$phi_mat))
expect_identical(sel$estimated_FDR, 1 / max(1, n_selected))
# The fields of the screening without bootstrap and without a filter
expect_false(sel$used_bootstrap)
expect_identical(sel$confidence_level, 0)
expect_identical(sel$estimated_correlation, 0)
expect_equal(dim(sel$beta_mat), c(p, 1L))
expect_gt(length(sel$dummy_betas), 0L)
expect_true(all(sel$dummy_betas != 0))
# The bootstrap reports its confidence level, and a supplied coefficient is reported as it is
boot <- TRexScreeningSelector$new(X, y, seed = 3, verbose = FALSE, control = trex_control(K = 10),
screen_control = trex_screen_control(use_bootstrap_CI = TRUE, R_boot = 200))
boot$select()
expect_true(boot$used_bootstrap)
expect_gt(boot$confidence_level, 0)
expect_lt(boot$confidence_level, 1)
ar1 <- TRexScreeningSelector$new(X, y, seed = 3, verbose = FALSE, control = trex_control(K = 10),
screen_control = trex_screen_control(trex_method = "TREX_DA_AR1",
cor_coef = 0.5))
ar1$select()
expect_identical(ar1$estimated_correlation, 0.5)
})
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.