-1
我tyring計算p-value
dbinom()
的每一行或R Dataframe
如何爲R數據框的每一行運行二項式測試?
數據=
small Sum
2 7
3 6
5 11
對於每一行,我可以這樣做:
> binom.test(2, 7, 0.5, alternative=c("two.sided"), conf.level = 0.95)
Exact binomial test
data: 2 and 7
number of successes = 2, number of trials = 7, p-value = 0.4531
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.03669257 0.70957914
sample estimates:
probability of success
0.2857143
但是,我沒有成功將其應用於所有行。
喜歡的東西:
counts$pVal <- 2*sum(dbinom(0:counts$small, counts$Sum, 0.5))
#or,
counts_2ms04h$pVal <- binom.test(0:counts$small, counts$Sum, 0.5, alternative=c("two.sided"), conf.level = 0.99)
## I also used tapply
test <- function(x, n, p){binom.test(x, n, p, alternative="two-sided")}
mapply(test, counts$small, counts$Sum, 0.5)
Error in binom.test(x, n, p, alternative = "two-sided") :
'n' must be a positive integer >= 'x'
感謝,