david-barnett / microViz

R package for microbiome data visualization and statistics. Uses phyloseq, vegan and the tidyverse. Docker image available.
https://david-barnett.github.io/microViz/
GNU General Public License v3.0
106 stars 11 forks source link

Error in if (!all(x >= 0)) stop("Negative distances not supported!") : #60

Closed lcamVz closed 2 years ago

lcamVz commented 2 years ago

I am getting this issue am I missing something?

> clean_Bacteria %>% 
+   tax_fix() %>% 
+   #ps_arrange("Treatments") %>% 
+   tax_agg("Class") %>% 
+   cor_heatmap(vars = c("p_h", "om", "t_o_c", "c_n_ratio", "nitrate_n_ppm", "clay"),
+               cor = "spearman")

Error in if (!all(x >= 0)) stop("Negative distances not supported!") : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In stats::cor(x = meta_mat, y = otu_mat, use = cor_use, method = cor) :
  the standard deviation is zero
GEbiotech commented 2 years ago

Hi, Did you find a solution for this issue? I got exactly the same error on my metagenomic dataset.

In the cor_heatmap function it happens at this step :

ser <- mat_seriate(mat = cor_mat, method = seriation_method, dist = seriation_dist, col_method = seriation_method_col, col_dist = seriation_dist_col)

Here is my variables values : method = 'OLO_ward' dist = 'euclidian' col_method='OLO_ward' seriation_dist_col='euclidian'

I guess I need to change this kind of parameters?

Thank you

Best regards

david-barnett commented 2 years ago

Based on the warning I believe this issue is arising where you have either variables and/or taxa with zero variance

In addition: Warning message:
In stats::cor(x = meta_mat, y = otu_mat, use = cor_use, method = cor) :
  the standard deviation is zero

For now, to work around this you will need to assess which variable(s) has/have zero variance, and not include that. If none of the variables have zero variance, then you might still have taxa in your dataset which are not present in any samples. tax_filter would help you with this.

You might also suppress the sorting of the heatmap by setting cor_heatmap(..., seriation_method = "Identity")


I will be having a look at improving cor_heatmap fairly soon, and plan to make the error messaging clearer.

A simple example to exemplify the underlying problem.

> cor(c(1,1,1,1,1,1,1), c(0,1,2,3,4,5,6))
[1] NA
Warning message:
In cor(c(1, 1, 1, 1, 1, 1, 1), c(0, 1, 2, 3, 4, 5, 6)) :
  the standard deviation is zero

The NA values returned by cor then are fed into a seriation method to sort the rows/cols which fails with the confusing error message.

david-barnett commented 2 years ago

update: a more informative error should soon be thrown about NAs when the next version of the seriation package is released https://github.com/mhahsler/seriation/pull/16