Description Usage Arguments Details Value Author(s) Examples
Computes a squared Euclidean distance matrix from a covariance matrix among p variables with mean zero
1 |
COV |
Symmetric variance-covariance matrix among p (centered) variables |
void |
|
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
Therefore, the output distance matrix will contain as off-diagonal entries
while in the diagonal, the distance between one variable with itself is d2(Xi,Xi) = 0
A matrix D containing the squared Euclidean distances
Marco Lopez-Cruz (lopezcru@msu.edu) and Gustavo de los Campos
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.