问题描述
我正在尝试对数据帧的所有行进行一系列的两样本比例测试。这是前三行的示例,其中x是肯定响应的系数,而n是总数。
df <- data.frame("x1" = c(370,450,490),"x2" = c(150,970,120),"n1" = c(1500,2700,4500),"n2" = c(3000,4900,3200))
我正在使用“ prop.test”函数,该函数将两个比例进行如下比较:
test <- prop.test(x = c(370,150),n = c(1500,3000),correct = "FALSE")
我尝试过:
Map(prop.test,x = c(df$x1,df$x2),n = c(df$n1,df$n2),correct = "FALSE")
,但是它返回6行1样本二项式检验的输出,而不是3行2样本二项式检验的输出。我必须使用地图不正确。有什么想法吗?
解决方法
pmap
允许遍历输入data.frame的每一行。
尝试:
library(purrr)
purrr::pmap(df,~{prop.test(x = c(..1,..2),n = c(..3,..4),correct = "FALSE")})
或
purrr::pmap(df,~with(list(...),prop.test(x = c(x1,x2),n = c(n1,n2),correct = "FALSE"))
[[1]]
2-sample test for equality of proportions without continuity correction
data: c(..1,..2) out of c(..3,..4)
X-squared = 378.44,df = 1,p-value < 2.2e-16
alternative hypothesis: two.sided
95 percent confidence interval:
0.1734997 0.2198336
sample estimates:
prop 1 prop 2
0.2466667 0.0500000
[[2]]
2-sample test for equality of proportions without continuity correction
data: c(..1,..4)
X-squared = 11.22,p-value = 0.0008094
alternative hypothesis: two.sided
95 percent confidence interval:
-0.04923905 -0.01334598
sample estimates:
prop 1 prop 2
0.1666667 0.1979592
[[3]]
2-sample test for equality of proportions without continuity correction
data: c(..1,..4)
X-squared = 130.66,p-value < 2.2e-16
alternative hypothesis: two.sided
95 percent confidence interval:
0.06015674 0.08262104
sample estimates:
prop 1 prop 2
0.1088889 0.0375000