Skip to contents

A factor connects a pattern across samples to a pattern across features. This tutorial fits three factors to a bundled dataset, turns the fit into a view, ranks the features behind one factor and plots activity across samples. Start with a model fitted directly by flashier; no special fitting wrapper is required.

Terms used across the tutorials: a factor is one fitted column of the model; its activity is its value in each sample and its effects are its values in each feature. A program is a planted or biological gene set that a factor may or may not recover. GBCD output calls its factors GEPs.

Fit and extract

GTEx association z-scores are bundled with flashier. Rows are SNP–gene pairs; columns are tissues.

library(flashier.utils)
data("gtex", package = "flashier")
set.seed(1)
fit <- flashier::flash(gtex, greedy_Kmax = 3, backfit = TRUE, verbose = 0)
view <- standardize_factors(fit, sample_side = "columns",
                            feature_scale = "association_z_score")
activity <- factor_activity(view)  # samples × factors
effects <- factor_effects(view)    # features × the same factors
dim(activity)
## [1] 44  3
dim(effects)
## [1] 1000    3
round(activity[1:3, ], 2)
##                             F1   F2 F3
## Adipose_Subcutaneous     -0.23 0.18  0
## Adipose_Visceral_Omentum -0.17 0.09  0
## Adrenal_Gland            -0.13 0.00  0

standardize_factors() needs two declarations. sample_side says which matrix side holds samples; here tissues are columns. feature_scale is a free text label for what the values measure, such as "association_z_score" or "log1p_normalized_counts". It is recorded, not applied: preprocess before fitting, because nothing here transforms your data. Rows and columns must have unique names; an unnamed fit is rejected rather than given made-up IDs.

What a view holds

names(view)
## [1] "activity" "effects"  "offset"   "factors"  "manifest"
  • activity: samples × factors matrix. One row per sample.
  • effects: features × factors matrix with the same factor columns.
  • offset: per-feature constant added back when reconstructing. tcrossprod(activity, effects) is the fitted matrix in samples × features orientation, so it is the transpose of gtex here because samples are columns; Advanced use shows the sweep() call that adds the offset.
  • manifest: the recorded declarations and coordinate choice (scaling, feature_scale, sample_side, factor IDs), plus a basis_id hash so results from different coordinates are never mixed by accident.

The default scaling = "ldf" splits the fit into unit-norm factors with the scale placed on the effects side. "raw" keeps flashier’s own coordinates and "rms" gives each activity column unit root-mean-square. All three reconstruct the same matrix. A factor’s proportion of variance explained (PVE) comes from factor_pve(fit); the bar chart is the first thing to look at after a fit.

Bar chart of proportion of variance explained for factors F1 to F3; F1 carries most of it.

Proportion of variance explained per factor.

Interpret one factor

plot_factor_weights(view, factors = "F1", n_label = 5)
All F1 feature effects ranked by size, an S-shaped curve with the five largest SNP-gene pairs labelled at each end.

F1 feature effects, ranked. Labels mark the five largest in absolute value.

factor_features(view, factor = "F1", select = "largest", n = 5)[,
  c("feature", "estimate")]
##                                   feature  estimate
## 1   ENSG00000174652.13_19_9540622_G_C_b37 -96.74807
## 2   ENSG00000226752.3_9_123583192_A_G_b37  94.55262
## 3   ENSG00000020922.8_11_94205747_C_T_b37 -72.72866
## 4 ENSG00000183605.12_10_120920588_T_C_b37 -69.53569
## 5    ENSG00000168653.6_1_39496819_G_A_b37  64.22673

select = "largest" ranks features by effect size within the factor. select = "distinctive" instead ranks the margin to the strongest competing factor, which separates a program from its neighbours. Effects retain their sign. A factor’s sign can reverse on both sides without changing reconstruction, so a negative effect does not by itself mean repression.

The heatmap shows tissue activity in the declared coordinates. These tissues do not supply donor-level replication. For your own matrix, use metadata summaries before assigning biological labels.

Next tasks and credits

Advanced use explains units and uncertainty. Compare priors separates reconstruction from program recovery. Pancreas programs is a real-data case study.

Native FLASH: Jason Willwerscheid, Peter Carbonetto, Wei Wang and Matthew Stephens. EBNM: Jason Willwerscheid, Matthew Stephens and Peter Carbonetto. Feature ranking adapts singlecelljamboreeR helpers by Peter Carbonetto and Matthew Stephens; activity displays credit David Zemmour / Zemmour Lab workflows; the ranked weights and PVE displays follow the MOFA2 plotting workflow of Ricard Argelaguet, Damien Arnol, Danila Bredikhin and Britta Velten conceptually, with no source reused. Use citation("flashier"), citation("ebnm"), and factor_provenance("factor_features"). Full credits and licenses.