How to implement R's p.adjust in Python

后端 未结 7 1644
时光说笑
时光说笑 2020-12-12 22:39

I have a list of p-values and I would like to calculate the adjust p-values for multiple comparisons for the FDR. In R, I can use:

pval <- read.csv(\"my_f         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 23:35

    Old question, but here's a translation of the R FDR code in python (which is probably fairly inefficient):

    def FDR(x):
        """
        Assumes a list or numpy array x which contains p-values for multiple tests
        Copied from p.adjust function from R  
        """
        o = [i[0] for i in sorted(enumerate(x), key=lambda v:v[1],reverse=True)]
        ro = [i[0] for i in sorted(enumerate(o), key=lambda v:v[1])]
        q = sum([1.0/i for i in xrange(1,len(x)+1)])
        l = [q*len(x)/i*x[j] for i,j in zip(reversed(xrange(1,len(x)+1)),o)]
        l = [l[k] if l[k] < 1.0 else 1.0 for k in ro]
        return l
    

提交回复
热议问题