使用列作为参数将 binom.test 应用于每一行?
Apply binom.test to every row using columns as arguments?
我意识到以前有人问过这种问题,但我不明白为什么我的代码出错了。
我已经尝试过单独使用 mapply
和使用 do.call
以及 purrr
包的 pmap
功能。我不断收到 "unused argument" 等错误。由于所有 3 个都失败了,我想我一定是在参数中错误地引用了我的数据。我使用 plyr
包中的 mdply
来做这样的事情,但那是一年多以前的事了。当然,任何替代方法也将不胜感激。
要创建数据框,compar
:
obs = floor(runif(500, 1,99))
p = round(runif(500,0,1), digits = 4)
n = floor(runif(500, 100,150))
test = rep("two.sided", 500)
conf = rep(0.95, 500)
compar = as.data.frame(cbind(obs,n, p))
compar$test = test
compar$conf = conf
head(compar, 3)
obs p n test conf
1 47 0.2432 133 two.sided 0.95
2 52 0.3391 118 two.sided 0.95
3 22 0.2790 115 two.sided 0.95
我试试pmap
:
pmap(.l = compar, .f = binom.test)
Error in .f(obs = .l[[c(1L, i)]], p = .l[[c(2L, i)]], n = .l[[c(3L, i)]], :
unused arguments (obs = .l[[c(1, i)]], test = .l[[c(4, i)]])
接下来,mapply
:
mapply(compar, FUN = binom.test)
Error in (function (x, n, p = 0.5, alternative = c("two.sided", "less", :
incorrect length of 'x'
最后,do.call
和mapply
do.call(mapply, c(binom.test, compar[c("obs", "n", "p", "test", "conf")]))
Error in (function (x, n, p = 0.5, alternative = c("two.sided", "less", :
unused arguments (obs = dots[[1]][[1]], test = dots[[4]][[1]])
列名与 binom.test
个参数不匹配;对于 pmap
版本,根据 binom.test
参数重命名列应该有效:
pmap(select(compar, x=obs, n, p, alternative=test, conf), binom.test)
#[[1]]
# Exact binomial test
#data: .l[[c(1L, i)]] and .l[[c(2L, i)]]
#number of successes = 5, number of trials = 149, p-value < 2.2e-16
#alternative hypothesis: true probability of success is not equal to 0.435
#95 percent confidence interval:
# 0.01098400 0.07657136
#sample estimates:
#probability of success
# 0.03355705
#[[2]]
# Exact binomial test
#data: .l[[c(1L, i)]] and .l[[c(2L, i)]]
#number of successes = 20, number of trials = 113, p-value = 1.391e-10
#alternative hypothesis: true probability of success is not equal to 0.4681
#95 percent confidence interval:
# 0.1115928 0.2600272
#sample estimates:
#probability of success
# 0.1769912
# more output
或者:pmap(rename(compar, x=obs, alternative=test), binom.test)
我意识到以前有人问过这种问题,但我不明白为什么我的代码出错了。
我已经尝试过单独使用 mapply
和使用 do.call
以及 purrr
包的 pmap
功能。我不断收到 "unused argument" 等错误。由于所有 3 个都失败了,我想我一定是在参数中错误地引用了我的数据。我使用 plyr
包中的 mdply
来做这样的事情,但那是一年多以前的事了。当然,任何替代方法也将不胜感激。
要创建数据框,compar
:
obs = floor(runif(500, 1,99))
p = round(runif(500,0,1), digits = 4)
n = floor(runif(500, 100,150))
test = rep("two.sided", 500)
conf = rep(0.95, 500)
compar = as.data.frame(cbind(obs,n, p))
compar$test = test
compar$conf = conf
head(compar, 3)
obs p n test conf
1 47 0.2432 133 two.sided 0.95
2 52 0.3391 118 two.sided 0.95
3 22 0.2790 115 two.sided 0.95
我试试pmap
:
pmap(.l = compar, .f = binom.test)
Error in .f(obs = .l[[c(1L, i)]], p = .l[[c(2L, i)]], n = .l[[c(3L, i)]], :
unused arguments (obs = .l[[c(1, i)]], test = .l[[c(4, i)]])
接下来,mapply
:
mapply(compar, FUN = binom.test)
Error in (function (x, n, p = 0.5, alternative = c("two.sided", "less", :
incorrect length of 'x'
最后,do.call
和mapply
do.call(mapply, c(binom.test, compar[c("obs", "n", "p", "test", "conf")]))
Error in (function (x, n, p = 0.5, alternative = c("two.sided", "less", :
unused arguments (obs = dots[[1]][[1]], test = dots[[4]][[1]])
列名与 binom.test
个参数不匹配;对于 pmap
版本,根据 binom.test
参数重命名列应该有效:
pmap(select(compar, x=obs, n, p, alternative=test, conf), binom.test)
#[[1]]
# Exact binomial test
#data: .l[[c(1L, i)]] and .l[[c(2L, i)]]
#number of successes = 5, number of trials = 149, p-value < 2.2e-16
#alternative hypothesis: true probability of success is not equal to 0.435
#95 percent confidence interval:
# 0.01098400 0.07657136
#sample estimates:
#probability of success
# 0.03355705
#[[2]]
# Exact binomial test
#data: .l[[c(1L, i)]] and .l[[c(2L, i)]]
#number of successes = 20, number of trials = 113, p-value = 1.391e-10
#alternative hypothesis: true probability of success is not equal to 0.4681
#95 percent confidence interval:
# 0.1115928 0.2600272
#sample estimates:
#probability of success
# 0.1769912
# more output
或者:pmap(rename(compar, x=obs, alternative=test), binom.test)