Skip to contents

Sort a correlation matrix based on hclust().

Usage

cor_sort(
  x,
  distance = "correlation",
  hclust_method = "complete",
  na_action = "infer",
  ...
)

Arguments

x

A correlation matrix.

distance

How the distance between each variable should be calculated. If correlation (default; suited for correlation matrices), the matrix will be rescaled to 0-1 (distance = 0 indicating correlation of 1; distance = 1 indicating correlation of -1). If raw, then the matrix will be used as a distance matrix as-is. Can be others (euclidean, manhattan, ...), in which case it will be passed to dist() (see the arguments for it).

hclust_method

Argument passed down into the method argument of hclust().

na_action

How to deal with undefined correlations, i.e. NA values in the matrix (which typically arise when two variables were never observed together). Can be "infer" (default), "zero", "omit" or "error". See the Undefined correlations section below.

...

Other arguments to be passed to or from other functions.

Undefined correlations

Clustering requires a complete distance matrix, so undefined correlations have to be resolved before hclust() can be run. Note that this only affects the ordering: the matrix that is returned keeps its NA values, and no imputed value is ever reported as a coefficient.

  • "infer" (default) estimates each undefined correlation from the variables that were observed with both members of the pair, as \(r_{bc} = r_{bS} R_{SS}^{-1} R_{Sc}\). This is the maximum-entropy (maximum-determinant) positive-definite completion of the matrix, i.e. the value that sets the partial correlation of the pair, given the remaining variables, to zero (Dempster, 1972). When no variable was observed with both members of the pair, or when the estimate cannot be computed, it falls back to 0 for that pair. With a single mediating variable it reduces to \(r_{ab} \times r_{ac}\).

  • "zero" treats every undefined correlation as 0. Cheaper and fully transparent, but it asserts that the pair is unrelated, which can pull apart variables that the rest of the matrix suggests belong together (especially with hclust_method = "complete").

  • "omit" computes the ordering on the complete-case submatrix, and appends the variables involved in undefined pairs at the end, in their original order.

  • "error" throws an error listing the undefined pairs.

"infer" is only available for square correlation matrices (i.e., with distance = "correlation"). For other distances, and for non-square matrices, undefined distances are replaced by the average of the defined ones unless na_action = "error".

References

Dempster, A. P. (1972). Covariance selection. Biometrics, 28(1), 157-175.

Grone, R., Johnson, C. R., Sá, E. M., & Wolkowicz, H. (1984). Positive definite completions of partial Hermitian matrices. Linear Algebra and its Applications, 58, 109-124.

Examples

x <- correlation(mtcars)

cor_sort(as.matrix(x))
#>             carb         wt         hp        cyl       disp        qsec
#> carb  1.00000000  0.4276059  0.7498125  0.5269883  0.3949769 -0.65624923
#> wt    0.42760594  1.0000000  0.6587479  0.7824958  0.8879799 -0.17471588
#> hp    0.74981247  0.6587479  1.0000000  0.8324475  0.7909486 -0.70822339
#> cyl   0.52698829  0.7824958  0.8324475  1.0000000  0.9020329 -0.59124207
#> disp  0.39497686  0.8879799  0.7909486  0.9020329  1.0000000 -0.43369788
#> qsec -0.65624923 -0.1747159 -0.7082234 -0.5912421 -0.4336979  1.00000000
#> vs   -0.56960714 -0.5549157 -0.7230967 -0.8108118 -0.7104159  0.74453544
#> mpg  -0.55092507 -0.8676594 -0.7761684 -0.8521620 -0.8475514  0.41868403
#> drat -0.09078980 -0.7124406 -0.4487591 -0.6999381 -0.7102139  0.09120476
#> am    0.05753435 -0.6924953 -0.2432043 -0.5226070 -0.5912270 -0.22986086
#> gear  0.27407284 -0.5832870 -0.1257043 -0.4926866 -0.5555692 -0.21268223
#>              vs        mpg        drat          am       gear
#> carb -0.5696071 -0.5509251 -0.09078980  0.05753435  0.2740728
#> wt   -0.5549157 -0.8676594 -0.71244065 -0.69249526 -0.5832870
#> hp   -0.7230967 -0.7761684 -0.44875912 -0.24320426 -0.1257043
#> cyl  -0.8108118 -0.8521620 -0.69993811 -0.52260705 -0.4926866
#> disp -0.7104159 -0.8475514 -0.71021393 -0.59122704 -0.5555692
#> qsec  0.7445354  0.4186840  0.09120476 -0.22986086 -0.2126822
#> vs    1.0000000  0.6640389  0.44027846  0.16834512  0.2060233
#> mpg   0.6640389  1.0000000  0.68117191  0.59983243  0.4802848
#> drat  0.4402785  0.6811719  1.00000000  0.71271113  0.6996101
#> am    0.1683451  0.5998324  0.71271113  1.00000000  0.7940588
#> gear  0.2060233  0.4802848  0.69961013  0.79405876  1.0000000
cor_sort(x, hclust_method = "ward.D2") # It can also reorder the long form output
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |         95% CI | t(30) |         p
#> --------------------------------------------------------------------
#> wt         |       carb |  0.43 | [ 0.09,  0.68] |  2.59 | 0.205    
#> wt         |       qsec | -0.17 | [-0.49,  0.19] | -0.97 | > .999   
#> wt         |         vs | -0.55 | [-0.76, -0.26] | -3.65 | 0.023*   
#> wt         |         am | -0.69 | [-0.84, -0.45] | -5.26 | < .001***
#> wt         |       gear | -0.58 | [-0.77, -0.29] | -3.93 | 0.012*   
#> cyl        |         wt |  0.78 | [ 0.60,  0.89] |  6.88 | < .001***
#> cyl        |       disp |  0.90 | [ 0.81,  0.95] | 11.45 | < .001***
#> cyl        |         hp |  0.83 | [ 0.68,  0.92] |  8.23 | < .001***
#> cyl        |       carb |  0.53 | [ 0.22,  0.74] |  3.40 | 0.041*   
#> cyl        |       qsec | -0.59 | [-0.78, -0.31] | -4.02 | 0.010*   
#> cyl        |         vs | -0.81 | [-0.90, -0.64] | -7.59 | < .001***
#> cyl        |       drat | -0.70 | [-0.84, -0.46] | -5.37 | < .001***
#> cyl        |         am | -0.52 | [-0.74, -0.21] | -3.36 | 0.043*   
#> cyl        |       gear | -0.49 | [-0.72, -0.17] | -3.10 | 0.079    
#> disp       |         wt |  0.89 | [ 0.78,  0.94] | 10.58 | < .001***
#> disp       |         hp |  0.79 | [ 0.61,  0.89] |  7.08 | < .001***
#> disp       |       carb |  0.39 | [ 0.05,  0.65] |  2.35 | 0.303    
#> disp       |       qsec | -0.43 | [-0.68, -0.10] | -2.64 | 0.197    
#> disp       |         vs | -0.71 | [-0.85, -0.48] | -5.53 | < .001***
#> disp       |       drat | -0.71 | [-0.85, -0.48] | -5.53 | < .001***
#> disp       |         am | -0.59 | [-0.78, -0.31] | -4.02 | 0.010*   
#> disp       |       gear | -0.56 | [-0.76, -0.26] | -3.66 | 0.023*   
#> hp         |         wt |  0.66 | [ 0.40,  0.82] |  4.80 | 0.001**  
#> hp         |       carb |  0.75 | [ 0.54,  0.87] |  6.21 | < .001***
#> hp         |       qsec | -0.71 | [-0.85, -0.48] | -5.49 | < .001***
#> hp         |         vs | -0.72 | [-0.86, -0.50] | -5.73 | < .001***
#> hp         |       drat | -0.45 | [-0.69, -0.12] | -2.75 | 0.170    
#> hp         |         am | -0.24 | [-0.55,  0.12] | -1.37 | > .999   
#> hp         |       gear | -0.13 | [-0.45,  0.23] | -0.69 | > .999   
#> qsec       |       carb | -0.66 | [-0.82, -0.40] | -4.76 | 0.001**  
#> qsec       |         vs |  0.74 | [ 0.53,  0.87] |  6.11 | < .001***
#> qsec       |         am | -0.23 | [-0.54,  0.13] | -1.29 | > .999   
#> qsec       |       gear | -0.21 | [-0.52,  0.15] | -1.19 | > .999   
#> vs         |       carb | -0.57 | [-0.77, -0.28] | -3.80 | 0.017*   
#> vs         |         am |  0.17 | [-0.19,  0.49] |  0.94 | > .999   
#> vs         |       gear |  0.21 | [-0.15,  0.52] |  1.15 | > .999   
#> mpg        |         wt | -0.87 | [-0.93, -0.74] | -9.56 | < .001***
#> mpg        |        cyl | -0.85 | [-0.93, -0.72] | -8.92 | < .001***
#> mpg        |       disp | -0.85 | [-0.92, -0.71] | -8.75 | < .001***
#> mpg        |         hp | -0.78 | [-0.89, -0.59] | -6.74 | < .001***
#> mpg        |       carb | -0.55 | [-0.75, -0.25] | -3.62 | 0.024*   
#> mpg        |       qsec |  0.42 | [ 0.08,  0.67] |  2.53 | 0.222    
#> mpg        |         vs |  0.66 | [ 0.41,  0.82] |  4.86 | 0.001**  
#> mpg        |       drat |  0.68 | [ 0.44,  0.83] |  5.10 | < .001***
#> mpg        |         am |  0.60 | [ 0.32,  0.78] |  4.11 | 0.008**  
#> mpg        |       gear |  0.48 | [ 0.16,  0.71] |  3.00 | 0.097    
#> drat       |         wt | -0.71 | [-0.85, -0.48] | -5.56 | < .001***
#> drat       |       carb | -0.09 | [-0.43,  0.27] | -0.50 | > .999   
#> drat       |       qsec |  0.09 | [-0.27,  0.43] |  0.50 | > .999   
#> drat       |         vs |  0.44 | [ 0.11,  0.68] |  2.69 | 0.187    
#> drat       |         am |  0.71 | [ 0.48,  0.85] |  5.57 | < .001***
#> drat       |       gear |  0.70 | [ 0.46,  0.84] |  5.36 | < .001***
#> am         |       carb |  0.06 | [-0.30,  0.40] |  0.32 | > .999   
#> am         |       gear |  0.79 | [ 0.62,  0.89] |  7.16 | < .001***
#> gear       |       carb |  0.27 | [-0.08,  0.57] |  1.56 | > .999   
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 32
cor_sort(summary(x, redundant = TRUE)) # As well as from the summary
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter |    carb |       wt |       hp |      cyl |     disp |     qsec
#> --------------------------------------------------------------------------
#> carb      |         |     0.43 |  0.75*** |    0.53* |     0.39 |  -0.66**
#> wt        |    0.43 |          |   0.66** |  0.78*** |  0.89*** |    -0.17
#> hp        | 0.75*** |   0.66** |          |  0.83*** |  0.79*** | -0.71***
#> cyl       |   0.53* |  0.78*** |  0.83*** |          |  0.90*** |   -0.59*
#> disp      |    0.39 |  0.89*** |  0.79*** |  0.90*** |          |    -0.43
#> qsec      | -0.66** |    -0.17 | -0.71*** |   -0.59* |    -0.43 |         
#> vs        |  -0.57* |   -0.55* | -0.72*** | -0.81*** | -0.71*** |  0.74***
#> mpg       |  -0.55* | -0.87*** | -0.78*** | -0.85*** | -0.85*** |     0.42
#> drat      |   -0.09 | -0.71*** |    -0.45 | -0.70*** | -0.71*** |     0.09
#> am        |    0.06 | -0.69*** |    -0.24 |   -0.52* |   -0.59* |    -0.23
#> gear      |    0.27 |   -0.58* |    -0.13 |    -0.49 |   -0.56* |    -0.21
#> 
#> Parameter |       vs |      mpg |     drat |       am |    gear
#> ---------------------------------------------------------------
#> carb      |   -0.57* |   -0.55* |    -0.09 |     0.06 |    0.27
#> wt        |   -0.55* | -0.87*** | -0.71*** | -0.69*** |  -0.58*
#> hp        | -0.72*** | -0.78*** |    -0.45 |    -0.24 |   -0.13
#> cyl       | -0.81*** | -0.85*** | -0.70*** |   -0.52* |   -0.49
#> disp      | -0.71*** | -0.85*** | -0.71*** |   -0.59* |  -0.56*
#> qsec      |  0.74*** |     0.42 |     0.09 |    -0.23 |   -0.21
#> vs        |          |   0.66** |     0.44 |     0.17 |    0.21
#> mpg       |   0.66** |          |  0.68*** |   0.60** |    0.48
#> drat      |     0.44 |  0.68*** |          |  0.71*** | 0.70***
#> am        |     0.17 |   0.60** |  0.71*** |          | 0.79***
#> gear      |     0.21 |     0.48 |  0.70*** |  0.79*** |        
#> 
#> p-value adjustment method: Holm (1979)

# Matrices with undefined correlations (e.g., variables that were never
# observed together) can be sorted too
d <- data.frame(
  a = rnorm(50),
  b = c(rnorm(25), rep(NA, 25)),
  c = c(rep(NA, 25), rnorm(25))
)
rez <- correlation(d)
cor_sort(rez)
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |        95% CI | t(23) |     p
#> ---------------------------------------------------------------
#> a          |          b | -0.10 | [-0.48, 0.30] | -0.50 | 0.623
#> a          |          c | -0.36 | [-0.66, 0.04] | -1.84 | 0.156
#> b          |          c |       |               |       |      
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 0-25
cor_sort(rez, na_action = "omit")
#> Warning: Fewer than two variables are free of undefined correlations, so the
#>   matrix cannot be sorted. Returning it unsorted.
#>   Use a different `na_action` to sort it anyway.
#> # Correlation Matrix (pearson-method)
#> 
#> Parameter1 | Parameter2 |     r |        95% CI | t(23) |     p
#> ---------------------------------------------------------------
#> a          |          b | -0.10 | [-0.48, 0.30] | -0.50 | 0.623
#> a          |          c | -0.36 | [-0.66, 0.04] | -1.84 | 0.156
#> b          |          c |       |               |       |      
#> 
#> p-value adjustment method: Holm (1979)
#> Observations: 0-25