Refitting stability under explicit biological-unit resampling
Source:R/stability.R
factor_stability.RdRefit the model on bootstrap or subsample draws of biological units (e.g. donors) and report how often each reference factor is recovered and how similar the recovered factors are. Donor occurrences preserve all their visits/cells and get unique synthetic IDs. Similarity quantiles summarize successful perturbations, not posterior uncertainty. No threshold gives candidate similarities without recovery rates. Automatic fitting is supported for ordinary wrapper fits and pure reorders. Recorded feature-intercept recipes are replayed with a protected intercept; a reference with no biological factors is refused before resampling. Edited, projected and custom fits require an explicit fit_fn that reproduces their fitting procedure; supplying settings alone does not remove this guard.
Arguments
- X
The named matrix the reference fit was fitted on, same orientation.
- reference_fit
Native fit from
fit_ebmf()carrying its recorded settings.- 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.- seed
Integer master seed. Required.
- B
Number of resamples (default 100).
- scheme
"bootstrap" (default) draws units with replacement; "subsample" draws
fractionof the units per stratum without replacement, so no duplicated rows bias empirical Bayes prior and noise estimates. Bootstrap duplicates units, and in a simulation its refits fitted more factors than the reference (a higherrankin$replicates), which changes what is matched; "subsample" did not.- fraction
Subsample fraction in (0, 1); required for "subsample". Each stratum draws
ceiling(fraction * units)units (at least one), so a stratum with few units can draw all of them and is then not resampled. The function warns when that happens and records the realised fractions inanalysis_metadata$resampling_plan.- strata
Optional names of unit-level
metadatacolumns to stratify resampling by.- fit_fn
Optional callback
function(X, metadata, sample_side, seed, settings, observation_aux).NULL(default) replays the reference fit's recordedfit_ebmf()recipe; required for edited, projected or custom fits. Return a native flash fit with recorded declarations, or a representation created bystandardize_factors()with explicit units and prior support.- preprocess_fn
Optional per-replicate callback returning a list with
X,metadataandmanifest.- min_congruence
Optional Tucker congruence in (0, 1) a refit factor must exceed to count as recovered; equality is unmatched.
NULLkeeps all candidates.- workers
Number of parallel workers (default 1); Unix fork backend above one.
- checkpoint_dir
Optional directory for atomic per-replicate checkpoints.
- keep_fits
Retain every replicate fit (default
FALSE; memory heavy).- control
Optional named list:
settings,observation_auxandcallback_configurationpassed tofit_fn.
Value
A stability_result with $summary (per reference factor), $replicates, $matches,
$resamples and $failures. In $summary, recovery_rate is recovered
refits over eligible refits (refits that returned no factor are not
eligible), recovery_rate_all_requested is recovered refits over all B
requested and is the one to report, and n_matched counts the refits where
the factor was assigned a finite similarity. The congruence quantiles
(median_, q025_, q975_congruence) describe match quality among those
n_matched refits only: they say nothing about refits that lost the factor,
and with min_congruence set they cannot fall below it. Read them with
recovery_rate_all_requested. In $matches, similarity belongs to the
assigned pair, which one-to-one assignment can make lower than the best
candidate; best_similarity is the highest similarity of that reference
factor with any refit factor.
Details
Origin: NEW_ORCHESTRATION / call_only. Credits flash refits with existing congruence/assignment APIs; use factor_provenance("factor_stability") for audited sources and runtime versions. Input units and basis are retained; display/conditional results do not establish biological replication.
See also
factor_replicability() for one pair of fits
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)
)
# Two resamples illustrate execution, not reliable recovery estimates.
factor_stability(X, reference_fit = fit, metadata = metadata, unit_col = "donor", B = 2, seed = 4, min_congruence = 0.8)$summary
#> factor n_requested n_successful n_failed n_eligible n_matched recovery_rate
#> 1 F1 2 2 0 2 1 0.5
#> 2 F2 2 2 0 2 1 0.5
#> recovery_rate_all_requested median_congruence q025_congruence q975_congruence
#> 1 0.5 0.9986473 0.9986473 0.9986473
#> 2 0.5 0.9992962 0.9992962 0.9992962
#> native_pve_mean native_pve_sd orientation_flip_rate
#> 1 0.4576599 NA 1
#> 2 0.4955802 NA 0