Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Wald test applies lfc_null and alt_hypothesis to each coefficient instead of to the contrast

Aperta
#483 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python

Direzione di ricerca

Start by reading wald_test() in glm.py and trace how the contrast, lfc_null, and alternative hypotheses are used to compute the statistic. The report includes a reproducible synthetic-data example comparing ~condition with ~0 + condition, plus a proposed parametrized test covering all five alternatives. Done means equivalent designs produce matching statistics and p-values for the same contrast.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Report

When lfc_null != 0 or alt_hypothesis is set, wald_test() in glm.py subtracts lfc_null from (and clips) every coefficient and only then applies the contrast vector:

stat = contrast @ np.fmax((lfc - lfc_null) / wald_se, 0)   # "greater"
wald_statistic = float(contrast @ (lfc - lfc_null) / wald_se)  # default

That is only right when the contrast has a single non-zero entry. As soon as it has two (a comparison between two non-reference levels, a design without intercept, or a user-supplied numeric contrast) the result tests a different hypothesis: for a contrast whose entries sum to zero, lfc_null cancels out entirely (contrast @ (lfc - lfc_null) = contrast @ lfc), and for the thresholded alternatives each coefficient is clipped separately, so the statistic can be large even when the contrasted LFC is below the threshold.

The same model written in two equivalent ways then gives different p-values. With the bundled synthetic data, contrast B vs A, lfc_null=0.5:

from pydeseq2.dds import DeseqDataSet
from pydeseq2.ds import DeseqStats
from pydeseq2.datasets import load_example_data

counts = load_example_data(modality="raw_counts", dataset="synthetic")
metadata = load_example_data(modality="metadata", dataset="synthetic")
for design in ["~condition", "~0 + condition"]:
    dds = DeseqDataSet(counts=counts, metadata=metadata, design=design, quiet=True)
    dds.deseq2()
    ds = DeseqStats(dds, contrast=["condition", "B", "A"], lfc_null=0.5, quiet=True)
    ds.summary()
    r = ds.results_df.loc["gene1"]
    print(design, ds.contrast_vector, r[["log2FoldChange", "lfcSE", "stat", "pvalue"]].round(4).tolist())
~condition      [0. 1.]  [0.6328, 0.2891, 0.4594, 0.646]
~0 + condition  [-1  1]  [0.6328, 0.2891, 2.1889, 0.0286]

Same estimate, same standard error, but the second statistic is 0.6328 / 0.2891 (null value dropped) instead of (0.6328 - 0.5) / 0.2891 = 0.459. Over the 10 genes, stat differs by up to 4.21 and pvalue by up to 0.89 between the two designs. With alt_hypothesis="greaterAbs" the ~0 + condition fit reports 3 genes at padj < 0.05 against 0 for ~condition. In DESeq2 (R) results(dds, contrast=..., lfcThreshold=0.5) gives identical numbers for both designs, because the threshold is applied to the contrast estimate.

The fix is to compute the scalar contrast @ lfc first and apply lfc_null and the alternative hypothesis to it, keeping the existing contrast standard error (sqrt(Hc' M Hc)). I have a branch with that change and a parametrized test (~condition vs ~0 + condition must agree for all five alternatives) that fails on main and passes with the change; I can open a PR if that is welcome. (A similar PR, #468, was closed in September without discussion; I am opening this issue first to check how you would like it handled.)

Versions

pydeseq2 0.5.4 (main 2ca3b74), pandas 3.0.6, numpy 2.5.4, scipy (current), Python 3.13.14, macOS 26.

Lingua principale
Python
Stelle
768
Fork
93
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di scverse/PyDESeq2

Tutte le issue di scverse/PyDESeq2

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.