Skip to contents

Computes a symmetric matrix of Pearson correlations, optionally using row weights.

Usage

cor_matrix(data, weights = NULL)

Arguments

data

(data.frame or matrix)
The dataframe from which variables are selected. Pairwise complete associations are calculated for instances of missing values. An error will return if any columns in data are non-numeric.

weights

(Scalar character or NULL)
The name of the column in data containing row weights. All values must be finite, nonnegative, and with a strictly positive sum. If NULL, an unweighted computation is performed.

Value

A numeric symmetric matrix with row and column names equal to names(data) (excluding the weight column, if any).

Details

Pairwise complete associations are calculated for instances of missing values.

Examples

#----------------------------------------------------------------------------
# cor_matrix() examples
#----------------------------------------------------------------------------
library(vclust)

set.seed(123)
n <- 200
df <- data.frame(
  x = rnorm(n),
  y = rnorm(n),
  z = rnorm(n),
  w = rexp(n, rate = 1)
)

# Correlation matrix
S1 <- cor_matrix(df, weights = "w")
round(S1, 3)
#>        x      y      z
#> x  1.000 -0.083 -0.001
#> y -0.083  1.000 -0.022
#> z -0.001 -0.022  1.000