Skip to contents

Mirrors the C# Correlation class of the Numerics library: the paired-vector forms (Correlation.Pearson/Spearman/KendallsTau, both IList<double> overloads) and the Pearson/Spearman column-pairwise matrix overloads (Pearson(double[,])/Spearman(double[,])).

Usage

correlation(x, y = NULL, method = c("pearson", "spearman", "kendall"))

Arguments

x

a numeric vector (paired-vector form, y required), or a numeric matrix or data frame (matrix form, y omitted) with one column per variable.

y

a numeric vector of the same length as x, or NULL (the default) to compute the correlation matrix of x's columns instead.

method

one of "pearson" (the default), "spearman", or "kendall". "kendall" is rejected when y is NULL: upstream has no KendallsTau(double[,]) overload, so there is no Kendall matrix form to compute.

Value

with y given, a single numeric correlation coefficient; with y NULL, a numeric ncol(x)-by-ncol(x) matrix, dimnamed from x's columns when x has column names.

Examples

x <- c(14, 8, 32, 7, 3, 15)
y <- c(10, 5, 7, 4, 3, 8)
correlation(x, y)
#> [1] 0.5450274
correlation(x, y, method = "kendall")
#> [1] 0.6
correlation(cbind(x, y))
#>           x         y
#> x 1.0000000 0.5450274
#> y 0.5450274 1.0000000