2012-12-07 18 views
2

當我在by選項中使用binom.test時,我很困惑。R中的二項式檢驗使用by()on data.frame

它似乎不適用於某些數據框,但適用於我放在一起的一些虛擬數據。

調用mean()的作品,因爲它應該,但...

我的示例代碼如下。


##### this does not work... 

bug <- InsectSprays 
bug$outcome <- ifelse(bug$count > 4, 1, 2) 
bug$spray.n <- ifelse(bug$spray == "A", 1, 
       ifelse(bug$spray == "B", 2, 
       ifelse(bug$spray == "C", 3, 
       ifelse(bug$spray == "D", 4, 
       ifelse(bug$spray == "E", 5, 6))))) 

binom.test(table(bug$outcome), alternative="greater")    
by(bug, bug$spray.n, FUN = function(X) binom.test(table(X$outcome), 
    alternative="greater")) 
by(bug, bug$spray.n, FUN = function(X) mean(X$count)        

##### this works... 

##### generating example data 
##### this has three groups, each with a binomial indicator 
##### success is coded as 1, failure as a 0 

set.seed(271828) 
center <- gl(3,10) 
outcome <- rbinom(length(center), 1, .6777) 
id <- seq(1,length(center),1) 
dat <- as.data.frame(cbind(center,id,outcome)) 

##### have to recode success and failure to use table() 
##### !!!!! would like to avoid having to do this... 

dat$primary <- ifelse(dat$outcome == 1 , 1 , 2) 
dat$cent <- as.factor(dat$center) 

##### carrying out one sided binomial test for positive outcome 

binom.test(table(dat$primary), alternative = "greater") 

##### would like to carry out the same test by center... 

by(dat, dat$center, FUN = function(X) binom.test(table(X$primary), 
    alternative = "greater")) 
by(dat, dat$center, FUN = function(X) mean(X$outcome)) 
+1

,如果你嘗試你可能會看到一個問題 –

+0

在沒有警告或評論的情況下,無法在'rm(list = ls())中輸入。 –

+0

@DWin對不起,偷偷摸摸的時候,我沒有密切關注...... –

回答

1

你可以看到這個問題,如果你嘗試:

by(bug, bug$spray.n, FUN = function(X) table(X$outcome))

+0

你,先生,是一個搖滾明星... –

2

的原因,一些binom.test電話沒有工作是因爲一些團體都成功(或故障)。所以,你需要每個組中至少有兩個級別才能進行測試(總體感覺......)。


出於完整性:`通過(錯誤,錯誤$ spray.n,FUN =功能(X)表(X $結果))`:

  ##### this does work... 

      air <- airquality 
      air 

      air$outcome <- ifelse(air$Wind > 10, 1, 2) 


      binom.test(table(air$outcome), alternative="greater") 



      by(air, air$Month, FUN = function(X) mean(X$Wind)) 

      by(air, air$Month, FUN = function(X) table(X$outcome)) 

      by(air, air$Month, FUN = function(X) binom.test(table(X$outcome), alternative="greater")) 
+0

+1你的額外解釋使其對其他人更有用。 –