View source: R/Helper_functions.R
| correct_positive_definite | R Documentation |
correct_positive_definite verifies that a given covariance matrix
is indeed positive definite by checking that all eigenvalues are positive.
If the given covariance matrix is not positive definite,
correct_positive_definite tries to modify the underlying correlation matrices
genetic_corrmat and full_corrmat in order to obtain a positive definite
covariance matrix.
correct_positive_definite(
covmat,
correction_val = 0.99,
correction_limit = 100
)
covmat |
A symmetric and numeric matrix. If the covariance matrix
should be corrected, it must have a number of attributes, such as
|
correction_val |
A positive number representing the amount by which
|
correction_limit |
A positive integer representing the upper limit for the correction procedure. Defaults to 100. |
This function can be used to verify that a given covariance matrix
is positive definite. It calculates all eigenvalues in order to
investigate whether they are all positive. This property is necessary
for the covariance matrix to be used as a Gaussian covariance matrix.
It is especially useful to check whether any covariance matrix obtained
by construct_covmat_multi is positive definite.
If the given covariance matrix is not positive definite, correct_positive_definite
tries to modify the underlying correlation matrices (called genetic_corrmat and
full_corrmat in construct_covmat or construct_covmat_multi) by
multiplying all off-diagonal entries in the correlation matrices by a given number.
If covmat is a symmetric and numeric matrix and all eigenvalues are
positive, correct_positive_definite simply returns covmat. If some
eigenvalues are not positive and correction_val is a positive number,
correct_positive_definite tries to convert covmat into a positive definite
matrix. If covmat has attributes add_ind, h2,
genetic_corrmat, full_corrmat and phenotype_names,
correct_positive_definite computes a new covariance matrix using slightly
modified correlation matrices genetic_corrmat and full_corrmat.
If the correction is performed successfully, i.e. if the new covariance matrix
is positive definite,the new covariance matrix is returned.
Otherwise, correct_positive_definite returns the original covariance matrix.
construct_covmat, construct_covmat_single and
construct_covmat_multi.
ntrait <- 2
genetic_corrmat <- matrix(0.6, ncol = ntrait, nrow = ntrait)
diag(genetic_corrmat) <- 1
full_corrmat <- matrix(-0.25, ncol = ntrait, nrow = ntrait)
diag(full_corrmat) <- 1
h2_vec <- rep(0.6, ntrait)
cov <- construct_covmat(fam_vec = c("m", "f"),
genetic_corrmat = genetic_corrmat,
h2 = h2_vec,
full_corrmat = full_corrmat)
cov
correct_positive_definite(cov)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.