2012-12-07 2 views
2

by 옵션에서 사용할 경우 binom.test의 동작이 혼동 스럽습니다.R의 이항 테스트 (data.frame에서 by()를 사용)

일부 데이터 프레임에서는 작동하지 않는 것처럼 보이지만 함께 사용하는 더미 데이터로 작동합니다. 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 통화의 일부가 작동하지 않은 이유는 (또는 실패). 따라서, 테스트를하기 위해서는 모든 그룹에서 최소한 2 개의 레벨이 필요합니다 (총체적으로 말이됩니다 ...). `(버그, 버그 $의 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 귀하의 추가 설명은 다른 사람들에게 더 유용합니다. –