r - Multiple binomial tests in one code -
i perform exact binomial test, binom
r package(binom.test
function), 4 different number of successes 4 different probability values.
i can singularly, know if can write code calculation in 1 command (e.g. lapply, loop).
x <- c(68, 69, 70, 75) #number of successes p <- c(1/365, 2/365, 3/365, 4/365) #probability of success n <- 265 #number of trials
the conf.level = 0.95
, alternative = "two.sided"
(as outcome can 1 or 0).
any suggestion?
i tried:
for (i in 1:4) { test[i] <- binom.test(x[i], n, p[i], alternative = "two.sided", conf.level = 0.95) }
but doesn't work.
use mapply
:
mapply(binom.test, x, p, n=n, alternative = "two.sided", conf.level = 0.95, simplify = false)
mapply calls function in first argument values in additional arguments , additional named parameters. (see mapply function)
if want 1 field of result, call mapply this:
mapply(function(x,p,n, ...){ binom.test(x, n, p, ...)$p.value }, x, p, n=n, alternative = "two.sided", conf.level = 0.95)
Comments
Post a Comment