Cross-validate by biological unit: fit on training units through your callback, project the fitted programs onto held-out units, and score prediction of withheld entries against a training-mean baseline. Projection scores use observations seen during activity estimation. Masked scores use disjoint entries/features and never enter activity/noise fitting. Learned hyperparameters may recalibrate on permitted test observations; control$prior_policy = "fixed_reference" fixes activity priors to the training fit, while residual variance still recalibrates. Trained noise is never frozen, and folds run serially. Paired preprocessing callbacks fit on training data then apply frozen state; masked inference receives NA before transform. Global preprocessing is labelled conditional and cannot establish independent full-pipeline validation.
Usage
factor_holdout(
X,
metadata,
unit_col,
folds,
fit_fn,
seed,
preprocess_fn = NULL,
evaluation = "projection",
activity_features = NULL,
scoring_features = NULL,
mask = NULL,
checkpoint_dir = NULL,
control = list()
)Arguments
- X
Named numeric matrix;
control$sample_sidesays which side holds samples.- metadata
Data frame with one row per sample, a
samplecolumn and the unit column.- unit_col
Name of the biological unit column, e.g.
"donor". Required.- folds
Data frame with a
foldcolumn and either asampleor a unit ID column; all samples of a unit must share a fold.- fit_fn
Callback
function(X, metadata, sample_side, seed, settings, observation_aux)that fits the training data and returns a native fit fromfit_ebmf()or a view. Required.- seed
Integer master seed. Required.
- preprocess_fn
NULL(default) orlist(fit, transform):fit(X, metadata, ...)returnsX,metadata,manifest,statefrom training data;transform(X, metadata, state, seed, settings)applies that frozen state to test data.- evaluation
"projection"(default; score the entries used to estimate test activities) or"masked"(estimate activities from some features, score disjoint withheld entries).- activity_features, scoring_features
For
"masked": disjoint character feature sets used to estimate activities and to score prediction.- mask
For
"masked": optional named logical matrix;TRUEentries are withheld for scoring.- checkpoint_dir
Optional directory for fold checkpoints; needs
control$callback_configuration.- control
Named list. Required:
sample_side("rows"or"columns") andpreprocessing_manifest(e.g.list(scope = "identity")) when nopreprocess_fn. Optional:settingsandobservation_auxpassed tofit_fn,projection_ebnm_fn,maxiter,dense_budget_bytes,cohort_col,evaluated_folds,callback_configuration,keep_predictions,keep_fits,prior_policy("query"or"fixed_reference"; seeproject_factor_activity()).
Value
A holdout_result with $metrics (per fold, model and weighting:
rmse, mae, prediction_r2, skill and correlation for the fit and the baseline; macro rows add
n_units_<metric>, the units each average used). prediction_r2 is
1 - SSE / SST with SST centred on the mean of the scored held-out entries
pooled over features, so feature means alone already score high. skill
is 1 - SSE(fit) / SSE(training mean) on the same entries: 0 is no better
than the training-mean baseline, and it is NA for the baseline row. Read
the fixed_programs row next to the training_mean row. Also $folds,
$failures, $activities and information boundaries. Each fold's seed is
drawn per fold label and recorded in $analysis_metadata$fold_seeds.
Details
Origin: NEW_ORCHESTRATION / call_only. Credits flash_init; flash_factors_init; flash_factors_fix; flash_backfit; use factor_provenance("factor_holdout") for audited sources and runtime versions. Input units and basis are retained; display/conditional results do not establish biological replication.
Author
Jason Willwerscheid; Peter Carbonetto; Wei Wang; Matthew Stephens. Full source and dependency credits: https://github.com/mdmanurung/flashier-utils/blob/main/CREDITS.md.
Examples
set.seed(3)
X <- tcrossprod(matrix(rnorm(48), 24, 2), matrix(rnorm(32), 16, 2)) +
matrix(rnorm(384, sd = 0.2), 24, 16)
dimnames(X) <- list(paste0("s", 1:24), paste0("g", 1:16))
fit <- fit_ebmf(X, "rows",
max_factors = 2, seed = 4, verbose = 0,
ebnm_fn = list(ebnm::ebnm_normal, ebnm::ebnm_point_laplace),
nullcheck = FALSE, backfit = TRUE, feature_scale = "simulated_centered_intensity"
)
view <- standardize_factors(fit)
metadata <- data.frame(
sample = rownames(X), donor = rownames(X),
group = rep(c("A", "B"), each = 12)
)
folds <- data.frame(sample = metadata$sample, fold = rep(c("train", "test"), c(16, 8)))
callback <- function(X, metadata, sample_side, seed, settings, observation_aux = list()) {
fit_ebmf(X, sample_side,
max_factors = 2, seed = seed, verbose = 0, nullcheck = FALSE,
feature_scale = "simulated_centered_intensity"
)
}
control <- list(sample_side = "rows", preprocessing_manifest = list(scope = "identity"), evaluated_folds = "test")
factor_holdout(X,
metadata = metadata, unit_col = "donor", folds = folds, fit_fn = callback, evaluation = "masked", seed = 4,
activity_features = paste0("g", 1:8), scoring_features = paste0("g", 9:16), control = control
)$metrics
#> fold evaluation_type model weighting unit cohort
#> test.rmse test masked fixed_programs observation <NA> <NA>
#> test.rmse1 test masked training_mean observation <NA> <NA>
#> test.rmse2 test masked fixed_programs unit s17 <NA>
#> test.rmse3 test masked training_mean unit s17 <NA>
#> test.rmse4 test masked fixed_programs unit s18 <NA>
#> test.rmse5 test masked training_mean unit s18 <NA>
#> test.rmse6 test masked fixed_programs unit s19 <NA>
#> test.rmse7 test masked training_mean unit s19 <NA>
#> test.rmse8 test masked fixed_programs unit s20 <NA>
#> test.rmse9 test masked training_mean unit s20 <NA>
#> test.rmse10 test masked fixed_programs unit s21 <NA>
#> test.rmse11 test masked training_mean unit s21 <NA>
#> test.rmse12 test masked fixed_programs unit s22 <NA>
#> test.rmse13 test masked training_mean unit s22 <NA>
#> test.rmse14 test masked fixed_programs unit s23 <NA>
#> test.rmse15 test masked training_mean unit s23 <NA>
#> test.rmse16 test masked fixed_programs unit s24 <NA>
#> test.rmse17 test masked training_mean unit s24 <NA>
#> test.rmse21 test masked fixed_programs macro_unit <NA> <NA>
#> test.rmse31 test masked training_mean macro_unit <NA> <NA>
#> rmse mae prediction_r2 skill correlation
#> test.rmse 0.2388830 0.1861926 0.95923968 0.9576021 0.9797082
#> test.rmse1 1.1601462 0.8612919 0.03862514 NA 0.2301984
#> test.rmse2 0.1893243 0.1597362 0.96771942 0.9418339 0.9950889
#> test.rmse3 0.7850029 0.6501366 0.44502795 NA 0.9688445
#> test.rmse4 0.3109650 0.2538024 0.72507091 0.7818093 0.9024999
#> test.rmse5 0.6657227 0.5057920 -0.26004031 NA 0.2035596
#> test.rmse6 0.3466820 0.2767473 0.92308065 0.9493328 0.9690002
#> test.rmse7 1.5401668 1.3405764 -0.51812811 NA 0.1582791
#> test.rmse8 0.2296171 0.1777237 0.89556475 0.9512939 0.9924598
#> test.rmse9 1.0404295 0.8186464 -1.14419351 NA -0.9283800
#> test.rmse10 0.1473523 0.1217385 0.93626439 0.8460159 0.9709563
#> test.rmse11 0.3755078 0.3305005 0.58608971 NA 0.8884216
#> test.rmse12 0.2477255 0.1880027 0.97335673 0.9839612 0.9873526
#> test.rmse13 1.9560688 1.6635852 -0.66117085 NA -0.4550050
#> test.rmse14 0.1993572 0.1761878 0.15588183 0.6735480 0.5034217
#> test.rmse15 0.3489171 0.2781492 -1.58573411 NA 0.1612909
#> test.rmse16 0.1680512 0.1356021 0.99047141 0.9869528 0.9965863
#> test.rmse17 1.4712364 1.3029493 0.26968550 NA 0.9503836
#> test.rmse21 0.2298843 0.1861926 0.82092626 0.8893435 0.9146707
#> test.rmse31 1.0228815 0.8612919 -0.35855797 NA 0.2434243
#> n_scored_entries n_samples n_units n_features n_units_rmse
#> test.rmse 64 8 8 8 NA
#> test.rmse1 64 8 8 8 NA
#> test.rmse2 8 1 1 8 NA
#> test.rmse3 8 1 1 8 NA
#> test.rmse4 8 1 1 8 NA
#> test.rmse5 8 1 1 8 NA
#> test.rmse6 8 1 1 8 NA
#> test.rmse7 8 1 1 8 NA
#> test.rmse8 8 1 1 8 NA
#> test.rmse9 8 1 1 8 NA
#> test.rmse10 8 1 1 8 NA
#> test.rmse11 8 1 1 8 NA
#> test.rmse12 8 1 1 8 NA
#> test.rmse13 8 1 1 8 NA
#> test.rmse14 8 1 1 8 NA
#> test.rmse15 8 1 1 8 NA
#> test.rmse16 8 1 1 8 NA
#> test.rmse17 8 1 1 8 NA
#> test.rmse21 64 8 8 8 8
#> test.rmse31 64 8 8 8 8
#> n_units_mae n_units_prediction_r2 n_units_skill n_units_correlation
#> test.rmse NA NA NA NA
#> test.rmse1 NA NA NA NA
#> test.rmse2 NA NA NA NA
#> test.rmse3 NA NA NA NA
#> test.rmse4 NA NA NA NA
#> test.rmse5 NA NA NA NA
#> test.rmse6 NA NA NA NA
#> test.rmse7 NA NA NA NA
#> test.rmse8 NA NA NA NA
#> test.rmse9 NA NA NA NA
#> test.rmse10 NA NA NA NA
#> test.rmse11 NA NA NA NA
#> test.rmse12 NA NA NA NA
#> test.rmse13 NA NA NA NA
#> test.rmse14 NA NA NA NA
#> test.rmse15 NA NA NA NA
#> test.rmse16 NA NA NA NA
#> test.rmse17 NA NA NA NA
#> test.rmse21 8 8 8 8
#> test.rmse31 8 8 0 8