Calculating adjusted p-values in Python
Asked Answered
H

5

33

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!

Holotype answered 7/8, 2014 at 14:31 Comment(0)
C
24

It is available in statsmodels.

http://statsmodels.sourceforge.net/devel/stats.html#multiple-tests-and-multiple-comparison-procedures

http://statsmodels.sourceforge.net/devel/generated/statsmodels.sandbox.stats.multicomp.multipletests.html

and some explanations, examples and Monte Carlo http://jpktd.blogspot.com/2013/04/multiple-testing-p-value-corrections-in.html

Coimbra answered 8/8, 2014 at 5:16 Comment(1)
In statsmodels 0.80, use import statsmodels.stats.multitest as multi; multi.multipletests(...).Kosaka
B
9

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
Bela answered 10/6, 2015 at 6:42 Comment(5)
It's resulting in a different adjusted p-values array than statsmodels.stats.multitest.multipletests with the method fdr_bhSwifter
Only minimally. I can give their version too and explain why on mondayBela
It would be very helpful! Thank youSwifter
I am deliviering my PhD today so I am busy, but this answer does the final (IMO unnecessary step): https://mcmap.net/q/453335/-how-to-implement-r-39-s-p-adjust-in-pythonBela
No problem! Thank you very much for the link and good luck with the PhD!Swifter
R
6

We also added FDR in SciPy (will be in the next release, SciPy 1.11).

https://scipy.github.io/devdocs/reference/generated/scipy.stats.false_discovery_control.html

from scipy import stats

ps = [0.0001, 0.0004, 0.0019, 0.0095, 0.0201, 0.0278, 0.0298, 0.0344,
      0.0459, 0.3240, 0.4262, 0.5719, 0.6528, 0.7590, 1.000]
stats.false_discovery_control(ps)

# array([0.0015    , 0.003     , 0.0095    , 0.035625  , 0.0603    ,
#        0.06385714, 0.06385714, 0.0645    , 0.0765    , 0.486     ,
#        0.58118182, 0.714875  , 0.75323077, 0.81321429, 1.        ])
Rationalism answered 4/5, 2023 at 10:6 Comment(0)
C
4

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 statsmodels: http://statsmodels.sourceforge.net/ipdirective/_modules/scikits/statsmodels/sandbox/stats/multicomp.html . Maybe it is already usable.

Carrigan answered 7/8, 2014 at 15:1 Comment(0)
H
0

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

Herrington answered 26/6, 2021 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.