Compare two groups descriptively in matched factor and feature space
Source:R/metadata.R
compare_factor_groups.RdDifference in mean activity between two groups for each factor, and the same difference pushed through the effects to feature units. Contrast c(A,B) always means A minus B. Workflow adapted independently from ZemmourLib FlashierDGE. No p-values or automatic conversion to fold changes.
Usage
compare_factor_groups(
fit,
metadata,
group,
contrast,
id_col = "sample",
unit_col = NULL,
weights = NULL,
na_action = "fail"
)Arguments
- fit
Native fit or a view from
standardize_factors().- metadata
Data frame with one row per sample; sample IDs in
id_col.- group
Name of the
metadatacolumn holding group labels. Factor level order is ignored; levels only define the groups.- contrast
Two group labels, e.g.
c("treated", "control"): first minus second.- id_col
Name of the sample ID column (default
"sample").- unit_col
Optional name of the independent-unit column for counts.
- weights
Optional nonnegative numeric vector named by sample ID.
- na_action
"fail"(default) or"omit".
Value
List with $factor_contrast (one row per factor),
$feature_contrast (one row per feature), $counts and matching metadata.
Details
Origin: ADAPTED / none. Credits FlashierDGE group contrast workflow; use factor_provenance("compare_factor_groups") for audited sources and runtime versions. Input units and basis are retained; display/conditional results do not establish biological replication.
Author
David Zemmour. 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)
)
compare_factor_groups(view, metadata, "group", c("B", "A"), unit_col = "donor")$factor_contrast
#> factor contrast estimate
#> 1 F1 B - A -0.02279082
#> 2 F2 B - A -0.04918164
#> basis_id
#> 1 22d48d58127d044596755bf0cc173fe8af9021a477e4ec63a9c00130d5355ffb
#> 2 22d48d58127d044596755bf0cc173fe8af9021a477e4ec63a9c00130d5355ffb