相同的代码产生不同的结果(p.adjust)

时间:2014-08-22 08:44:58

标签: r statistics

我想使用R对Benjamini和Hochberg的FDR进行p调整。但是当我使用内置函数时,结果不是我所期望的(几乎完全相同的值)。但是,当我提取函数(稍作修改)并将其作为自己的函数时,它会给出不同的结果。我很好奇为什么会这样。

可以在http://pastebin.com/w2WRJD2s

找到完整的p值列表


代码:

BHAdjust = function(p) {
  lp = n = length(p)
  i = lp:1L
  o = order(p, decreasing = TRUE)
  ro = order(o)
  return(pmin(1, cummin(n/i * p[o]))[ro])
}

adjustedValues = data.frame(p,
                            p.adjust(p, "BH"),
                            BHAdjust(p))
head(adjustedValues, 25)


输出:

> head(adjustedValues, 25)
            p p.adjust.p...BH.. BHAdjust.p.
1          NA                NA          NA
2  0.74611808         0.9935715  0.86179530
3  0.67478588         0.9935715  0.82143053
4  0.02903144         0.9935715  0.05407105
5  0.91896732         0.9935715  0.95715994
6  0.99468266         0.9975246  0.99601960
7  0.84588305         0.9935715  0.91863392
8  0.45523400         0.9935715  0.65221025
9  0.02105568         0.9935715  0.03951256
10 0.97237337         0.9935715  0.98574978
11 0.75585835         0.9935715  0.86649987
12 0.98392280         0.9935715  0.98906222
13 0.51172583         0.9935715  0.69568566
14 0.68731809         0.9935715  0.82806060
15 0.65301206         0.9935715  0.80637908
16 0.59183087         0.9935715  0.75312367
17 0.34104743         0.9935715  0.52065642
18 0.34438992         0.9935715  0.52468404
19         NA                NA          NA
20 0.07132209         0.9935715  0.13023274
21 0.59003164         0.9935715  0.75283716
22 0.52619364         0.9935715  0.70506163
23 0.92279478         0.9935715  0.95883139
24         NA                NA          NA
25         NA                NA          NA

2 个答案:

答案 0 :(得分:1)

以下是处理NA s:

的准系统版本
BHAdjust <- function (p) { 
  p0 <- p
  if (all(nna <- !is.na(p))) 
    nna <- TRUE
  p <- p[nna]
  #note that n = length(p) in p.adjust gets evaluated when N is called the first time
  n <- lp <- length(p) 
  i <- lp:1L
  o <- order(p, decreasing = TRUE)
  ro <- order(o)
  p0[nna] <- pmin(1, cummin(n/i * p[o]))[ro]

  p0
}

all.equal(
  BHAdjust(adjustedValues[,1]),
  p.adjust(adjustedValues[,1], method="BH"))
#[1] TRUE

答案 1 :(得分:0)

尝试使用最后一个额外参数:

p.adjust(p, method = p.adjust.methods, n = length(p))

Library Adjust