cov2dist: Conversion of covariance to distance matrix

Description Usage Arguments Details Value Author(s) Examples

View source: R/R_utils_wC.R

Description

Computes a squared Euclidean distance matrix from a covariance matrix among p variables with mean zero

Usage

1
cov2dist(COV, void = FALSE)

Arguments

COV

Symmetric variance-covariance matrix among p (centered) variables

void

TRUE or FALSE to whether return or not return the output. When FALSE no result is displayed but the input COV is modified. Default void=FALSE

Details

For any variables Xi and Xj with mean zero and with sample vectors xi = (xi1,...,xin)' and xj = (xj1,...,xjn)' , their (sample) variances are equal (up-to a constant) to their cross-products, this is, var(Xi) = x'ixi and var(Xj) = x'jxj. Likewise, the covariance is cov(Xi,Xj) = x'ixj.

The distance d(Xi,Xj) between the variables expressed in terms of cross-products is

d2(Xi,Xj) = x'ixi + x'jxj - 2x'ixj

Therefore, the output distance matrix will contain as off-diagonal entries

d2(Xi,Xj) = var(Xi) + var(Xj) - 2cov(Xi,Xj)

while in the diagonal, the distance between one variable with itself is d2(Xi,Xi) = 0

Value

A matrix D containing the squared Euclidean distances

Author(s)

Marco Lopez-Cruz (lopezcru@msu.edu) and Gustavo de los Campos

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  require(SFSI)
  
  # Simulate matrix
  n = 100; p=5
  X = scale(matrix(rnorm(n*p),ncol=p))

  # Distance matrix from a cross-product
  COV = crossprod(X)   # Cross-product X'X
  cov2dist(COV)
  # it must equal (but faster) to:
  as.matrix(dist(t(X)))^2

  # Distance matrix from a variance-covariance matrix
  COV = cov(X)   # Variance matrix of X
  (n-1)*cov2dist(COV)
  # it must equal (but faster) to:
  as.matrix(dist(t(X)))^2

  # Using void=TRUE
  cov2dist(COV,void=TRUE)
  (n-1)*COV   # notice that COV was modified

MarcooLopez/SFSI_data documentation built on April 15, 2021, 10:53 a.m.