首页 文章

相关矩阵与dplyr,tidyverse和扫帚 - P值矩阵

提问于
浏览
3

所有 . 我想使用 dplyr and/or broom packages and testing multiple variables at the same time 从相关矩阵中获取p值 . 我知道其他方法,但dplyr对我来说似乎更容易,更直观 . 此外,dplyr需要关联每个变量以获得特定的p值,这使得过程更容易和更快 .

我检查了其他链接,但它们不适用于这个问题(example 1example 2example 3)当我使用此代码时,会报告相关系数 . 但是,P值不是 .

agreg_base_tipo_a %>% 
  dplyr::select(S2.RT, BIS_total, IDATE, BAI, ASRS_total) %>% 
  do(as.data.frame(cor(., method="spearman", use="pairwise.complete.obs")))

请查看此可重现的代码:

set.seed(1164)

library(tidyverse)
ds <- data.frame(id=(1) ,a=rnorm(10,2,1), b=rnorm(10,3,2), c=rnorm(5,1,05))

ds %>% 
  select(a,b,c) %>% 
  do(as.data.frame(cor(., method="spearman", use="pairwise.complete.obs")))

1 回答

  • 3

    这个答案是基于akrun对此post的评论 . 通过使用 rcorr 函数,我们可以计算相关性和P值 . 要访问这些组件,请使用 ds_cor$rds_cor$P .

    set.seed(1164)
    
    library(tidyverse)
    library(Hmisc)
    
    ds <- data.frame(id=(1) ,a=rnorm(10,2,1), b=rnorm(10,3,2), c=rnorm(5,1,05))
    
    ds_cor <- ds %>%
      select(-id) %>% 
      as.matrix() %>%
      rcorr(type = "spearman")
    
    ds_cor
    #    a     b     c
    # a  1.00  0.28 -0.42
    # b  0.28  1.00 -0.25
    # c -0.42 -0.25  1.00
    # 
    # n= 10 
    # 
    # 
    # P
    #   a      b      c     
    # a        0.4250 0.2287
    # b 0.4250        0.4929
    # c 0.2287 0.4929
    

相关问题