Wednesday, July 17, 2013

Complex numbers in R and a new version of phyl.cca

I just posted a new version of phyl.cca (my function for phylogenetic canonical correlation analysis) that addresses the following error, which initially seemed very alarming:

> phyl.cca(tree,X,Y)
Error in ccs > 0 : invalid comparison with complex values

It turns out that part spectral decomposition (eigenanalysis) can result in very slightly negative eigenvalues, which result in complex numbers during some steps of the calculations of CCA. In later steps, the imaginary parts go to zero - but R keeps the class of a number as "complex" even if the imaginary part is zero.

To fix this, I merely check to see if all imaginary parts of the canonical correlations are zero, and (if so) set the canonical correlation to the real part of the number only:

if(all(Im(ccs)==0)) ccs<-Re(ccs)

This seems to have solved the problems.

This update is also in a new phytools build (phytools 0.3-11) which can be installed from source.

No comments:

Post a Comment

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.