require(SFSI)
data(wheatHTP)
X = scale(Y[,4:7])
(V = crossprod(X)) # Covariance matrix
# Covariance matrix to distance matrix
(D1 = cov2dist(V))
# it must equal (but faster) to:
D0 = as.matrix(dist(t(X)))^2
max(abs(D0-D1))
# Covariance to a correlation matrix
(R1 = cov2cor2(V))
# it must equal (but faster) to:
R0 = cov2cor(V)
max(abs(R0-R1))
if(requireNamespace("float")){
# Using a 'float' type variable
V2 = float::fl(V)
D2 = cov2dist(V2)
max(abs(D1-D2)) # discrepancy with previous matrix
R2 = cov2cor2(V2)
max(abs(R1-R2)) # discrepancy with previous matrix
}
# Using void=TRUE
cov2dist(V,void=TRUE)
V # notice that V was modified
cov2dist(V2,void=TRUE)
V2 # notice that V2 was modified
Run the code above in your browser using DataLab