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

Error Adonis (New error after reinstalling Phyloseq), Error in colnames<- #1457

Open
DrMaestre opened this issue Apr 8, 2021 · 21 comments
Open

Comments

@DrMaestre
Copy link

Hi, I had to reinstall my R and Phyloseq and now I am getting this error when trying to implement adonis. I have been googling it without any success. Any help will be much appreciated. Thanks!

data(GlobalPatterns)
metadata <- as(sample_data(GlobalPatterns), "data.frame")
dist.uf <- phyloseq::distance(GlobalPatterns, method = "unifrac")
ps.adonis <- adonis(dist.uf ~ SampleType, data = metadata, perm=9999)
Error in colnames<-(*tmp*, value = colnames(lhs)) :
attempt to set 'colnames' on an object with less than two dimensions

R version 3.6.3
packageVersion("phyloseq")
[1] ‘1.30.0’

@ycl6
Copy link

ycl6 commented Apr 10, 2021

@DrMaestre I didn't encounter this error.
What's the version of your package vegan (which is where the adonis function is from)?

I tested it with:

library(phyloseq)
library(vegan)
other attached packages:
[1] vegan_2.5-6     lattice_0.20-41 permute_0.9-5   phyloseq_1.30.0

@DrMaestre
Copy link
Author

DrMaestre commented Apr 10, 2021 via email

@ajverster
Copy link

Hi,

I am having this same problem when I didn't used to. I tried to run the exact same code and got the exact same error. DrMaestre, what do you mean by transpose the matrix? For the record I have phyloseq=1.32.0 and vegan 2.5.7

@DrMaestre
Copy link
Author

DrMaestre commented May 6, 2021 via email

@lkoest12
Copy link

@DrMaestre,

I noticed this problem as well, and adjusting to Adonis2 solved it for me. But now, I can no longer run @pmartinezarbizu without it throwing the same error.

I did some digging, and found that pmartinezarbizu included a pairwise.adonis2() function that allows a user to include strata, but found that it is still using adonis() as its base (as opposed to adonis2).

Does anyone have an explanation for why we had to switch to using Adonis2 in phyloseq? Additionally, has anyone found an alternative to pairwise comparisons of groups with this new version?

Help would be greatly appreciated!

-Luke

@pmartinezarbizu
Copy link

HI, sorry to drop into your conversation. in pairwise.adonis I kept the link to adonis instead of adonis2, also in pairwise.adonis2().
The code of adonis2 was re-written from scrath and uses a different format in the output that makes my wrapper incompatible. I could re-writte pairwise.adonis, to use adonis2 but did not find the time to do so.
Can somebody tell me the error when using pairwise.adonis2, so I can look at it.
pedro

@lkoest12
Copy link

lkoest12 commented Jul 1, 2021

Good morning Pedro and thanks for responding so quickly!

When I run the original pairwise.adonis command, I recieve the following error:
Error in colnames<-(*tmp*, value = colnames(lhs)) :
attempt to set 'colnames' on an object with less than two dimensions

Which is the same error I get when trying to run the original adonis command for PERMANOVA.

If i switch to the adonis2 command, the PERMANOVA analysis runs smoothly. But If I switch to the pairiwse.adonis2 command, I recieve the following error:
Error in x1[[2]] <- NULL : replacement has length zero

I don't think it has anything to do with the pairwise.adonis2 command, but rather having to use the adonis2 command for the phyloseq data. I am using the command as follows:

'pairwise_adonis_results <- pairwise.adonis(BRAY_CURTIS_DIST_MATRIX, sample_data(PHYLOSEQ_OBJECT)$VARIABLE)'

The phyloseq object contains an otu table, metadata and taxonomy component.

Do you have any thoughts? I am a huge fan of the pairwise.adonis command and would love to keep using it!

-Luke

@pmartinezarbizu
Copy link

HI @lkoest12 ,
lets try to get trough this together.

You need to make sure that the argument passed to pairwise.adonis is a column of a data.frame or a vector of values.

In pairwise.adonis2 the rhs (right-hand side) argument of the formula is a column name of a data.frame which is specify in data= your.data.frame.
So you could try instead of
sample_data(PHYLOSEQ_OBJECT)$VARIABLE)

something like
as.data.frame( sample_data(PHYLOSEQ_OBJECT)$VARIABLE))

or
pairwise_adonis_results <- pairwise.adonis2(BRAY_CURTIS_DIST_MATRIX ~VARIABLE, data= data.frame( VARIABLE = sample_data(PHYLOSEQ_OBJECT)$VARIABLE) )

Pedro

@pmartinezarbizu
Copy link

So inspect the output of
sample_data(PHYLOSEQ_OBJECT)$VARIABLE)
and make sure that is is compatible with a vector of factors.
pe

@lkoest12
Copy link

lkoest12 commented Jul 1, 2021

OK!

I tried out the new lines, and they didn't work. I see what you were thinking though, so I converted the metadata portion of the phyloseq object to data.frame and tried that as well. No dice.

I took some screenshots and wrote a csv of my data frame to share. What I find most interesting is that the original adonis() command no longer works with this type of data either. As @DrMaestre mentioned above, when he switched to adonis2() that fixed the issue. Could it be a disconnect with vegan and phyloseq causing this? I am not entirely sure what the difference between adonis() and adonis2() is, nor do I understand why the original no longer works.

In your examples online for pairwise.adonis() I don't think you use any distance matrices in your examples. I did run through your examples and it worked fine. My distance object is considered a "dist" value and cannot be converted to a data.frame. I can't really look at the structure to compare (probably the lack of skills as a coder).

I can pass some code and data for a reprex if that would help!

adonis_code

###################################################################################################

bray_curtis_distance_matrix

data_sub_type_df_pairwise_troubleshoot.xlsx

@lkoest12
Copy link

lkoest12 commented Jul 8, 2021

Hello Pedro @pmartinezarbizu,

I was wondering if you had a chance to take a look at this. I know you're pretty busy, so no worries if you haven't had time yet. Also it was a holiday weekend in the US, too.

I tried looking through some of the documentation describing the change between adonis and adonis2, but it seems somewhat vague to me. The Rdocumentation page states that both adonis and adonis2 commands will accept bray-curtis dissimilarity matrices, so I am a bit confused as to why the latest updates broke the link between adonis and phyloseq. I found a thread about how phyloseq:distance and vegdist commands are not quite the same thing. But this was from 2016, and my code was working up until the update.

@ARBramucci
Copy link

I am also having the same issue.

@lkoest12
Copy link

Hello everyone,

I may have found the problem for both adonis and pairwise.adonis. I think it may have to do with the functions that the package "DescTools" are masking. I found that the following example used by Chris still seemed to work fine:

https://chrischizinski.github.io/rstats/adonis/

So I matched all of his inputs and still had trouble. I then just loaded only the packages that I needed for the specific analysis, and it worked again. Then it was just narrowing it down from there. This fixed the original Adonis command too, but it seems that Vegan is preparing to deprecate the original.

Try it again without loading DescTools.

packageVersion("phyloseq")
[1] ‘1.36.0’
packageVersion("Vegan")
[1] ‘2.5.7’
packageVersion("pairwiseadonis")
[1] ‘0.0.1’

@pmartinezarbizu @ARBramucci @DrMaestre

@jeremie-bf
Copy link

Hi,

I also had the same problem (Error in colnames<-(*tmp*, value = colnames(lhs)) :
attempt to set 'colnames' on an object with less than two dimensions
), but converting my dist object (Bray-Curtis dissimilarity matrix from vegdist) to a matrix with the as.matrix function seems to solve it.

That being said, I have no idea why it is working, since the documentation specify that the LHS should be a "community matrix or dissimilarity matrix (eg. from vegdist or dist)".

I don't know if I'm breaking any computational laws that I don't know of, but for now it seems to be working for me.

If it's not legit, please let me know.

If it is, I hope it helps.

@pmartinezarbizu
Copy link

HI @lkoest12 you sent your file with the factors data_subtype...xlsx
I was trying to reproduce your error. Can you please also send me data_bray?
Best would be if you save data_bray as an R object, so we mantain the class.

Note, that in your example you are using the wrong sintax

for pairwise.adonis use "matrix , factors"

for pairwise.adonis2 use "matrix ~ factors, data = data"

You are using the oposit way.

So
pairwise.adonis(iris[,1:4] , iris$Species)

pairwise.adonis2(iris[,1:4] ~ Species,data=iris)

Regards PEdro
.

@JFMSilva
Copy link

JFMSilva commented Aug 9, 2021

The error appears when using Vegan function adonis with the package "DescTools" loaded.

This code works:
library(pairwiseAdonis)
data(iris)
x <- vegdist(iris[,1:4])
factors <- iris$Species
pairwise.adonis(x, factors)

Code from pairwise.adonis:
elem <- 1
co <- combn(unique(as.character(factors)), 2)
x1 <- as.matrix(x)[factors %in% c(as.character(co[1, elem]), as.character(co[2, elem])), factors %in% c(as.character(co[1, elem]), as.character(co[2, elem]))]
tmp <- factors[factors %in% c(co[1, elem], co[2, elem])]
adonis(x1 ~ tmp, permutations = 99)

But when loading "DescTools" the error appears:
library(DescTools)
adonis(x1 ~ tmp, permutations = 99)

Strangely, if I unname the matrix, adonis works again:
adonis(unname(x1) ~ tmp, permutations = 99)

Session info:
R version 4.1.0 (2021-05-18) other attached packages: DescTools_0.99.42 pairwiseAdonis_0.0.1 cluster_2.1.2 vegan_2.5-7 lattice_0.20-44 permute_0.9-5

  • Edit:
    I found about this error when using rcompanion::cldList to calculate a compact letter display and it imports DescTools package:
    Result <- pairwise.adonis(x, factors)
    rcompanion::cldList(comparison = gsub(x = Result$pairs, pattern = " vs ", replacement = '-'), p.value = Result$p.adjusted, threshold = 0.05)

  • Edit 2:
    For anyone having this error, you don't even have to load the package, just having a command like this one DescTools::Abind() anywhere in the script, or even in another open script, will cause pairwise.adonis to stop working. @pmartinezarbizu, you can test with this script, just save the content in a file and open it with R-studio:
    library(pairwiseAdonis)
    data(iris)
    x <- vegdist(iris[,1:4])
    factors <- iris$Species
    pairwise.adonis(x, factors)
    DescTools::Abind()

This happens because R-studio loads the package via namespace automatically when opening scripts, as can be seen in sessionInfo() and is been discussed at #8072

@pmartinezarbizu
Copy link

Hi @JFMSilva thank you for the post,
indeed I could reproduce the error loading DescTools. It is a complex error and i dont know where to start. The error comes with DescTools in combination with adonis() but as far I could explore not with adonis2().
I have been able to change the pairwise.adonis2 function tom use adonis2 instead of adonis and this seems to overcome the problem. But if I change pairwise.adonis to use adonis2 then I get a new error.

Error in eval(predvars, data, env) : Object 'factors' not found

I dont understand were this is comming from, vegan or DescTools

I will further inpect this.
Pedro

@JFMSilva
Copy link

I take a look at the adonis function, and find out why it fails when LHS of the formula is a matrix. The error occurs in line 65 of the function:
colnames(beta.spp) <- colnames(lhs)
because adonis expects that both objects are NULL in this situation, but DescTools changes a dist object to a named dist object when subsetting rows.
tmp <- iris[1:50, 1:4]
str(dist(tmp)) # there is a Labels attribute, but its not returned when asking for col or rownames

'dist' num [1:1225] 0.539 0.51 0.648 0.141 0.616 ...

  • attr(*, "Size")= int 50
  • attr(*, "Labels")= chr [1:50] "1" "2" "3" "4" ...
  • attr(*, "Diag")= logi FALSE
  • attr(*, "Upper")= logi FALSE
  • attr(*, "method")= chr "euclidean"
  • attr(*, "call")= language dist(x = tmp)

library(DescTools)
str(dist(tmp)) # now it returns as colnames

'dist' Named num [1:1225] 0.539 0.51 0.648 0.141 0.616 ...

  • attr(*, "Size")= int 50
  • attr(*, "Labels")= chr [1:50] "1" "2" "3" "4" ...
  • attr(*, "Diag")= logi FALSE
  • attr(*, "Upper")= logi FALSE
  • attr(*, "method")= chr "euclidean"
  • attr(*, "call")= language dist(x = tmp)

# whithout row subset
str(dist(iris[, 1:4]))

'dist' num [1:11175] 0.539 0.51 0.648 0.141 0.616 ...

  • attr(*, "Size")= int 150
  • attr(*, "Diag")= logi FALSE
  • attr(*, "Upper")= logi FALSE
  • attr(*, "method")= chr "euclidean"
  • attr(*, "call")= language dist(x = iris[, 1:4])

# this returns to a simple dist object
rownames(tmp) <- NULL
str(dist(tmp))

'dist' num [1:1225] 0.539 0.51 0.648 0.141 0.616 ...

  • attr(*, "Size")= int 50
  • attr(*, "Diag")= logi FALSE
  • attr(*, "Upper")= logi FALSE
  • attr(*, "method")= chr "euclidean"
  • attr(*, "call")= language dist(x = tmp)

I also looked up the sequence of packages imports to find the real culprit: DescTools > gld > e1071 > proxy, and sure enough, package proxy maskes as.dist and dist from stats and as.matrix from base.

@pmartinezarbizu
Copy link

Thank you @JFMSilva for your work, this is very informative.
I will inspect if just adding unname(x1) at the appropiate place will overcome the problem. Is not elegant but pragmatic.

"package proxy maskes as.dist and dist from stats and as.matrix from base..." OHG how brave one should be to mask functions from stats and base!

@pmartinezarbizu
Copy link

I have updated both pairwise.adonis and pairwise.adonis2. They problem shoudl be solved now and you can close the issue.
Please check and feedback. Pedro

@katemunnik
Copy link

katemunnik commented Oct 19, 2021

Thanks to all of you for your comments. I had the same, very frustrating issue @lkoest12 , and mine was also because 'DescTools' snuck in with rcompanion::cldList - just as it had for @JFMSilva .
Thanks to your help - I'm now using unname() for my dist.matrix in adonis (have not had to switch to adonis2), and this is working well. Also with @pmartinezarbizu 's updates I can now use pairwise.adonis (without unname for my dist.matrix) and its working well too.

I still need to use rcompanion::cldList and these changes have allowed the rest of my workflow to remain unchanged.
Thanks so much!

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

9 participants