我对选项中binom.test
使用时的行为感到困惑。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))