'cor_matrix' computing correlation matrix for numeric variables from data.

cor_matrix(data, variables = NULL, method = "pearson")

Arguments

data

A numeric matrix, a data frame or a vector.

variables

An optional character string giving a variables for computing correlation matrix. This must be colnames from data.

method

A character string specifying the correlation coefficient to be calculated. Possible values are: 'pearson' - computes the Pearson correlation coefficient (default), 'spearman' - computes the Spearman rank correlation coefficient.

Value

A correlation matrix is returned as a numeric matrix, which is a symmetric matrix with correlation coefficients between -1 and 1. The diagonal elements are always equal to 1.

Examples


library(toRpEDA)

#computing correlation matrix only from numeric variables
cor_matrix(data = iris)
#> Warning: Only numeric variables were used to calculate the correlation matrix.
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
#> Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
#> Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
#> Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000

#computing correlation matrix from selected numeric variables
cor_matrix(data = mtcars, variables = c("cyl", "vs", "carb"))
#>             cyl         vs       carb
#> cyl   1.0000000 -0.8108118  0.5269883
#> vs   -0.8108118  1.0000000 -0.5696071
#> carb  0.5269883 -0.5696071  1.0000000

#computing correlation matrix from numeric variables using 'spearman' method
data <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))
cor_matrix(data, method = 'spearman')
#>             x           y           z
#> x  1.00000000 -0.07563156  0.08609661
#> y -0.07563156  1.00000000 -0.07695170
#> z  0.08609661 -0.07695170  1.00000000