【问题标题】:How to apply p.adjust to each row in matrix in R?如何将 p.adjust 应用于 R 矩阵中的每一行?
【发布时间】:2021-07-22 13:07:29
【问题描述】:

我有一个 100 x 10 000 的矩阵,其 p 值 (pval) 对应于 100 个假设的 10 000 次重复。 现在我想使用函数p.adjust 对矩阵中的每一行应用 bonferroni 校正。

我的代码运行了,但与原始 p 值矩阵相比,p 值没有变化,与预期的 0.05 水平相比,FWER 仍约为 0.994。

早些时候我尝试改用apply,但意识到我希望调整每一行,sapply 应该更合适。

这是我的代码:

#we simulate n random variables and test the null hypothesis that all
#means are simultaneously 0. (case where all nulls are true.)
n <- 100
pval <- matrix(NA, nrow = 1e4, ncol = n) #making matrix for data
for(k in 1 : 1e4){
  X <- replicate(n=n, expr = rnorm(100)) #replicate makes a matrix with n columns and 100 rows of norm(1,0)
  pval[k, ] <-  apply(X=X, MARGIN = 2, FUN = function(x){ #apply applies a function on our X matrix. MARGIN = 2 means the function is applied on the columns.
    t.test(x = x, mu = 0)$p.value #the function being applied is t.test. (t test is taken on all columns in the X matrix (100 rows))
  }) #this returns a matrix with n rows, 10000 columns where each column represents a p-value.
} #the data is uncorrelated. all zero - hypotheses are true.

#now we apply the Bonferroni correction:
padjBonf <- matrix(NA, nrow = 1e4, ncol = n) #making matrix for adjusted p-vals
for(k in 1 : 1e4){
  padjBonf[k,] <- sapply(X = pval[k,], FUN = function(x){ #sapply applies a function on our X vector. MARGIN = 2 means the function is applied on the columns.
    p.adjust(x, method = "bonferroni") 
  }) 
} 

【问题讨论】:

    标签: r statistics


    【解决方案1】:

    像这样使用 sapply,您在 单个值上运行 p.adjust

    注意以下几点:

    p.adjust( 0.05, method="bonferroni" ) # returns 0.05, unchanged!
    

    这就是你正在经历的。

    您可能打算给 p.adjust all 每个实验的 p 值,因此您的 p.val 的整行,如下所示:

    padjBonf[k,] = p.adjust( p.val[k,], method="bonferroni" )
    

    这应该适当地返回所有 1。

    或者您可以继续更正每个 p 值并告诉它 n=100 如手册中所述,但实际上没有必要,并且 p.adjust 是在考虑上述用法的情况下编写的。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 2011-01-19
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2019-09-04
      相关资源
      最近更新 更多