Calculating adjusted p-values in Python

Question:

So, I’ve been spending some time looking for a way to get adjusted p-values (aka corrected p-values, q-values, FDR) in Python, but I haven’t really found anything. There’s the R function p.adjust, but I would like to stick to Python coding, if possible. Is there anything similar for Python?

If this is somehow a bad question, sorry in advance! I did search for answers first, but found none (except a Matlab version)… Any help is appreciated!

Asked By: erikfas

||

Answers:

You can try the module rpy2 that allows you to import R functions (b.t.w., a basic search returns How to implement R's p.adjust in Python).

Another possibility is to look at the maths an redo it yourself, because it is still relatively easy.

Apparently there is an ongoing implementation in scipy: http://statsmodels.sourceforge.net/ipdirective/_modules/scikits/statsmodels/sandbox/stats/multicomp.html . Maybe it is already usable.

Answered By: JulienD

According to the biostathandbook, the BH is easy to compute.

def fdr(p_vals):

    from scipy.stats import rankdata
    ranked_p_values = rankdata(p_vals)
    fdr = p_vals * len(p_vals) / ranked_p_values
    fdr[fdr > 1] = 1

    return fdr
Answered By: The Unfun Cat

You mentioned in your question q-values and no answer provided a link which addresses this. I believe this package (at least it seems so from the documentation) calculates q-values in python

https://puolival.github.io/multipy/

and also this one

https://github.com/nfusi/qvalue

Answered By: user3494047
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.