R:用ddply分组测试

use*_*234 1 r plyr

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

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)
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我想计算每个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") ) )
Run Code Online (Sandbox Code Playgroud)

但是,我遇到输出问题:它的组织方式与正常的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
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*rsh 6

您可以添加名为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
Run Code Online (Sandbox Code Playgroud)

统计量是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
      ) 
Run Code Online (Sandbox Code Playgroud)

这使:

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