View source: R/genetic_covariance.R
| dist_from_cov | R Documentation |
Converts a covariance matrix to its associated squared pairwise distance
matrix. This inverts the Gower double-centering used by
cov_from_genetic_data.
dist_from_cov(Cov)
Cov |
A square numeric covariance matrix. Does not need to be
full-rank (e.g. the output of |
The squared distance between units i and j is:
D_{ij} = C_{ii} + C_{jj} - 2 C_{ij}
which is the law-of-cosines identity relating a covariance matrix to its implied squared Euclidean distances. The diagonal of the returned matrix is zero.
Use this function to convert a covariance matrix produced by
cov_from_biallelic or cov_from_genetic_data into
a distance matrix suitable for generalized_wishart or
mlpe.
A symmetric numeric matrix of the same dimension as Cov with
zero diagonal and non-negative off-diagonal entries.
cov_from_biallelic, cov_from_genetic_data,
dist_from_biallelic
Cov <- matrix(c(2.0, 1.2, 0.8,
1.2, 1.5, 0.7,
0.8, 0.7, 1.1), nrow = 3, byrow = TRUE)
dist_from_cov(Cov)
# Round-trip: covariance -> distance -> (verify non-negative, zero diag)
Y <- matrix(c(2, 1, 0, 1, 1, 1, 0, 1, 2), nrow = 3, byrow = TRUE)
S_cov <- cov_from_biallelic(Y)
S_dist <- dist_from_cov(S_cov)
diag(S_dist) # all zeros
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.