【发布时间】: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