Hello,
I have obtained strange results using eigen on a symmetric matrix:

# this function perform a double centering of a matrix (xij-rowmean(i)-colmean(j)+meantot)
dbcenter=function(mat){
rmean=apply(mat,1,mean)
cmean=apply(mat,2,mean)
newmat=sweep(mat,1,rmean,"-")
newmat=sweep(newmat,2,cmean,"-")
newmat=newmat+mean(mat)
newmat}


# i use spdep package to create a spatial contiguity matrix
library(spdep)
x=dbcenter(nb2mat(cell2nb(3,3),style="B"))

#compute eigenvalues of a 9 by 9 matrix
res=eigen(x)

# some eigenvalues are equal to 0
eq0 <- apply(as.matrix(res$values),1,function(x) identical(all.equal(x, 0), TRUE))


# I remove the corresponding eigenvectors
res0=res$vec[,-which(eq0)]

# then I compute the Froebenius norm with the identity matrix
sum((diag(1,ncol(res0))-crossprod(res0))^2)

[1] 1.515139e-30

# The results are correct,
# then I do it again with a biggest matrix(100 by 100)

x=dbcenter(nb2mat(cell2nb(10,10),style="B"))
res=eigen(x)
eq0 <- apply(as.matrix(res$values),1,function(x) identical(all.equal(x, 0), TRUE))
res0=res$vec[,-which(eq0)]
sum((diag(1,ncol(res0))-crossprod(res0))^2)


[1] 3.986387


I have try the same with res=eigen(x,EISPACK=T):

x=dbcenter(nb2mat(cell2nb(10,10),style="B"))
res=eigen(x,EISPACK=T)
eq0 <- apply(as.matrix(res$values),1,function(x) identical(all.equal(x, 0), TRUE))
res0=res$vec[,-which(eq0)]
sum((diag(1,ncol(res0))-crossprod(res0))^2)
[1] 1.315542e-27



So I wonder I there is a bug in the LAPACK algorithm or if there are some things that I have not understood. Note that my matrix has some pairs of equal eigenvalues.


Thanks in advance.

St�phane DRAY
--------------------------------------------------------------------------------------------------


D�partement des Sciences Biologiques
Universit� de Montr�al, C.P. 6128, succursale centre-ville
Montr�al, Qu�bec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293
E-mail : [EMAIL PROTECTED]
--------------------------------------------------------------------------------------------------


Web                                          http://www.steph280.freesurf.fr/

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to