Skip to contents

Computes a symmetric matrix of squared association measures for a mix of numeric and categorical variables, optionally using row weights. For numeric–numeric pairs the entry is the weighted squared Pearson correlation \(r_w^2\). For numeric–factor pairs the entry is the weighted eta-squared \(\eta_w^2\). For factor–factor pairs the entry is the weighted squared first canonical correlation \(\rho_{w,1}^2\).

Usage

cor_sq_matrix(data, weights = NULL)

Arguments

data

(data.frame)
The dataframe from which variables are selected. Numeric columns are treated as continuous, while character and logical columns are coerced to factors. Pairwise complete associations are calculated for instances of missing values.

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. Character and logical variables in data are coerced to factors.

Examples

#----------------------------------------------------------------------------
# cor_sq_matrix() examples
#----------------------------------------------------------------------------
library(vclust)

set.seed(123)
n <- 200
df <- data.frame(
  x = rnorm(n, 0, 1),
  y = 0.6 * rnorm(n) + 0.4 * rnorm(n),    # another numeric
  g = factor(sample(letters[1:3], n, replace = TRUE, prob = c(0.4, 0.4, 0.2))),
  h = factor(sample(c("yes", "no"), n, replace = TRUE)),
  z = rnorm(n),
  w = rexp(n, rate = 1)
)

# Use all variables
S1 <- cor_sq_matrix(df, weights = "w")
round(S1, 3)
#>       x     y     g     h     z
#> x 1.000 0.001 0.027 0.009 0.000
#> y 0.001 1.000 0.046 0.005 0.000
#> g 0.027 0.046 1.000 0.113 0.017
#> h 0.009 0.005 0.113 1.000 0.005
#> z 0.000 0.000 0.017 0.005 1.000

# Select a subset of variables explicitly, unweighted
S2 <- cor_sq_matrix(df[c("x", "y", "g", "h")])
round(S2, 3)
#>       x     y     g     h
#> x 1.000 0.002 0.007 0.008
#> y 0.002 1.000 0.010 0.005
#> g 0.007 0.010 1.000 0.015
#> h 0.008 0.005 0.015 1.000

# Include character and logical columns; they are coerced to factors internally
df$char_fac <- sample(c("A", "B", "C"), n, replace = TRUE)
df$logi_fac <- sample(c(TRUE, FALSE), n, replace = TRUE)
S3 <- cor_sq_matrix(df, weights = "w")
round(S3, 3)
#>              x     y     g     h     z char_fac logi_fac
#> x        1.000 0.001 0.027 0.009 0.000    0.023    0.001
#> y        0.001 1.000 0.046 0.005 0.000    0.021    0.002
#> g        0.027 0.046 1.000 0.113 0.017    0.011    0.010
#> h        0.009 0.005 0.113 1.000 0.005    0.004    0.010
#> z        0.000 0.000 0.017 0.005 1.000    0.018    0.003
#> char_fac 0.023 0.021 0.011 0.004 0.018    1.000    0.018
#> logi_fac 0.001 0.002 0.010 0.010 0.003    0.018    1.000

# Missing values
df$logi_fac[1:30] <- NA
S4 <- cor_sq_matrix(df, weights = "w")
round(S4, 3)
#>              x     y     g     h     z char_fac logi_fac
#> x        1.000 0.001 0.027 0.009 0.000    0.023    0.000
#> y        0.001 1.000 0.046 0.005 0.000    0.021    0.004
#> g        0.027 0.046 1.000 0.113 0.017    0.011    0.010
#> h        0.009 0.005 0.113 1.000 0.005    0.004    0.021
#> z        0.000 0.000 0.017 0.005 1.000    0.018    0.003
#> char_fac 0.023 0.021 0.011 0.004 0.018    1.000    0.016
#> logi_fac 0.000 0.004 0.010 0.021 0.003    0.016    1.000