How to implement R's p.adjust in Python

后端 未结 7 1647
时光说笑
时光说笑 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:23

    Here is an in-house function I use:

    def correct_pvalues_for_multiple_testing(pvalues, correction_type = "Benjamini-Hochberg"):                
        """                                                                                                   
        consistent with R - print correct_pvalues_for_multiple_testing([0.0, 0.01, 0.029, 0.03, 0.031, 0.05, 0.069, 0.07, 0.071, 0.09, 0.1]) 
        """
        from numpy import array, empty                                                                        
        pvalues = array(pvalues) 
        n = float(pvalues.shape[0])                                                                           
        new_pvalues = empty(n)
        if correction_type == "Bonferroni":                                                                   
            new_pvalues = n * pvalues
        elif correction_type == "Bonferroni-Holm":                                                            
            values = [ (pvalue, i) for i, pvalue in enumerate(pvalues) ]                                      
            values.sort()
            for rank, vals in enumerate(values):                                                              
                pvalue, i = vals
                new_pvalues[i] = (n-rank) * pvalue                                                            
        elif correction_type == "Benjamini-Hochberg":                                                         
            values = [ (pvalue, i) for i, pvalue in enumerate(pvalues) ]                                      
            values.sort()
            values.reverse()                                                                                  
            new_values = []
            for i, vals in enumerate(values):                                                                 
                rank = n - i
                pvalue, index = vals                                                                          
                new_values.append((n/rank) * pvalue)                                                          
            for i in xrange(0, int(n)-1):  
                if new_values[i] < new_values[i+1]:                                                           
                    new_values[i+1] = new_values[i]                                                           
            for i, vals in enumerate(values):
                pvalue, index = vals
                new_pvalues[index] = new_values[i]                                                                                                                  
        return new_pvalues
    

提交回复
热议问题