`prior_LFC_var` is squared in the Wald test's ridge

Aperta Adatta ai principianti
#516 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #537 di @VamikaSinghal — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python

Direzione di ricerca

Il ridge viene costruito in ds.py:293 in DeseqStats come np.diag(1 / self.prior_LFC_var**2); leggi prima quel costruttore e la docstring di prior_LFC_var. Esegui la riproduzione dall'issue per confermare che DeseqStats.SE attualmente corrisponde al SE di Wald I / 16 (1.098596) invece di 0.841471 per prior_LFC_var=[4, 4]. Rimuovere il quadrato è la correzione minima; valuta anche se serve una correzione di scala con log(2)^2, poi esegui i test di Wald sotto tests/ per verificare che nient'altro cambi.

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

Descrizione

bug

DeseqStats(prior_LFC_var=...) uses the reciprocal of the square of each value as the Wald test's ridge: with prior_LFC_var=[4, 4] the lowest-count gene's standard error is 1.098596, the one with ridge I / 16, not 0.841471, the one with ridge I / 4; the docstring calls it the "Prior variance for LFCs, used for ridge regularization", and a normal prior with variance v has penalty 1 / v, as in DESeq2's fitGLMsWithPrior (lambda <- 1/betaPriorVar). ds.py:293 sets ridge_factor = np.diag(1 / self.prior_LFC_var**2); np.diag(1 / self.prior_LFC_var) would match the docstring, which could also give the scale (DESeq2's prior variance is on the log2 scale and fitNbinomGLMs divides lambda by log(2)^2, while this ridge applies to natural-log coefficients).

Source line numbers refer to commit 2ca3b74.

Reproduction
import numpy as np
import pandas as pd
from pydeseq2.dds import DeseqDataSet


def simulate(n_genes=200, groups=(4, 4), seed=0):
	"""Counts (samples x genes) and metadata; dispersion 0.05 + 1 / mean."""
	rng = np.random.default_rng(seed)
	group = np.repeat(np.arange(len(groups)), groups)
	base = rng.lognormal(4, 1.5, n_genes)
	lfc = rng.normal(0, 0.5, (len(groups), n_genes))
	lfc[0] = 0
	mu = base * np.exp(lfc[group])
	disp = 0.05 + 1 / base
	counts = rng.negative_binomial(1 / disp, 1 / (1 + disp * mu))
	samples = [f"s{i}" for i in range(len(group))]
	counts = pd.DataFrame(counts, index=samples, columns=[f"g{j}" for j in range(n_genes)])
	metadata = pd.DataFrame({"condition": np.array(list("ABCDEF"))[group]}, index=samples)
	return counts, metadata


def dataset(counts, metadata, **kwargs):
	return DeseqDataSet(counts=counts, metadata=metadata, n_cpus=1, quiet=True, **kwargs)


import numpy as np
from pydeseq2.ds import DeseqStats
from pydeseq2.glm import wald_test
dds = dataset(*simulate(n_genes=50, groups=(4, 4), seed=6))
dds.deseq2()
ds = DeseqStats(dds, contrast=["condition", "B", "A"], prior_LFC_var=np.array([4.0, 4.0]), n_cpus=1, quiet=True)
ds.run_wald_test()
gene = dds.var["_normed_means"].idxmin()
X = dds.obsm["design_matrix"].to_numpy()
lfc = dds.varm["LFC"].loc[gene].to_numpy()
mu = dds.obs["size_factors"].to_numpy() * np.exp(X @ lfc)
for v in [4.0, 16.0]:
	se = wald_test(X, dds.var.loc[gene, "dispersions"], lfc, mu, np.eye(2) / v, np.array([0.0, 1.0]), 0.0, None)[2]
	print(f"Wald SE with ridge I / {v:g}: {se:.6f}")
print(f"DeseqStats.SE with prior_LFC_var=[4, 4]: {ds.SE[gene]:.6f}")

Expected: DeseqStats.SE equal to the standard error with ridge I / 4.

Actual, with PyDESeq2 0.6.0:

Wald SE with ridge I / 4: 0.841471
Wald SE with ridge I / 16: 1.098596
DeseqStats.SE with prior_LFC_var=[4, 4]: 1.098596

Split out from #491.

Lingua principale
Python
Stelle
770
Fork
98
Merge medio
19h 55m
PR unite (30g)
45

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.