【问题标题】:Apply two sample proportion test across rows跨行应用两个样本比例测试
【发布时间】:2020-12-28 04:28:41
【问题描述】:

我正在尝试对数据框的所有行进行一系列两样本比例测试。以下是前 3 行的示例,其中 x 是 yes 响应的数量,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 样本二项式检验的输出。我一定是错误地使用了地图。有什么想法吗?

【问题讨论】:

    标签: r purrr categorical-data


    【解决方案1】:

    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多