Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

betadisper throws an error when all distances are zero #372

Open
FVFaleiro opened this issue Jul 15, 2020 · 2 comments
Open

betadisper throws an error when all distances are zero #372

FVFaleiro opened this issue Jul 15, 2020 · 2 comments

Comments

@FVFaleiro
Copy link

Dear all,
the betadisper crash when all values from the distance matrix is equal to zero.
I did not find where is the error, but I think the function could return zero in this case as do not have dispersion in the data (see code below).

Reproducible example

x <- data.frame(sp1 = rep(1, 10), sp2 =  rep(2, 10), sp3 = rep(3, 10), sp4 = rep(4, 10))
g <- as.factor(c(rep("a", 5), rep("b", 5)))
d <- dist(x)
betadisper(d, g)
# Error in dimnames(x) <- dn : 
#  length of 'dimnames' [2] not equal to array extent

Possible solution

if (length(unique(d)) == 1 & unique(d) == 0)  {
  centroids <- rep(0, length(unique(group)))
  group.distances <- rep(0, length(unique(group)))
}
@gavinsimpson gavinsimpson changed the title betadisper crash when all distances are zero betadisper throws an error when all distances are zero Jul 15, 2020
@jarioksa
Copy link
Contributor

jarioksa commented Jul 16, 2020

There are several other functions that also throw an error. I have now made minimal fixes to betadisper, capscale, isomap, pcnm, wcmdscale and also scores.default. All these had exactly the same problem: trying to set names to dimensions that did not exist. Now these no longer throw an error, but return these results are zero or NULL. However, in most cases you cannot do anything with these results, but you will probably get unrelated errors if you want, say, plot the results. Actually, I think that throwing an error was a legitimate thing to do: it was an error to use all-zero distances.

In one case there really is a crash situation: monoMDS (and hence metaMDS) fail if all your dissimilarities are zero. Then the function just hangs in compiled code, and you cannot interrupt the function but you have to kill R. I haven't fixed this yet, but I'll do it soon.

@jarioksa
Copy link
Contributor

I have now fixed all issue that I found in branch 0-dist. I haven't merged this yet: please have a look this ( @gavinsimpson). My fix is minimal, and if you want to have more extensive fixes, you can work on your own branch or work on that 0-dist branch before merging it to master.

jarioksa added a commit that referenced this issue Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants