Calculate correlation coefficients between variables in a data.frame, matrix or table using 3 different functions for 3 different possible pairs of vairables:

• numeric - numeric

• numeric - categorical

• categorical - categorical

calculate_cors(
x,
num_num_f = NULL,
num_cat_f = NULL,
cat_cat_f = NULL,
max_cor = NULL
)

# S3 method for explainer
calculate_cors(
x,
num_num_f = NULL,
num_cat_f = NULL,
cat_cat_f = NULL,
max_cor = NULL
)

# S3 method for matrix
calculate_cors(
x,
num_num_f = NULL,
num_cat_f = NULL,
cat_cat_f = NULL,
max_cor = NULL
)

# S3 method for table
calculate_cors(
x,
num_num_f = NULL,
num_cat_f = NULL,
cat_cat_f = NULL,
max_cor = NULL
)

# S3 method for default
calculate_cors(
x,
num_num_f = NULL,
num_cat_f = NULL,
cat_cat_f = NULL,
max_cor = NULL
)

## Arguments

x object used to select method. See more below. A function used to determine correlation coefficient between a pair of numeric variables A function used to determine correlation coefficient between a pair of numeric and categorical variable A function used to determine correlation coefficient between a pair of categorical variables A number used to indicate absolute correlation (like 1 in cor). Must be supplied if any of *_f arguments is supplied.

## Value

A symmetrical matrix A of size n x n, where n - amount of columns in x (or dimensions for table). The value at A(i,j) is the correlation coefficient between ith and jth variable. On the diagonal, values from max_cor are set.

## X argument

When x is a data.frame, all columns of numeric type are treated as numeric variables and all columns of factor type are treated as categorical variables. Columns of other types are ignored.

When x is a matrix, it is converted to data.frame using as.data.frame.matrix.

When x is a explainer, the tests are performed on its data element.

When x is a table, it is treated as contingency table. Its dimensions must be named, but none of them may be named Frequency.

## Default functions

By default, the function calculates p_value of statistical tests ( cor.test for 2 numeric, chisq.test for factor and kruskal.test for mixed).

Then, the correlation coefficients are calculated as -log10(p_value). Any results above 100 are treated as absolute correlation and cut to 100.

The results are then divided by 100 to fit inside [0,1].

If only numeric data was supplied, the function used is cor.test.

## Custom functions

Creating consistent measures for correlation coefficients, which are comparable for different kinds of variables, is a non-trivial task. Therefore, if user wishes to use custom function for calculating correlation coefficients, he must provide all necessary functions. Using a custom function for one case and a default for the other is consciously not supported. Naturally, user may supply copies of default functions at his own responsibility.

Function calculate_cors chooses, which parameters of *_f are required based on data supported. For example, for a matrix with numeric data only num_num_f is required. On the other hand, for a table only cat_cat_f is required.

All *_f parameters must be functions, which accept 2 parameters (numeric or factor vectors respectively) and return a single number from [0,max_num]. The num_cat_f must accept numeric argument as first and factor argument as second.

cor.test, chisq.test, kruskal.test

## Examples


data(mtcars)
# Make sure, that categorical variables are factors
mtcars$vs <- factor(mtcars$vs, labels = c('V-shaped', 'straight'))
mtcars$am <- factor(mtcars$am, labels = c('automatic', 'manual'))
calculate_cors(mtcars)
#>             mpg        cyl       disp          hp        drat          wt
#> mpg  1.00000000 0.09213768 0.09027782 0.067476725 0.047504984 0.098880796
#> cyl  0.09213768 1.00000000 0.11744043 0.084586878 0.050838285 0.069145071
#> disp 0.09027782 0.11744043 1.00000000 0.071461389 0.052771998 0.109128153
#> hp   0.06747673 0.08458688 0.07146139 1.000000000 0.020004879 0.043823888
#> drat 0.04750498 0.05083829 0.05277200 0.020004879 1.000000000 0.053201852
#> wt   0.09888080 0.06914507 0.10912815 0.043823888 0.053201852 1.000000000
#> qsec 0.01767462 0.03436456 0.01881271 0.052391063 0.002079008 0.004699691
#> vs   0.04078383 0.05231293 0.04252054 0.045443188 0.018952975 0.029658944
#> am   0.02756135 0.02437490 0.03291156 0.013599308 0.038793688 0.043978089
#> gear 0.02267530 0.02379521 0.03016107 0.003071426 0.050777880 0.033385091
#> carb 0.02964792 0.02711675 0.01597431 0.061063597 0.002067802 0.018345001
#>             qsec          vs          am        gear        carb
#> mpg  0.017674616 0.040783832 0.027561351 0.022675300 0.029647920
#> cyl  0.034364557 0.052312933 0.024374902 0.023795207 0.027116748
#> disp 0.018812712 0.042520543 0.032911561 0.030161068 0.015974311
#> hp   0.052391063 0.045443188 0.013599308 0.003071426 0.061063597
#> drat 0.002079008 0.018952975 0.038793688 0.050777880 0.002067802
#> wt   0.004699691 0.029658944 0.043978089 0.033385091 0.018345001
#> qsec 1.000000000 0.049801564 0.005890707 0.006152266 0.043432361
#> vs   0.049801564 1.000000000 0.002553069 0.025715289 0.019595498
#> am   0.005890707 0.002553069 1.000000000 0.044059498 0.005198029
#> gear 0.006152266 0.025715289 0.044059498 1.000000000 0.008893124
#> carb 0.043432361 0.019595498 0.005198029 0.008893124 1.000000000
# For a table:
data(HairEyeColor)
calculate_cors(HairEyeColor)
#>            Hair         Eye         Sex
#> Hair 1.00000000 0.246335235 0.013360089
#> Eye  0.24633523 1.000000000 0.001704363
#> Sex  0.01336009 0.001704363 1.000000000
# Custom functions:
num_mtcars <- mtcars[,-which(colnames(mtcars) %in% c('vs', 'am'))]
my_f <- function(x,y) cor.test(x, y, method = 'spearman', exact=FALSE)\$estimate
calculate_cors(num_mtcars, num_num_f = my_f, max_cor = 1)
#>             mpg        cyl       disp         hp        drat         wt
#> mpg   1.0000000 -0.9108013 -0.9088824 -0.8946646  0.65145546 -0.8864220
#> cyl  -0.9108013  1.0000000  0.9276516  0.9017909 -0.67888119  0.8577282
#> disp -0.9088824  0.9276516  1.0000000  0.8510426 -0.68359210  0.8977064
#> hp   -0.8946646  0.9017909  0.8510426  1.0000000 -0.52012499  0.7746767
#> drat  0.6514555 -0.6788812 -0.6835921 -0.5201250  1.00000000 -0.7503904
#> wt   -0.8864220  0.8577282  0.8977064  0.7746767 -0.75039041  1.0000000
#> qsec  0.4669358 -0.5723509 -0.4597818 -0.6666060  0.09186863 -0.2254012
#> gear  0.5427816 -0.5643105 -0.5944703 -0.3314016  0.74481617 -0.6761284
#> carb -0.6574976  0.5800680  0.5397781  0.7333794 -0.12522294  0.4998120
#>             qsec       gear       carb
#> mpg   0.46693575  0.5427816 -0.6574976
#> cyl  -0.57235095 -0.5643105  0.5800680
#> disp -0.45978176 -0.5944703  0.5397781
#> hp   -0.66660602 -0.3314016  0.7333794
#> drat  0.09186863  0.7448162 -0.1252229
#> wt   -0.22540120 -0.6761284  0.4998120
#> qsec  1.00000000 -0.1481997 -0.6587181
#> gear -0.14819967  1.0000000  0.1148870
#> carb -0.65871814  0.1148870  1.0000000