Skip to contents

If argument 'df' results from cor_df(), transforms it to a correlation matrix. If argument 'df' is a dataframe with predictors, and the argument 'predictors' is provided then cor_df() is used to compute pairwise correlations, and the result is transformed to matrix.

Accepts a parallelization setup via future::plan() and a progress bar via progressr::handlers() (see examples).

Usage

cor_matrix(df = NULL, predictors = NULL)

Arguments

df

(required; data frame, tibble, or sf) A data frame with responses and predictors. Default: NULL.

predictors

(optional; character vector) Names of the predictors to select from df. If omitted, all numeric columns in df are used instead. If argument response is not provided, non-numeric variables are ignored. Default: NULL

Value

correlation matrix

See also

Other pairwise_correlation: cor_clusters(), cor_cramer_v(), cor_df(), cor_select()

Author

Blas M. Benito, PhD

Examples

data(
  vi,
  vi_predictors
)

#reduce size of vi to speed-up example execution
vi <- vi[1:1000, ]

#mixed predictors
vi_predictors <- vi_predictors[1:10]

#parallelization setup
future::plan(
  future::multisession,
  workers = 2 #set to parallelly::availableCores() - 1
)

#progress bar
# progressr::handlers(global = TRUE)

#correlation data frame
df <- cor_df(
  df = vi,
  predictors = vi_predictors
)

df
#>                     x                  y correlation
#> 1  koppen_description        koppen_zone  0.99745611
#> 2        koppen_group        koppen_zone  0.99142913
#> 3            swi_mean koppen_description  0.90497221
#> 4            swi_mean        koppen_zone  0.90454452
#> 5             swi_max           swi_mean  0.89615880
#> 6             swi_max        koppen_zone  0.89539332
#> 7             swi_max koppen_description  0.89444755
#> 8  koppen_description       koppen_group  0.87130629
#> 9             swi_min        koppen_zone  0.86281575
#> 10           swi_mean       koppen_group  0.86243147
#> 11            swi_min koppen_description  0.82466707
#> 12            swi_max       koppen_group  0.81826035
#> 13            swi_min       koppen_group  0.81719574
#> 14           swi_mean          soil_type  0.75221964
#> 15            swi_max          soil_type  0.73723126
#> 16            swi_min           swi_mean  0.67767585
#> 17            swi_min            swi_max  0.64216484
#> 18            swi_min          soil_type  0.61277268
#> 19       koppen_group          soil_type  0.57728499
#> 20     topo_diversity         topo_slope  0.53815381
#> 21     topo_elevation        koppen_zone  0.53549999
#> 22     topo_elevation koppen_description  0.51881683
#> 23     topo_elevation         topo_slope  0.37851557
#> 24 koppen_description          soil_type  0.36467487
#> 25     topo_diversity koppen_description  0.35558428
#> 26     topo_diversity        koppen_zone  0.35536647
#> 27     topo_diversity          soil_type  0.34146783
#> 28        koppen_zone          soil_type  0.33542742
#> 29     topo_elevation          soil_type  0.32824302
#> 30     topo_diversity       koppen_group  0.31970029
#> 31         topo_slope        koppen_zone  0.31392482
#> 32         topo_slope koppen_description  0.29823947
#> 33         topo_slope          soil_type  0.29353313
#> 34         topo_slope       koppen_group  0.27219308
#> 35           swi_mean     topo_diversity  0.24291289
#> 36            swi_min     topo_diversity  0.23685867
#> 37     topo_elevation     topo_diversity  0.22066271
#> 38            swi_min         topo_slope  0.21877803
#> 39     topo_elevation       koppen_group  0.20367646
#> 40            swi_max     topo_diversity  0.18539344
#> 41           swi_mean     topo_elevation -0.17649343
#> 42           swi_mean         topo_slope  0.12893647
#> 43            swi_max         topo_slope  0.10922059
#> 44            swi_max     topo_elevation -0.08993101
#> 45            swi_min     topo_elevation -0.04417289

#correlation matrix
m <- cor_matrix(
  df = df
)

m
#>                    koppen_description koppen_group koppen_zone soil_type
#> koppen_description          1.0000000    0.8713063   0.9974561 0.3646749
#> koppen_group                0.8713063    1.0000000   0.9914291 0.5772850
#> koppen_zone                 0.9974561    0.9914291   1.0000000 0.3354274
#> soil_type                   0.3646749    0.5772850   0.3354274 1.0000000
#> swi_max                     0.8944476    0.8182603   0.8953933 0.7372313
#> swi_mean                    0.9049722    0.8624315   0.9045445 0.7522196
#> swi_min                     0.8246671    0.8171957   0.8628158 0.6127727
#> topo_diversity              0.3555843    0.3197003   0.3553665 0.3414678
#> topo_elevation              0.5188168    0.2036765   0.5355000 0.3282430
#> topo_slope                  0.2982395    0.2721931   0.3139248 0.2935331
#>                       swi_max  swi_mean    swi_min topo_diversity
#> koppen_description 0.89444755 0.9049722 0.82466707      0.3555843
#> koppen_group       0.81826035 0.8624315 0.81719574      0.3197003
#> koppen_zone        0.89539332 0.9045445 0.86281575      0.3553665
#> soil_type          0.73723126 0.7522196 0.61277268      0.3414678
#> swi_max            1.00000000 0.8961588 0.64216484      0.1853934
#> swi_mean           0.89615880 1.0000000 0.67767585      0.2429129
#> swi_min            0.64216484 0.6776758 1.00000000      0.2368587
#> topo_diversity     0.18539344 0.2429129 0.23685867      1.0000000
#> topo_elevation     0.08993101 0.1764934 0.04417289      0.2206627
#> topo_slope         0.10922059 0.1289365 0.21877803      0.5381538
#>                    topo_elevation topo_slope
#> koppen_description     0.51881683  0.2982395
#> koppen_group           0.20367646  0.2721931
#> koppen_zone            0.53549999  0.3139248
#> soil_type              0.32824302  0.2935331
#> swi_max                0.08993101  0.1092206
#> swi_mean               0.17649343  0.1289365
#> swi_min                0.04417289  0.2187780
#> topo_diversity         0.22066271  0.5381538
#> topo_elevation         1.00000000  0.3785156
#> topo_slope             0.37851557  1.0000000

#generating it from the original data
m <- cor_matrix(
  df = vi,
  predictors = vi_predictors
)

m
#>                    koppen_description koppen_group koppen_zone soil_type
#> koppen_description          1.0000000    0.8713063   0.9974561 0.3646749
#> koppen_group                0.8713063    1.0000000   0.9914291 0.5772850
#> koppen_zone                 0.9974561    0.9914291   1.0000000 0.3354274
#> soil_type                   0.3646749    0.5772850   0.3354274 1.0000000
#> swi_max                     0.8944476    0.8182603   0.8953933 0.7372313
#> swi_mean                    0.9049722    0.8624315   0.9045445 0.7522196
#> swi_min                     0.8246671    0.8171957   0.8628158 0.6127727
#> topo_diversity              0.3555843    0.3197003   0.3553665 0.3414678
#> topo_elevation              0.5188168    0.2036765   0.5355000 0.3282430
#> topo_slope                  0.2982395    0.2721931   0.3139248 0.2935331
#>                       swi_max  swi_mean    swi_min topo_diversity
#> koppen_description 0.89444755 0.9049722 0.82466707      0.3555843
#> koppen_group       0.81826035 0.8624315 0.81719574      0.3197003
#> koppen_zone        0.89539332 0.9045445 0.86281575      0.3553665
#> soil_type          0.73723126 0.7522196 0.61277268      0.3414678
#> swi_max            1.00000000 0.8961588 0.64216484      0.1853934
#> swi_mean           0.89615880 1.0000000 0.67767585      0.2429129
#> swi_min            0.64216484 0.6776758 1.00000000      0.2368587
#> topo_diversity     0.18539344 0.2429129 0.23685867      1.0000000
#> topo_elevation     0.08993101 0.1764934 0.04417289      0.2206627
#> topo_slope         0.10922059 0.1289365 0.21877803      0.5381538
#>                    topo_elevation topo_slope
#> koppen_description     0.51881683  0.2982395
#> koppen_group           0.20367646  0.2721931
#> koppen_zone            0.53549999  0.3139248
#> soil_type              0.32824302  0.2935331
#> swi_max                0.08993101  0.1092206
#> swi_mean               0.17649343  0.1289365
#> swi_min                0.04417289  0.2187780
#> topo_diversity         0.22066271  0.5381538
#> topo_elevation         1.00000000  0.3785156
#> topo_slope             0.37851557  1.0000000

#disable parallelization
future::plan(future::sequential)