Test continuous factor activities using an explicit inferential engine
Source:R/association.R
test_factor_association.RdRegress each factor's activity on a sample-level design and return one row per factor and term, with p-values. Conditional on the fitted activity basis. Independent engines reject repeated units. dream requires the declared unit in random effects. Native warnings and errors remain visible; failures are recorded. No count preprocessing or voom.
Usage
test_factor_association(
fit,
metadata,
formula,
engine,
unit_col,
independence,
id_col = "sample",
factors = NULL,
contrasts = NULL,
include_intercept = FALSE,
na_action = "fail",
adjust = "BH",
adjust_scope = "all_tests",
control = list(),
keep_models = TRUE,
moderate = FALSE
)Arguments
- fit
Native fit or a view from
standardize_factors()(never display percentiles).- metadata
Data frame with one row per sample, including the design columns, the sample ID and the biological unit.
- formula
One-sided design formula over
metadatacolumns, e.g.~ group + age.- engine
"lm"(stats),"limma"or"dream"(variancePartition), chosen explicitly; the last two need their packages installed. limma always applies empirical Bayes moderation; with few factors it reproduces lm (identical SE, df and p at K = 2) and needs K in the tens to borrow strength. Moderation assumes the factors' residual variances are on a similar scale, so uselmfor views in raw scale (a warning appears when they differ by more than tenfold). dream reports its own Satterthwaite t, df and p unlessmoderate = TRUE.- unit_col
Name of the biological unit column, e.g.
"donor". Required.- independence
"independent"(one sample per unit) or"repeated"(several samples per unit; needsengine = "dream"with the unit as a random effect). Required.- id_col
Name of the sample ID column (default
"sample").- factors
Optional character factor IDs to test. Default all.
- contrasts
Optional numeric matrix, rows named by design terms and columns by contrast; omitted terms have zero weight.
- include_intercept
Also test the intercept when no contrasts are supplied (default
FALSE).- na_action
"fail"(default) or"omit"rows with NA jointly.- adjust
Multiple-testing method name for
stats::p.adjust()(default"BH").- adjust_scope
"all_tests"(default) or"by_term".- control
Optional named list of engine settings: lm
joint_coefficient_covariance, limma/dreamebayes, and dream native settings (dropped-method changes are forbidden).- keep_models
Retain native model fits for diagnostics (default
FALSE).- moderate
engine = "dream"only: apply empirical Bayes moderation (variancePartition::eBayes()) after the fit (defaultFALSE). The moderated df pools residual df across factors (about 8000 in a simulation where the between-donor df is 8), which makes p-values for terms that vary between donors anti-conservative once there are three or more factors. limma always moderates andlmnever does, so the argument is ignored for them.
Value
List with $table (one row per factor and term: estimate,
std.error, statistic, df, p.value, adj.p.value), $models, $design, $contrasts, $diagnostics,
$coefficient_covariance where available, and $analysis_metadata
identifying conditional inference. With adjust_scope = "all_tests" the
Benjamini-Hochberg family is the non-intercept tests; intercept tests (from
include_intercept = TRUE) are adjusted within their own family.
adj.p.value is adjusted only within this call.
Details
Origin: NEW_ORCHESTRATION / call_only. Credits stats::lm; limma; dream; later brms; use factor_provenance("test_factor_association") for audited sources and runtime versions. Input units and basis are retained; display/conditional results do not establish biological replication.
See also
backproject_contrast() to express a term in feature units
Author
Mikhael Manurung; R Core Team (stats::lm); Gordon Smyth and limma contributors; Gabriel Hoffman (variancePartition::dream). 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)
# one sample per donor, so the sample ID can serve as the unit; name the donor column otherwise
metadata <- data.frame(
sample = rownames(X), donor = rownames(X),
group = rep(c("A", "B"), each = 12)
)
test_factor_association(view, metadata = metadata, formula = ~group, engine = "lm", unit_col = "donor", independence = "independent")$table
#> factor contrast term estimate std.error statistic df p.value
#> 1 F1 groupB groupB -0.02279082 0.08176642 -0.2787308 22 0.7830546
#> 2 F2 groupB groupB -0.04918164 0.08242195 -0.5967056 22 0.5567952
#> status reason adj.p.value engine n_samples n_units
#> 1 supported <NA> 0.7830546 lm 24 24
#> 2 supported <NA> 0.7830546 lm 24 24
#> basis_id
#> 1 afb45eb58b0030ea2f005b3e18b88f3e4c87cb3b9b9dd3b4ed5e4e620fbfd5b6
#> 2 afb45eb58b0030ea2f005b3e18b88f3e4c87cb3b9b9dd3b4ed5e4e620fbfd5b6
#> inference
#> 1 conditional_on_factor_fit
#> 2 conditional_on_factor_fit