
bioLeak is an R package for detecting, quantifying, and
diagnosing data leakage in biomedical machine-learning workflows. It
provides leakage-resistant resampling, guarded preprocessing, post-hoc
auditing, and inference tools for cross-validation and related
evaluation settings.
In scope:
Out of scope:
Standard cross-validation assumes independent samples and
exchangeable labels. Biomedical datasets often violate these assumptions
due to repeated measures, site effects, batch structure, and temporal
dependence. These violations can inflate performance metrics even when a
model does not generalize. bioLeak enforces leakage-aware
resampling and provides post-hoc diagnostics that estimate how much
apparent performance could be driven by leakage or confounding.
| Function | Description |
|---|---|
make_split_plan() |
Leakage-aware splits: subject-grouped, batch-blocked, study
leave-out, time-series, and N-axis combined modes with compact storage
option and constraint-aware train/test exclusion; stratification
balances groups or, with stratify_method = "rows", class
row counts per fold |
check_split_overlap() |
Explicit overlap-invariant validation across declared grouping axes |
as_rsample() |
Convert LeakSplits to an rsample rset for
tidymodels interoperability |
| Function | Description |
|---|---|
fit_resample() |
Cross-validated fitting with train-only imputation, normalization,
filtering, and feature selection; supports binomial, multiclass,
regression, and survival tasks; parallel execution via
future.apply |
tune_resample() |
Nested hyperparameter tuning via tidymodels
tune/dials with leakage-aware outer splits,
hyperparameter aggregation across folds, and optional threshold tuning;
survival tasks are not yet supported |
impute_guarded() |
Standalone train-only imputation (median, knn, missForest, none) |
guard_to_recipe() |
Convert guarded preprocessing specifications to recipes
pipelines |
| Function | Description |
|---|---|
audit_leakage() |
Permutation gap test, batch-outcome and fold-batch association tests, univariate and multivariate target leakage scans, near-duplicate detection with an expected chance count, and mechanism-level risk summaries |
audit_leakage_by_learner() |
Multi-learner auditing for multi-model fits |
audit_report() |
Self-contained HTML summary of audit results |
calibration_summary() |
Probability calibration checks |
confounder_sensitivity() |
Sensitivity analysis for confounding effects |
| Function | Description |
|---|---|
delta_lsi() |
Leakage sensitivity index (ΔLSI): Huber M-estimate of the paired performance gap, with a Nadeau–Bengio corrected repeated-CV t-test (p-value and confidence interval) and inference tiers by folds per repeat |
cv_ci() |
Cross-validation confidence intervals with Nadeau-Bengio correction |
| Function | Description |
|---|---|
simulate_leakage_suite() |
Generate controlled leakage scenarios for benchmarking audit sensitivity |
benchmark_leakage_suite() |
Reproducible modality-by-mechanism benchmark grids with detection-rate summaries |
plot_calibration(),
plot_confounder_sensitivity(),
plot_fold_balance(), plot_overlap_checks(),
plot_perm_distribution(), plot_time_acf()
Requires R >= 4.3.
From CRAN:
install.packages("bioLeak")Development version from GitHub:
install.packages("remotes")
remotes::install_github("selcukorkmaz/bioLeak")Non-obvious dependencies:
SummarizedExperiment (Bioconductor, optional) is needed
only for SummarizedExperiment input:
BiocManager::install("SummarizedExperiment").glmnet,
ranger, pROC, PRROC,
survival, future.apply, RANN,
rmarkdown, tune, dials.library(bioLeak)
set.seed(1)
n_subject <- 40
rep_per_subject <- 3
n <- n_subject * rep_per_subject
subject <- rep(seq_len(n_subject), each = rep_per_subject)
batch <- rep(seq_len(6), length.out = n_subject)[subject] # one batch per subject
# Subject-level latent risk creates dependence across repeated measures
subj_risk <- rnorm(n_subject, sd = 1)
x1 <- subj_risk[subject] + rnorm(n, sd = 0.5)
x2 <- rnorm(n)
x3 <- rnorm(n)
p_subj <- stats::plogis(1.5 * subj_risk)
outcome <- factor(ifelse(runif(n) < p_subj[subject], "case", "control"),
levels = c("control", "case"))
df <- data.frame(subject, batch, outcome, x1, x2, x3)
# Leakage-aware splits (subjects do not cross folds)
splits <- make_split_plan(
df,
outcome = "outcome",
mode = "subject_grouped",
group = "subject",
v = 5,
stratify = TRUE,
seed = 1
)
# Guarded pipeline (train-only preprocessing)
spec <- parsnip::logistic_reg(mode = "classification") |>
parsnip::set_engine("glm")
fit_guarded <- fit_resample(
df,
outcome = "outcome",
splits = splits,
learner = spec,
metrics = "auc",
preprocess = list(
impute = list(method = "median"),
normalize = list(method = "zscore"),
filter = list(var_thresh = 0),
fs = list(method = "none")
),
refit = FALSE,
seed = 1
)
# Leaky comparator: add a leakage feature computed on the full dataset
df_leaky <- within(df, {
leak_subject <- ave(as.numeric(outcome == "case"), subject, FUN = mean)
})
fit_leaky <- fit_resample(
df_leaky,
outcome = "outcome",
splits = splits,
learner = spec,
metrics = "auc",
preprocess = list(
impute = list(method = "none"),
normalize = list(method = "none"),
filter = list(var_thresh = 0),
fs = list(method = "none")
),
refit = FALSE,
seed = 1
)
# Use unstratified permutations here to avoid warnings in small grouped data
audit_guarded <- audit_leakage(
fit_guarded,
metric = "auc",
B = 30,
perm_stratify = FALSE,
X_ref = df[, c("x1", "x2", "x3")]
)
audit_leaky <- audit_leakage(
fit_leaky,
metric = "auc",
B = 30,
perm_stratify = FALSE,
X_ref = df_leaky[, c("x1", "x2", "x3", "leak_subject")]
)
summary(fit_guarded)
summary(fit_leaky)
summary(audit_guarded)
summary(audit_leaky)Interpretation notes:
leak_subject ranks near the top of the target leakage scan,
the performance gap is likely inflated by leakage.bioLeak integrates with the tidymodels ecosystem at
multiple levels:
fit_resample() and
tune_resample() accept rsample rset/rsplit
objects as splits. as_rsample() converts
LeakSplits to an rsample rset.preprocess can be a
recipes::recipe (prepped on training folds, baked on test
folds). guard_to_recipe() converts guarded preprocessing
specifications into a recipes pipeline.learner accepts
parsnip::model_spec or workflows::workflow
objects.metrics accepts
yardstick::metric_set objects.Note: When using recipes/workflows, the built-in guarded preprocessing list is not applied; ensure your recipe is leakage-safe.
Example (rsample + recipes + yardstick):
if (requireNamespace("rsample", quietly = TRUE) &&
requireNamespace("recipes", quietly = TRUE) &&
requireNamespace("yardstick", quietly = TRUE)) {
# grouped resampling; split_cols names the design, since df also has a
# batch column and bioLeak will not guess between the two
rs <- rsample::group_vfold_cv(df, group = subject, v = 5)
rec <- recipes::recipe(outcome ~ ., data = df) |>
recipes::step_normalize(recipes::all_numeric_predictors())
ys <- yardstick::metric_set(yardstick::roc_auc, yardstick::accuracy)
fit_rs <- fit_resample(
df,
outcome = "outcome",
splits = rs,
split_cols = c(group = "subject"),
learner = spec,
preprocess = rec,
metrics = ys,
refit = FALSE
)
}bioLeak supports four task types:
auc, pr_auc, and accuracy;
additional metrics can be supplied via
yardstick::metric_set when supported by the learner
outputs.accuracy, macro_f1, and
log_loss.rmse and cindex.cindex in fit_resample().The learner argument accepts parsnip model specs,
workflows, or built-in learner strings ("glmnet",
"ranger"). Models such as base R glm or
xgboost can still be used through parsnip/workflows or
custom learners, but they are not built-in character learner names.
Survival outcomes are supported in fit_resample(), but
support is less complete across the package. In particular,
tune_resample() does not yet support survival tasks.
X_ref can still pass undetected.p_value_adj,
flag_fdr) provide a more conservative screen.audit@info$duplicate_null); use
duplicate_scope = "all" to include within-fold duplicates
and review for data-quality issues.audit@info$mechanism_summary) provides a compact
mechanism-level risk view across permutation, confounding, target-proxy,
duplicate, and temporal signals.Common misinterpretations:
X_ref.Biomedical ML researchers, biostatisticians, and methodologists reviewing cross-validation and leakage risk.
citation("bioLeak")When reporting results, include: split mode, grouping columns, random seeds, preprocessing steps, learner specification, metrics, and audit settings (B, target threshold, similarity method). Include both guarded and leaky comparator results when used.
MIT