简体   繁体   English

如何仅从 R 中的特定列对中提取显着相关性?

[英]How to extract only significant correlations from specific column pairs in R?

I need to calculate the correlation of some specific variables (columns).我需要计算一些特定变量(列)的相关性。

To calculate the correlation of specific columns I get through this code:要计算特定列的相关性,我通过以下代码获得:

df<-read.csv("http://renatabrandt.github.io/EBC2015/data/varechem.csv", row.names=1)
    
cor_df<-(cor(df, method="spearman")[1:6, 7:14])%>%as.data.frame()

output output

在此处输入图像描述

However I would like R to create a new matrix but only with the correlations with a level of significance, whose p-value <0.05, only for the set [1:6, 7:14] , that is to say exclude those not significant (p-value >0.05)但是我希望 R 创建一个新矩阵,但仅具有具有显着性水平的相关性,其 p 值 <0.05,仅适用于集合[1:6, 7:14] ,也就是说排除那些不显着的(p 值 >0.05)

I expect the non-significant ones to be deleted, or filled in with NA, or a new data.frame with just the signifiers.我希望删除不重要的那些,或者用 NA 填充,或者只包含能指的新 data.frame。

my expectavie is:我的期望是:

在此处输入图像描述

Please find below one possible solution using Hmisc , corrplot and dplyr libraries请在下面使用Hmisccorrplotdplyr库找到一种可能的解决方案

Reprex代表

  • Computes the correlation coefficients and corresponding pvalues using the rcorr() function of the Hmisc library使用Hmisc库的rcorr() function 计算相关系数和相应的 pvalue
library(Hmisc)
library(corrplot)
library(dplyr)


coeffs <- rcorr(as.matrix(df), type="spearman")[[1]][1:6, 7:14]
coeffs
#>              Al         Fe          Mn          Zn           Mo   Baresoil
#> N  -0.151805133 -0.1295934 -0.01261144 -0.07526648  0.004643575 0.15481627
#> P  -0.001739509 -0.1200000  0.60782609  0.73423234  0.035371924 0.03043478
#> K   0.006089604 -0.1156773  0.67579910  0.74244074 -0.039359822 0.18264841
#> Ca -0.289628187 -0.3982609  0.63130435  0.68638545 -0.175533171 0.27739130
#> Mg -0.187866932 -0.2382609  0.57043478  0.60069601 -0.118938093 0.29739130
#> S   0.320574163  0.1117634  0.51402480  0.77789865  0.334337367 0.07784301
#>     Humdepth          pH
#> N  0.1307120 -0.07186484
#> P  0.2102302 -0.12114884
#> K  0.2963972 -0.31001388
#> Ca 0.4396914 -0.25114066
#> Mg 0.4912655 -0.33161178
#> S  0.1698382 -0.21448892



pvalues <- rcorr(as.matrix(df), type="spearman")[[3]][1:6, 7:14]
pvalues
#>           Al         Fe           Mn           Zn        Mo  Baresoil
#> N  0.4788771 0.54615126 0.9533606683 7.266830e-01 0.9828194 0.4700940
#> P  0.9935636 0.57648987 0.0016290786 4.418653e-05 0.8696630 0.8877339
#> K  0.9774704 0.59039698 0.0002896520 3.264276e-05 0.8551122 0.3929703
#> Ca 0.1698232 0.05391473 0.0009388912 2.126270e-04 0.4119734 0.1894124
#> Mg 0.3793530 0.26221751 0.0036070461 1.909894e-03 0.5798929 0.1581543
#> S  0.1266908 0.60311127 0.0101838168 7.669395e-06 0.1103062 0.7176938
#>      Humdepth        pH
#> N  0.54266218 0.7386046
#> P  0.32412825 0.5728181
#> K  0.15961613 0.1404062
#> Ca 0.03156073 0.2365150
#> Mg 0.01477451 0.1134202
#> S  0.42754109 0.3141949
  • Visualization using the corrplot() function使用corrplot() function 进行可视化
r <- corrplot(coeffs, 
              method = "number", 
              p.mat = pvalues, 
              sig.level = 0.05, # displays only corr. coeff. for p < 0.05
              insig = "blank",  # else leave the cell blank
              tl.srt = 0,       # control the orintation of text labels
              tl.offset = 1)    # control of the offset of the text labels

  • Use the results of the corrplot() function to build a more "traditionnally" matrix of results使用corrplot() function 的结果构建更“传统”的结果矩阵
# Keep only the correlation coefficients for pvalues < 0.05
ResultsMatrix <- r$corrPos %>% 
  mutate(corr = ifelse(p.value < 0.05, corr, NA)) 


# Set factors to control the order of rows and columns in the final cross-table
ResultsMatrix$xName <- factor(ResultsMatrix$xName, 
                              levels = c("Al", "Fe", "Mn", "Zn", "Mo", "Baresoil", "Humdepth", "pH"))

ResultsMatrix$yName <- factor(ResultsMatrix$yName,
                              levels = c("N", "P", "K", "Ca", "Mg", "S"))

# Build the cross-table
xtabs(corr ~ yName + xName, 
      data = ResultsMatrix, 
      sparse = TRUE, 
      addNA = TRUE)
  • Output Output
#> 6 x 8 sparse Matrix of class "dgCMatrix"
#>      xName
#> yName Al Fe        Mn        Zn Mo Baresoil  Humdepth pH
#>    N  NA NA        NA        NA NA       NA        NA NA
#>    P  NA NA 0.6078261 0.7342323 NA       NA        NA NA
#>    K  NA NA 0.6757991 0.7424407 NA       NA        NA NA
#>    Ca NA NA 0.6313043 0.6863854 NA       NA 0.4396914 NA
#>    Mg NA NA 0.5704348 0.6006960 NA       NA 0.4912655 NA
#>    S  NA NA 0.5140248 0.7778986 NA       NA        NA NA

Created on 2021-12-21 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2021 年 12 月 21 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM