Weighted Pearson correlation matrix
cor_matrix.RdComputes a symmetric matrix of Pearson correlations, optionally using row weights.
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 indataare non-numeric.- weights
(Scalar character or
NULL)
The name of the column indatacontaining row weights. All values must be finite, nonnegative, and with a strictly positive sum. IfNULL, 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).
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