forked from allenzhuaz/SIMHT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cFDR_adjP.R
52 lines (47 loc) · 1.9 KB
/
cFDR_adjP.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' @param pval The structural p-values, the type is a list.
#' @param t The thresholds determine the families are selected or not, also affects conditional p-value within families
#' @param method p-value combining methods
cFDR.cp.adjust <- function(pval, t, method=c("Fisher", "Stouffer", "minP")){
method <- match.arg(method)
if (length(t)<length(pval)){t <- rep(t,len=length(pval))}
condPval <- vector("list", length(pval))
adjcondP <- vector("list", length(pval))
if (method=="Fisher"){
for (i in which(lapply(pval,prod)<=exp(-t/2)) ){
condPval[[i]] <- integer(length(pval[[i]]))
for (j in 1:length(pval[[i]])){
if (length(pval[[i]])==1){condPval[[i]][j] <- pval[[i]][j]/exp(-t[i]/2)}
else {
condPval[[i]][j] <- pval[[i]][j]/min(c(exp(-t[i]/2)/prod(pval[[i]][-j]),1))
}
}
adjcondP[[i]] <- p.adjust(condPval[[i]], method = "BH")
}
}
if (method=="Stouffer"){
stfun <- function(x){sum(qnorm(1-x))/sqrt(length(x))}
for (i in which(sapply(pval,stfun)>=t) ){
condPval[[i]] <- integer(length(pval[[i]]))
for (j in 1:length(pval[[i]])){
if (length(pval[[i]])==1){condPval[[i]][j] <- pval[[i]][j]/(1-pnorm(t[i]))}
else {
condPval[[i]][j] <- pval[[i]][j]/(1-pnorm(sqrt(length(pval[[i]]))*t[i]-sum(qnorm(1-pval[[i]][-j]))))
}
}
adjcondP[[i]] <- p.adjust(condPval[[i]], method = "BH")
}
}
else if (method=="minP"){
for (i in which(sapply(pval,min)<=t) ){
condPval[[i]] <- integer(length(pval[[i]]))
for (j in 1:length(pval[[i]])){
if (length(pval[[i]])==1){condPval[[i]][j] <- pval[[i]][j]/t[i]}
else {
condPval[[i]][j] <- pval[[i]][j]/ifelse(min(pval[[i]][-j]) > t[i], t[i] ,1)
}
}
adjcondP[[i]] <- p.adjust(condPval[[i]], method = "BH")
}
}
return(adjcondP)
}