首页 文章

R:用ddply分组测试

提问于
浏览
1

我试图计算每个因子级别的数据框中两个数字列之间的相关性 . 这是一个示例数据框:

concentration <-(c(3, 8, 4, 7, 3, 1, 3, 3, 8, 6))
area <-c(0.5, 0.9, 0.3, 0.4, 0.5, 0.8, 0.9, 0.2, 0.7, 0.7)
area_type <-c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
data_frame <-data.frame(concentration, area, area_type)

在这个例子中,我想计算每个level_type级别的浓度和面积之间的相关性 . 我想使用cor.test而不是cor,因为我想要p值和kendall tau值 . 我尝试使用ddply来做到这一点:

ddply(data_frame, "area_type", summarise,
  corr=(cor.test(data_frame$area, data_frame$concentration,
                 alternative="two.sided", method="kendall") ) )

但是,我遇到输出问题:它的组织方式与正常的Kendall cor.test输出不同,后者表示z值,p值,备选假设和tau估计 . 而不是那样,我得到下面的输出 . 我不知道输出的每一行表示什么 . 此外,每个level_type级别的输出值都相同 .

area_type                                         corr
1          A                                    0.3766218
2          A                                         NULL
3          A                                    0.7064547
4          A                                    0.1001252
5          A                                            0
6          A                                    two.sided
7          A               Kendall's rank correlation tau
8          A data_frame$area and data_frame$concentration
9          B                                    0.3766218
10         B                                         NULL
11         B                                    0.7064547
12         B                                    0.1001252
13         B                                            0
14         B                                    two.sided
15         B               Kendall's rank correlation tau
16         B data_frame$area and data_frame$concentration

ddply我做错了什么?或者还有其他方法吗?谢谢 .

2 回答

  • 5

    您可以添加名为corr的其他列 . 此外,您的语法稍有不正确 . . 指定变量来自您指定的数据框 . 然后删除data_frame $,否则它将使用整个数据框:

    ddply(data_frame, .(area_type), summarise, corr=(cor.test(area, concentration, alternative="two.sided", method="kendall")), name=names(corr) )

    这使:

    area_type                           corr        name
    1          A                      -0.285133   statistic
    2          A                           NULL   parameter
    3          A                      0.7755423     p.value
    4          A                     -0.1259882    estimate
    5          A                              0  null.value
    6          A                      two.sided alternative
    7          A Kendall's rank correlation tau      method
    8          A         area and concentration   data.name
    9          B                              6   statistic
    10         B                           NULL   parameter
    11         B                      0.8166667     p.value
    12         B                            0.2    estimate
    13         B                              0  null.value
    14         B                      two.sided alternative
    15         B Kendall's rank correlation tau      method
    16         B         area and concentration   data.name
    

    统计量是z值,估计值是tau估计值 .

    编辑:你也可以像这样做只拉你想要的东西:

    corfun<-function(x, y) {
      corr=(cor.test(x, y,
                     alternative="two.sided", method="kendall"))
    }
    
    ddply(data_frame, .(area_type), summarise,z=corfun(area,concentration)$statistic,
          pval=corfun(area,concentration)$p.value,
          tau.est=corfun(area,concentration)$estimate,
          alt=corfun(area,concentration)$alternative
          )
    

    这使:

    area_type z pval tau.est alt 1 A -0.285133 0.7755423 -0.1259882 two.sided 2 B 6.000000 0.8166667 0.2000000 two.sided

  • 0

    这是不起作用的部分原因是cor.test返回:

    Pearson's product-moment correlation
    
    data:  data_frame$concentration and data_frame$area
    t = 0.5047, df = 8, p-value = 0.6274
    alternative hypothesis: true correlation is not equal to 0
    95 percent confidence interval:
    -0.5104148  0.7250936
    sample estimates:
      cor 
      0.1756652
    

    这些信息不能放入data.frame(ddply),不会使代码复杂化 . 如果您能提供所需的确切信息,我可以提供进一步的帮助 . 我会看看只是使用

    corrTest <- ddply(.data = data_frame, 
                     .variables = .(area_type), 
                     .fun = cor(concentration, area,))
                                    method="kendall")))
    

    我没有测试过这段代码,但这是我最初采用的路线,可以在这里工作 .

相关问题