Dear Jovana.

The only thing I'd add to Michael's comment is that a common cause of 
zero (negative because of numerical imprecision) eigenvalues is when one 
column of your data matrix is a perfect linear function or linear 
combination of one or more other columns. To inspect the eigenvalues (as 
opposed to their square-roots, which is what is output by the "phyl.pca" 
print method) just enter diag(obj$Eval) at the command prompt in R (in 
which obj is the object created by your phytools::phyl.pca function call).

Best wishes, Liam

Liam J. Revell
Professor of Biology, University of Massachusetts Boston
Web: http://faculty.umb.edu/liam.revell/
Book: Phylogenetic Comparative Methods in R 
<https://press.princeton.edu/books/phylogenetic-comparative-methods-in-r> 
(/Princeton University Press/, 2022)


On 11/21/2024 7:23 AM, Michael Zyphur via R-sig-phylo wrote:
> CAUTION: EXTERNAL SENDER
>
> Hi Jovana
>
> The warnings you’re encountering (NaNs were generated) are likely
> because the phyl.pca function uses eigenvalue decomposition. When
> eigenvalues are negative or very close to zero, taking the square root
> as part of the process (e.g., sqrt(V[i, i] * obj$Eval[j, j])) results
> in NaN values.
>
> Negative eigenvalues can arise due to numerical imprecision, or they
> may indicate a problem with the structure of your dataset or the
> phylogenetic covariance matrix. If you've already identified Z-score
> transformation as a potential issue, it might be worth investigating
> further. Here are a few steps to try:
>
> Check the Data:
> Ensure that the input data (data.mean) and the phylogenetic tree
> (tree) are appropriately scaled and formatted. Look for extreme values
> or cases where all variables for a particular taxon are identical.
>
> Inspect the Phylogenetic Tree:
> Ensure the tree is ultrametric and well-resolved. If the tree has
> branch lengths that are zero or very small, these can lead to
> computational issues. Tools like ape::is.ultrametric(tree) can help
> verify this.
>
> Use Alternative Transformations:
> If the Z-score transformation introduces negative values that may be
> problematic, you could experiment with other normalization techniques
> (e.g., range scaling to [0,1]).
>
> Try Alternative PCA Methods:
> If the problem persists, consider using other PCA methods tailored for
> phylogenetic data, such as prcomp with pre-transformed data or robust
> PCA options in the phytools or vegan packages, depending on your data
> structure.
>
> If none of these approaches resolve the issue, sharing a reproducible
> example of your data and code could be useful?
>
>
> Best wishes
>
> Michael Zyphur
> Director
> Institute for Statistical and Data Science
> instats.org
>
>
>
> Best wishes
>
> Michael Zyphur
> Director
> Institute for Statistical and Data Science
> instats.org
>
>
> On Thu, 21 Nov 2024 at 23:08, Jovana Malikovic
> <[email protected]> wrote:
>> Dear all,
>> Can someone explain the correct way to handle this warning message?
>>
>> data.ppca <-phyl.pca(tree, data.mean)
>> data.ppca
>>
>> Warnings:
>> 1: In sqrt(V[i, i] * obj$Eval[j, j]) : NaNs were generated
>> 2: In sqrt(V[i, i] * obj$Eval[j, j]) : NaNs were generated
>> 3: In sqrt(V[i, i] * obj$Eval[j, j]) : NaNs were generated
>> 4: In sqrt(V[i, i] * obj$Eval[j, j]) : NaNs were generated
>> 5: In sqrt(V[i, i] * obj$Eval[j, j]) : NaNs were generated
>>
>>
>> I understand that this happened because of the negative values in the 
>> dataset after the Z-score transformation, but I don't know if there is a way 
>> to solve it.
>>
>> Thank you in advance,
>> Jovana
>>
>>          [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-phylo mailing list [email protected]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
>> Searchable archive athttp://www.mail-archive.com/[email protected]/
> _______________________________________________
> R-sig-phylo mailing list [email protected]
> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
> Searchable archive athttp://www.mail-archive.com/[email protected]/
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-phylo mailing list - [email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/[email protected]/

Reply via email to