Correlation matrix with dplyr, tidyverse and broom - P-value matrix

一个人想着一个人 提交于 2019-12-10 10:09:12

问题


all. I want to obtain the p-value from a correlation matrix using dplyr and/or broom packages and testing multiple variables at the same time. I'm aware of other methods, but dplyr seems easier and more intuitive for me. In addition, dplyr will need to correlate each variable to obtain the specific p-value, what makes the process easier and faster.

I checked other links, but they did not work for this question (example 1, example 2, example 3) When I use this code, the correlation coefficients are reported. However, the P-values are not.

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")))

Please, check out this reproducible code to word:

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:


This answer is based on akrun's comment from this post. By using the rcorr function, we can calculate the correlations and P values. To access these components, use ds_cor$r and ds_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       


来源:https://stackoverflow.com/questions/50458635/correlation-matrix-with-dplyr-tidyverse-and-broom-p-value-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!