跨行应用两个样本比例检验

Apply two sample proportion test across rows

我正在尝试对数据框的所有行进行一系列双样本比例测试。这是前 3 行的示例,其中 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, ..2) out of c(..3, ..4)
X-squared = 11.22, df = 1, 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, ..2) out of c(..3, ..4)
X-squared = 130.66, df = 1, 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