Transfer fixed reference programs to a new dataset
Source:R/projection.R
project_factor_activity.RdKeep the reference feature effects fixed and estimate activities for new samples, so programs learned on one dataset can be scored on another. The query must be preprocessed the same way as the reference; you declare that.
Usage
project_factor_activity(
fit,
query,
sample_side,
feature_scale,
preprocessing,
factors = NULL,
ebnm_fn = NULL,
prior_policy = c("query", "fixed_reference"),
feature_match = "exact",
gene_mapping = NULL,
min_coverage = 0.5,
maxiter = 200L,
S = NULL,
var_type = NULL,
dense_budget_bytes = 256 * 1024^2
)Arguments
- fit
Native reference fit or a view from
standardize_factors().- query
Named matrix of new samples measured on the reference features;
sample_sidesays which side holds samples. Needs at least two samples.- sample_side
"rows"or"columns"ofquery. Required.- feature_scale
Free-text label for what the query values measure. Required.
- preprocessing
Named list with character
scopeanddescriptionof the query preprocessing, e.g.list(scope = "within_dataset", description = "log1p library-size normalized counts"). Required; the caller declares compatibility with the reference.- factors
Optional character factor IDs to keep fixed, in source order. Default all.
- ebnm_fn
Optional native prior for the new features. The recorded activity prior is reused; the default new-feature prior is signed point-Laplace.
- prior_policy
"query" (default) re-estimates activity priors on the query; "fixed_reference" reuses the reference fit's estimated priors.
- feature_match
"exact"(default, all reference features must be present) or"intersection"(use the shared features, subject tomin_coverage).- gene_mapping
Optional character vector mapping query feature names (names) to reference feature names (values), one to one.
- min_coverage
Minimum fraction of reference features present (default 0.5).
- maxiter
Native iteration limit (default 200).
- S
Optional native standard errors; sparse plus S is refused.
- var_type
Native query residual variance setting. NULL (default) inherits the reference fit's recorded
var_type; views without one use 0L.- dense_budget_bytes
Budget for dense allocations in bytes (default 256 MiB).
Value
List with $fit (native), $representation (view with the query
samples and the exact source effects and offsets), $reconstruction
(in-sample fit_r2, not prediction), coverage and exclusions in the
settings, and $warnings.
Details
Uses the same native fixed-program projection as holdout evaluation. No automatic preprocessing or normalized usages. Sparse projection is retained when offsets are zero; necessary dense copies obey the declared budget.
Query-set dependence: with prior_policy = "query" activity priors are
re-estimated by empirical Bayes over the query rows. "fixed_reference"
instead fixes each activity prior to the reference fit's estimate
(L_ghat/F_ghat, raw native coordinates); it needs the native reference
fit and refuses views. Residual variance is still estimated on the query:
var_type = 0 (and the feature-wise setting) pools it over samples, so the
same cell projected within two different query sets can still receive
different activities. Only "fixed_reference" with sample-wise variance
(var_type = 1 for row samples, 2 for column samples) makes each sample's
activity independent of the other query samples. Row-chunked projection is therefore an approximation,
not an exact decomposition, and is not offered; oversize queries are refused
pre-flight against dense_budget_bytes (see
reports/FU3B_CP6_CHUNKING_DECISION.md).
See also
factor_holdout() for held-out evaluation, project_factor_features()
Author
Mikhael Manurung; native flashier authors Jason Willwerscheid, Peter Carbonetto, Wei Wang and Matthew Stephens. Conceptual STARCAT credit: Dylan Kotliar and Michelle Curtis; no source reuse.
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, scaling = "raw")
# Eight new samples on the same 16 features; effects stay fixed.
query <- tcrossprod(matrix(rnorm(16), 8, 2), view$effects) + matrix(rnorm(128, sd = 0.2), 8, 16)
dimnames(query) <- list(paste0("q", 1:8), colnames(X))
projected <- project_factor_activity(view, query, "rows", "simulated_centered_intensity",
preprocessing = list(scope = "identity"),
ebnm_fn = list(ebnm::ebnm_normal, ebnm::ebnm_point_laplace), maxiter = 20
)
projected$representation$activity
#> F1 F2
#> q1 2.19108128 -1.0909889
#> q2 0.70165368 0.0171441
#> q3 -0.34887892 1.9646601
#> q4 0.76141846 -1.0077338
#> q5 1.70095901 1.1622439
#> q6 1.13376263 -0.7024055
#> q7 0.81150274 -2.2519419
#> q8 -0.07980979 -1.3145972
projected$reconstruction
#> rmse mae fit_r2 correlation
#> 0.1693914 0.1349313 0.9898576 0.9951943
#> n_scored_entries
#> 128.0000000
# One factor, priors fixed to the reference, shared genes only, a stricter memory budget.
fixed <- project_factor_activity(fit, X[1:6, ], "rows", "simulated_centered_intensity",
preprocessing = list(scope = "identity"), factors = "F1", prior_policy = "fixed_reference",
feature_match = "intersection", min_coverage = 0.9, dense_budget_bytes = 1e7, maxiter = 50
)
fixed$representation$activity
#> F1
#> s1 -0.0008176591
#> s2 -0.3496369865
#> s3 0.8421445582
#> s4 1.2562158318
#> s5 -0.1359373705
#> s6 -0.8319080014