-1

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.

meck373
  • 1,114
  • 1
  • 18
  • 31
  • 1
    The eigenvalues are in a different order. Both results are correct. Note that it is accepted practice to order the eigenvalues according to nonincreasing absolute value, as is returned by R, but not by Excel. So if one answer is "wrong," it is Excel's. – Matthew Lundberg Jul 14 '14 at 17:44
  • @Matthew Lundberg: this should probably be an answer – nico Jul 14 '14 at 17:48
  • @nico I suppose so, although josilber has the actual fix. – Matthew Lundberg Jul 14 '14 at 17:57

2 Answers2

4

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
josliber
  • 43,891
  • 12
  • 98
  • 133
3

The eigenvalues are in a different order. Both results are correct.

Note that it is accepted practice to order the eigenvalues according to nonincreasing absolute value, as is returned by R, but not by Excel. So if one answer is "wrong," it is Excel's.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112