2014-02-24 44 views
0

我有一個很大的表,有幾千個值,我想用binom.test來計算p值。舉個例子:添加p值的列 - 速度效率

test <- data.frame("a" = c(4,8,8,4), "b" = c(2,3,8,0)) 

添加一個名爲"pval"我用第三列:

test$pval <- apply(test, 1, function(x) binom.test(x[2],x[1],p=0.05)$p.value) 

這對於一個小的測試樣品,如上述,當我嘗試使用此爲我的實際工作正常,但是數據集的速度太慢了。有什麼建議麼?

+0

你能給我們提供一些關於你的大數據集的更多信息嗎?例如,你是否期望'test'中有很多重複的行? – josliber

回答

3

如果您只是使用p值並始終使用雙面測試,那麼只需從現有的binom.test函數中提取該代碼的一部分。

simple.binom.test <- function(x, n) 
{ 
    p <- 0.5 
    relErr <- 1 + 1e-07 
    d <- dbinom(x, n, p) 
    m <- n * p 
    if (x == m) 1 else if (x < m) { 
    i <- seq.int(from = ceiling(m), to = n) 
    y <- sum(dbinom(i, n, p) <= d * relErr) 
    pbinom(x, n, p) + pbinom(n - y, n, p, lower.tail = FALSE) 
    } else { 
    i <- seq.int(from = 0, to = floor(m)) 
    y <- sum(dbinom(i, n, p) <= d * relErr) 
    pbinom(y - 1, n, p) + pbinom(x - 1, n, p, lower.tail = FALSE) 
    } 
} 

現在test that它給出了相同的值之前:

library(testthat) 
test_that(
    "simple.binom.test works", 
    { 
    #some test data 
    xn_pairs <- subset(
     expand.grid(x = 1:50, n = 1:50), 
     n >= x 
    ) 

    #test that simple.binom.test and binom.test give the same answer for each row. 
    with(
     xn_pairs, 
     invisible(
     mapply(
      function(x, n) 
      { 
      expect_equal(
       simple.binom.test(x, n), 
       binom.test(x, n)$p.value 
      ) 
      }, 
      x, 
      n 
     ) 
    ) 
    ) 
    } 
) 

現在看到它是多麼快:

xn_pairs <- subset(
    expand.grid(x = 1:50, n = 1:50), 
    n >= x 
)  
system.time(
    with(
    xn_pairs, 
    mapply(
     function(x, n) 
     { 
     binom.test(x, n)$p.value 
     }, 
     x, 
     n 
    ) 
) 
) 
## user system elapsed 
## 0.52 0.00 0.52 
system.time(
    with(
    xn_pairs, 
    mapply(
     function(x, n) 
     { 
     simple.binom.test(x, n) 
     }, 
     x, 
     n 
    ) 
) 
) 
## user system elapsed 
## 0.09 0.00 0.09 

五倍加快。