Search code examples
reigeneigenvectoreigenvalue

Spectral decomposition (eigen)


i have a problem with calculation the Spectral decomposition, i guess, with the sorting of eigen.

According to this website http://www.deltaquants.com/cleaning-correlation-matrices.html i would like to do the same calculation in R

Input <- data.frame(read.csv2(file="testmatrix.csv", header=FALSE, sep=";"))
# same matrix as the example on the website
Eigen <- eigen(Input, only.values=FALSE, symmetric = TRUE)

#Get the eigenvalues/eigenvectors
Eigen$values
Eigen$vectors

The result on the website (excel):

Result form the website

The result from eigen (R)

R Calculation

As the result the new correlation matrix C is not correct.

Thanks for the help. I could provide further information e.c. Code or more details - if it helps.


Solution

  • If you want to order the eigenvalue of a matrix in increasing order, just index eigenvectors and eigenvalues with the output of the order function:

    mat <- matrix(c(1, 2, 3, 2, 7, 4, 3, 4, 0), nrow=3)
    e <- eigen(mat)
    o <- order(e$values, decreasing=FALSE)
    e$values[o]
    # [1] -2.961797  1.056689  9.905108
    e$vectors[,o]
    #            [,1]       [,2]       [,3]
    # [1,]  0.5110650  0.7915817 -0.3349790
    # [2,]  0.2299503 -0.5014262 -0.8340831
    # [3,] -0.8282122  0.3492421 -0.4382859