Build a contrast analysis using one of several statistical methods
Source:R/build_contrast_analysis.R
build_contrast_analysis.RdThe factory of prolfqua's modelling: method selects a facade class
from the facade registry, and the facade runs the full pipeline from
strategy construction through modelling to contrast computation. Every
facade returns its results through the same interface
(get_contrasts(), get_Plotter(), get_missing(),
to_wide()), so the results of different methods have the same
columns and can be compared or combined.
Arguments
- lfqdata
LFQData object with the data and its configuration
- modelstr
one right-hand-side formula string without the response variable (e.g.
"~ group_"). The response is taken automatically fromlfqdata$get_config()$get_response().- contrasts
nonempty named character vector of contrast expressions (e.g.
c("A_vs_B" = "group_A - group_B"))- method
a registered facade key. The built-in keys are: linear models
"lm","rlm","rfit"; limma"limma","limma_voom","deqms","deqms_voom","limpa","limpa_nested"; limit-of-detection imputation"lm_impute","limma_impute","limma_voom_impute","rfit_impute"; peptide-level models"lmer_nested","ropeca_nested","ropecaV2_nested"; detection models"firth","binomial_nested","binomial_firth_nested"; the hurdle model"hurdle_nested", combining intensity and detection. Downstream packages may add more viaregister_facade. Method names are case sensitive and must match exactly, as must the factor and column names inmodelstrandcontrasts. The authoritative list isnames(list_facades()). Defaults to"lm".- ...
arguments of the method's facade constructor, e.g.
weights,trendandrobustfor the limma methods orcontrolfor the iterative fits; each facade's help page lists them (ContrastsLimmaFacade,ContrastsRLMFacade, ...). An argument the facade does not take is an error.
Value
a facade object of the class registered for method, see
lookup_facade.
Details
Methods ending in _nested take peptide- or precursor-level input and
report protein-level contrasts; the others take input at the level they
report, usually aggregated proteins. "limpa" needs input aggregated
with AggregateLimpa.
The formula is translated into a design matrix with R's current
contrast options (normally treatment coding). Configured predictors are
categorical labels; their reference level is the first level of a factor
column, or the first sorted label of a character column. Each contrast
expression combines group-level coefficient weights, rather than referring
directly to design-matrix coefficient names. For
example, "group_A - group_Ctrl" subtracts the control group mean
from the A group mean, including the reference coefficient as needed.
Interaction cell means are named with a colon and quoted with backticks
inside expressions. See the Under the hood article for worked
one-factor and ~ A * B design matrices and linear functions.
Internally, limpa_nested and hurdle_nested aggregate child
features to proteins. Detection methods (binomial_nested,
binomial_firth_nested, firth, and the detection component
of hurdle_nested) encode intensities as detected/not detected.
These aggregation and detection steps have no inverse. The analysis keeps
its input in analysis$.lfqdata; for aggregated input this is already
at the modelled hierarchy. fitted() and residuals() expose
the modelled observed responses (inspect each hurdle component separately).
Intensity effects are differences on the modelled scale, hence log2 fold
changes for log2 responses; detection effects are log odds ratios, with
fitted responses reported as probabilities. Transformations performed
before this call retain their input columns in LFQData; this call does not
record a transformation history or infer inverse transformations.
With a fixed design and fixed observations per protein, the per-protein
lm and lmer_nested fitting loops grow approximately linearly
with the number of proteins and run sequentially. limma fits the
expression matrix in one call and has lower per-protein overhead.
Single-run elapsed seconds for 500, 1000 and 2000 proteins were respectively
0.317, 0.429 and 0.911 for lm; 0.061, 0.057 and 0.059 for
limma; and 8.18, 15.76 and 31.50 for lmer_nested
(R 4.6.2, arm64 macOS, 12 samples, three peptides per protein for the
nested model, no missing responses). These timings cover facade
construction, including fitting, but exclude lazy contrast extraction.
They are illustrative timings, not guarantees: sample count, design width,
missingness and mixed-model convergence affect cost. Some mixed fits in
this run emitted convergence warnings. Reproduction code and environment
details are in srr/benchmark-runtime.R in the source repository.
Prior art
Improvements on existing R implementations, applied per protein behind one interface:
lm(stats),rlm(MASS),rfit(Rfit),limmaandlimma_voom(limma),deqms(DEqMS),limpaandlimpa_nested(limpa),lmer_nested(lme4, lmerTest),firth(logistf).Derivation of a published method:
ropeca_nestedfollows the peptide-median method of PECA and ROPECA (Suomi and Elo 2017, Bioconductor package PECA) without its ROTS step, on per-peptide linear models of any design;ContrastsROPECANestedFacadelists what differs and compares it with PECA.First implementations of new methods: the
_imputemethods (lm_impute,rfit_impute,limma_impute,limma_voom_impute), which refit groups without observations at the limit of detection with a borrowed variance (Wolski et al. 2023);ropecaV2_nested, a median peptide z-score with a correlation-aware null;binomial_nestedandbinomial_firth_nested, per-protein models of peptide detection.hurdle_nestedcombines the intensity and detection tests as msqrob2'shypothesisTestHurdledoes, on prolfqua's own components.
The Comparing methods article on the package website compares these implementations on one dataset. Comparisons with MSstats, msqrob2 and proDA are in the companion package prolfquabenchmark.
See also
Other modelling:
AnovaExtractor,
Contrasts,
ContrastsComparison,
ContrastsDEqMSFacade,
ContrastsDEqMSVoomFacade,
ContrastsFacadeBase,
ContrastsFirth,
ContrastsFirthFacade,
ContrastsHurdleFacade,
ContrastsHurdleNestedFacade,
ContrastsLMFacade,
ContrastsLMImputeFacade,
ContrastsLimma,
ContrastsLimmaFacade,
ContrastsLimmaImputeFacade,
ContrastsLimmaVoomFacade,
ContrastsLimmaVoomImputeFacade,
ContrastsLimpaFacade,
ContrastsLimpaNestedFacade,
ContrastsLmerNestedFacade,
ContrastsModerated,
ContrastsModeratedDEqMS,
ContrastsPlotter,
ContrastsRLMFacade,
ContrastsROPECA,
ContrastsROPECANestedFacade,
ContrastsROPECAV2,
ContrastsROPECAV2NestedFacade,
ContrastsRfitFacade,
ContrastsRfitImputeFacade,
ContrastsTable,
INTERNAL_FUNCTIONS_BY_FAMILY,
Model,
ModelFirth,
ModelLimma,
StrategyLM,
StrategyLimma,
StrategyLimpa,
StrategyLmer,
StrategyLogistf,
StrategyRLM,
StrategyRfit,
build_model(),
build_model_glm_protein(),
build_model_impute(),
build_model_limma(),
build_model_limma_impute(),
build_model_limma_voom(),
build_model_limma_voom_impute(),
build_model_limpa(),
compute_borrowed_variance(),
compute_borrowed_variance_limma(),
df.residual.rfit_prolfqua(),
group_label(),
impute_from_model(),
impute_refit_singular(),
list_facades(),
lookup_facade(),
lr_test(),
merge_contrasts_results(),
new_imputed_model(),
nobs.rfit_prolfqua(),
pivot_model_contrasts_to_wide(),
prolfqua_analysis_methods,
register_facade(),
sigma.rfit_prolfqua(),
strategy_limma(),
strategy_limpa(),
strategy_logistf(),
vcov.rfit_prolfqua()
Examples
istar <- sim_lfq_data_protein_config(n_proteins = 20)
#> `protein_Id`: columns `proteinID`, `idtype2` united with separator '~'.
#> creating sampleName from file_name column
#> setup_analysis: dropped 5 unconfigured columns: average_prot_abundance, sd, mean, N, Replicate.
#> setup_analysis: added 34 rows with NA response for feature and sample combinations absent from the input.
lfqdata <- LFQData$new(istar$data, istar$config)
lfqdata$rename_response("transformedIntensity")
contrasts <- c("A_vs_Ctrl" = "group_A - group_Ctrl")
fa_lm <- build_contrast_analysis(lfqdata, "~ group_", contrasts, method = "lm")
#> fit (Model): 34 missing responses dropped (e.g. 0m5WN4~1448, 9VUkAq~4562, At886V~3296, BEJI92~9143, CtOJ9t~2837, ...): NA response (missing observation) left out of the fit
#> See $provenance().
head(fa_lm$get_contrasts())
#> # A tibble: 6 × 16
#> modelName estimate_type protein_Id contrast diff avgAbd
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 lm observed 0EfVhX~5954 A_vs_Ctrl 2.72 23.2
#> 2 lm observed 0m5WN4~1448 A_vs_Ctrl 0.600 17.4
#> 3 lm observed 7cbcrd~8305 A_vs_Ctrl 2.59 27.0
#> 4 lm observed 9VUkAq~4562 A_vs_Ctrl 0.0679 19.4
#> 5 lm observed At886V~3296 A_vs_Ctrl -1.01 19.1
#> 6 lm observed BEJI92~9143 A_vs_Ctrl -0.873 20.9
#> # ℹ 10 more variables: std.error.unmoderated <dbl>, df.unmoderated <int>,
#> # std.error <dbl>, statistic <dbl>, df <dbl>, p.value <dbl>, conf.low <dbl>,
#> # conf.high <dbl>, sigma <dbl>, FDR <dbl>
fa_limma <- build_contrast_analysis(lfqdata, "~ group_", contrasts, method = "limma")
#> fit (limma): 34 missing responses dropped (e.g. 0m5WN4~1448, 9VUkAq~4562, At886V~3296, BEJI92~9143, CtOJ9t~2837, ...): NA response (missing observation) left out of the fit
#> See $provenance().
head(fa_limma$get_contrasts())
#> # A tibble: 6 × 16
#> modelName estimate_type protein_Id contrast diff FDR
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 limma observed 0EfVhX~5954 A_vs_Ctrl 2.72 0.188
#> 2 limma observed 0m5WN4~1448 A_vs_Ctrl 0.600 0.623
#> 3 limma observed 7cbcrd~8305 A_vs_Ctrl 2.59 0.0271
#> 4 limma observed 9VUkAq~4562 A_vs_Ctrl 0.0679 0.967
#> 5 limma observed At886V~3296 A_vs_Ctrl -1.01 0.623
#> 6 limma observed BEJI92~9143 A_vs_Ctrl -0.873 0.623
#> # ℹ 10 more variables: std.error.unmoderated <dbl>, df.unmoderated <dbl>,
#> # std.error <dbl>, statistic <dbl>, p.value <dbl>, sigma <dbl>, df <dbl>,
#> # conf.low <dbl>, conf.high <dbl>, avgAbd <dbl>
fa_deqms <- build_contrast_analysis(lfqdata, "~ group_", contrasts, method = "deqms")
#> fit (Model): 34 missing responses dropped (e.g. 0m5WN4~1448, 9VUkAq~4562, At886V~3296, BEJI92~9143, CtOJ9t~2837, ...): NA response (missing observation) left out of the fit
#> See $provenance().
head(fa_deqms$get_contrasts())
#> # A tibble: 6 × 16
#> modelName estimate_type contrast protein_Id diff avgAbd
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 deqms observed A_vs_Ctrl 0EfVhX~5954 2.72 23.2
#> 2 deqms observed A_vs_Ctrl 0m5WN4~1448 0.600 17.4
#> 3 deqms observed A_vs_Ctrl 7cbcrd~8305 2.59 27.0
#> 4 deqms observed A_vs_Ctrl 9VUkAq~4562 0.0679 19.4
#> 5 deqms observed A_vs_Ctrl At886V~3296 -1.01 19.1
#> 6 deqms observed A_vs_Ctrl BEJI92~9143 -0.873 20.9
#> # ℹ 10 more variables: std.error.unmoderated <dbl>, df.unmoderated <int>,
#> # std.error <dbl>, statistic <dbl>, df <dbl>, p.value <dbl>, conf.low <dbl>,
#> # conf.high <dbl>, sigma <dbl>, FDR <dbl>
istar_pep <- sim_lfq_data_peptide_config()
#> `protein_Id`: columns `proteinID`, `idtype2` united with separator '~'.
#> creating sampleName from file_name column
#> setup_analysis: dropped 6 unconfigured columns: average_prot_abundance, sd, mean, N, avg_peptide_abd, Replicate.
#> setup_analysis: added 36 rows with NA response for feature and sample combinations absent from the input.
lfqdata_pep <- LFQData$new(istar_pep$data, istar_pep$config)
lfqdata_pep <- lfqdata_pep$get_Transformer()$log2()$lfq
#> Transformed response column added: log2_abundance
fa_lmer <- build_contrast_analysis(
lfqdata_pep,
"~ group_ + (1 | peptide_Id) + (1 | sampleName)",
contrasts,
method = "lmer_nested"
)
#> boundary (singular) fit: see help('isSingular')
#> boundary (singular) fit: see help('isSingular')
#> boundary (singular) fit: see help('isSingular')
#> boundary (singular) fit: see help('isSingular')
#> boundary (singular) fit: see help('isSingular')
#> Warning: fit (Model): 4 fits failed (e.g. 7cbcrd~5725, 9VUkAq~4703, CGzoYe~2147, DoWup2~5896): Error: grouping factors must have > 1 sampled level
#> See $provenance().
#> fit (Model): 5 singular fits (e.g. 0EfVhX~0087, Fl4JiV~8625, HvIpHG~9079, JcKVfU~9653, SGIVBl~5782): coefficients not estimable or fewer than 2 residual df
#> fit (Model): 32 missing responses dropped (e.g. 0EfVhX~0087, BEJI92~5282, Fl4JiV~8625, HvIpHG~9079, SGIVBl~5782): NA response (missing observation) left out of the fit
#> See $provenance().
head(fa_lmer$get_contrasts())
#> # A tibble: 6 × 16
#> modelName estimate_type protein_Id contrast diff avgAbd
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 lmer_nested observed 0EfVhX~0087 A_vs_Ctrl -0.000832 4.34
#> 2 lmer_nested observed BEJI92~5282 A_vs_Ctrl 0.322 4.22
#> 3 lmer_nested observed Fl4JiV~8625 A_vs_Ctrl -0.0413 4.38
#> 4 lmer_nested observed HvIpHG~9079 A_vs_Ctrl -0.372 4.40
#> 5 lmer_nested observed JcKVfU~9653 A_vs_Ctrl -0.107 5.05
#> 6 lmer_nested observed SGIVBl~5782 A_vs_Ctrl 0.0307 4.68
#> # ℹ 10 more variables: std.error.unmoderated <dbl>, df.unmoderated <dbl>,
#> # std.error <dbl>, statistic <dbl>, df <dbl>, p.value <dbl>, conf.low <dbl>,
#> # conf.high <dbl>, sigma <dbl>, FDR <dbl>
fa_ropeca <- build_contrast_analysis(lfqdata_pep, "~ group_", contrasts, method = "ropeca_nested")
#> fit (Model): 36 missing responses dropped (e.g. 0EfVhX~0087~ITLb4x1q, 0EfVhX~0087~ahQLlQY7, 0EfVhX~0087~dJkdz7so, 7cbcrd~5725~D5dQ4nKk, 9VUkAq~4703~eIC06D7g, ...): NA response (missing observation) left out of the fit
#> See $provenance().
head(fa_ropeca$get_contrasts())
#> # A tibble: 6 × 14
#> modelName estimate_type protein_Id contrast avgAbd diff FDR statistic
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 ropeca_nes… observed 0EfVhX~00… A_vs_Ct… 4.27 -0.0742 5.28e-2 -1.75
#> 2 ropeca_nes… observed 7cbcrd~57… A_vs_Ct… 4.51 0.741 9.91e-5 8.79
#> 3 ropeca_nes… observed 9VUkAq~47… A_vs_Ct… 4.47 -0.598 6.91e-6 -12.7
#> 4 ropeca_nes… observed BEJI92~52… A_vs_Ct… 4.23 0.277 1.87e-3 3.94
#> 5 ropeca_nes… observed CGzoYe~21… A_vs_Ct… 4.76 -0.310 3.74e-5 -9.26
#> 6 ropeca_nes… observed DoWup2~58… A_vs_Ct… 4.43 0.295 1.38e-6 14.7
#> # ℹ 6 more variables: std.error <dbl>, df <int>, p.value <dbl>, conf.low <dbl>,
#> # conf.high <dbl>, sigma <dbl>
fa_firth <- build_contrast_analysis(lfqdata, "~ group_", contrasts, method = "firth")
head(fa_firth$get_contrasts())
#> # A tibble: 6 × 16
#> modelName estimate_type protein_Id contrast sigma df diff FDR
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 firth observed 0EfVhX~5954 A_vs_Ctrl 1 Inf 4.27e-16 1
#> 2 firth observed 0m5WN4~1448 A_vs_Ctrl 1 Inf -2.20e+ 0 0.878
#> 3 firth observed 7cbcrd~8305 A_vs_Ctrl 1 Inf 4.27e-16 1
#> 4 firth observed 9VUkAq~4562 A_vs_Ctrl 1 Inf -1.35e+ 0 1
#> 5 firth observed At886V~3296 A_vs_Ctrl 1 Inf 5.58e-16 1
#> 6 firth observed BEJI92~9143 A_vs_Ctrl 1 Inf -1.35e+ 0 1
#> # ℹ 8 more variables: std.error <dbl>, statistic <dbl>, p.value <dbl>,
#> # conf.low <dbl>, conf.high <dbl>, avgAbd <dbl>, std.error.unmoderated <dbl>,
#> # df.unmoderated <dbl>