# nearPD: Find nearest positive definite matrix In lmf: Functions for estimation and inference of selection in age-structured populations

## Description

Compute the nearest positive definite matrix to an approximate one, typically a correlation or variance-covariance matrix.

## Usage

 ```1 2 3``` ```nearPD(x, corr = FALSE, keepDiag = FALSE, do2eigen = TRUE, doSym = FALSE, doDykstra = TRUE, only.values = FALSE, only.matrix = TRUE, eig.tol = 1e-06, conv.tol = 1e-07, posd.tol = 1e-08, maxit = 100, trace = FALSE) ```

## Arguments

 `x` numeric n * n approximately positive definite matrix, typically an approximation to a correlation or covariance matrix. `corr` logical indicating if the matrix should be a correlation matrix. `keepDiag` logical, generalizing `corr`: if `TRUE`, the resulting matrix should have the same diagonal (`diag(x)`) as the input matrix. `do2eigen` logical indicating if a `posdefify` eigen step should be applied to the result of the Higham algorithm. `doSym` logical indicating if `X <- (X + t(X))/2` should be done, after `X <- tcrossprod(Qd, Q)`. Some doubt if this is necessary. `doDykstra` logical indicating if Dykstra's correction should be used; true by default. If false, the algorithm is basically the direct fixpoint iteration Y(k) = P_U(P_S(Y(k-1))). `only.values` logical; if `TRUE`, the result is just the vector of eigen values of the approximating matrix. `only.matrix` logical indicating if only the matrix should be returned. `eig.tol` defines relative positiveness of eigenvalues compared to largest one, λ_1. Eigen values λ_k are treated as if zero when λ_k / λ_1 = eig.tol. `conv.tol` convergence tolerance for Higham algorithm. `posd.tol` tolerance for enforcing positive definiteness (in the final `posdefify` step when `do2eigen` is `TRUE`). `maxit` maximum number of iterations allowed. `trace` logical or integer specifying if convergence monitoring should be traced.

## Details

This function is identical to `nearPD` in package Matrix as far as the algorithmic method is concerned, but has an addition of the argument `only.matrix` to ease its application within the function `fs`, has lost the argument `ensureSymmetry` and have a small change in the list returned when `only.matrix = FALSE`.

Please see `nearPD` in package Matrix for further details.

## Value

`nearPD` returns a numeric vector of eigen values of the approximating matrix if `only.values = TRUE`, returns the computed positive definite matrix if `only.matrix = TRUE` and else returns a list with the following componets:

 `mat` matrix of class "dpoMatrix", the computed positive-definite matrix. `eigenvalues` numeric vector of eigenvalues of `mat`. `corr` logical, just the argument `corr`. `normF` the Frobenius norm (`norm(x-X, "F")`) of the difference between the original and the resulting matrix. `iterations` number of iterations needed. `converged` logical indicating if iterations converged.

## Author(s)

Jens Oehlschlaegel donated a first version. Subsequent changes by the Matrix package authors and present modifications by Thomas Kvalnes.

## References

Cheng, S.H. and Higham, N. 1998. A Modified Cholesky Algorithm Based on a Symmetric Indefinite Factorization. SIAM Journal on Matrix Analysis and Applications, 19, 1097-1110.

Knol, D.L. and ten Berge, J.M.F. 1989. Least-squares approximation of an improper correlation matrix by a proper one. Psychometrika, 54, 53-61.

Higham, N. 2002. Computing the nearest correlation matrix - a problem from finance. IMA Journal of Numerical Analysis, 22, 329-343.

`fs`, `lmf`, `nearPD`, `posdefify`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```#Simulated non-positive definite (PD) matrix nonPD <- matrix(c(2.04e-03, 3.54e-05, 7.52e-03, 3.54e-05, 6.15e-07, 1.30e-04, 7.52e-03, 1.30e-04, 2.76e-02), ncol = 3) #View eigenvalues (PD = only positive eigenvalues) eigen(nonPD) #Calculate PD matrix PD <- nearPD(nonPD, only.matrix = TRUE) PD #View eigenvalues eigen(PD) #More thorough examples are given in the help pages for nearPD #in the Matrix package. ```

### Example output

```eigen() decomposition
\$values
[1]  2.964892e-02  2.727213e-09 -8.310771e-06

\$vectors
[,1]         [,2]         [,3]
[1,] -0.262804380  0.002272349  0.964846461
[2,] -0.004544349 -0.999989050  0.001117327
[3,] -0.964838436  0.004090961 -0.262811829

[,1]         [,2]         [,3]
[1,] 2.047737e-03 3.540039e-05 0.0075178920
[2,] 3.540039e-05 6.122832e-07 0.0001299661
[3,] 7.517892e-03 1.299661e-04 0.0276005740
eigen() decomposition
\$values
[1] 2.964892e-02 2.964892e-10 2.963457e-10

\$vectors
[,1]          [,2]         [,3]
[1,] -0.26280437  9.648484e-01  0.001201854
[2,] -0.00454325  8.141693e-06 -0.999989679
[3,] -0.96483844 -2.628071e-01  0.004381407
```

lmf documentation built on May 30, 2017, 1:44 a.m.