vegandevs/vegan

adonis2, multiple level factor

Closed this issue · 9 comments

Im sorry if this question was posted early, I swear I spent a long time searching the issue page but couldn't find anything.

I'm running adonis2 on a series of factors, and most are binomial (eg. yes/no, High/Low), so far so good, thanks for this!
A couple of my variables are split into multiple levels (eg. "Age.groups" has three levels: 18-35, 36-55, and 56-75).
If I run adonis2 as:

adonis2(bCurtis_distances ~ sample_data(phyloseq)$Age.group)

I get

Permutation test for adonis under reduced model
Terms added sequentially (first to last)
Permutation: free
Number of permutations: 999

adonis2(formula = bCurtis_distances ~ sample_data(phyloseq)$Age.group)
                            Df SumOfSqs      R2      F Pr(>F)   
sample_data(data)$Age.group  2   0.7952 0.04983 1.4945   0.01 **
Residual                    57  15.1643 0.95017                 
Total                       59  15.9595 1.00000                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Is there a way to get contrast statistics for each level of my Age.group variable against each other?

Martin

You cannot have contrasts against all n(n – 1)/2 pairs of levels, but you can have standard R contrasts with keyword by = "onedf" in current github version of vegan.

@jarioksa
I have vegan 2.6-2 (latest version), but adding by = "onedf" returns:

Error in match.arg(by, c("terms", "margin")) : 
  'arg' should be one of “terms”, “margin”

Now i checked the R page documentation and indeed you're right, the arguments for "by" should be "terms", "margin" AND "onedf", but the error message I received says otherwise... any ideas?

Appreciate your help!

Martin

It is only in github at the moment: version should vegan 2.6-3. We are planning a release, but at the moment it is only in github.

sorry to keep writing on a closed issue: when you say it's only on github, where exactly can i find the 2.6-3 version and how do i get it into my RStudio?

Martin

I'll add instructions to the README, but buyer beware installing development versions of software (though less of a problem right now as we are nearing release). Basically what Jari means is the current version of the source code on GitHub (it happens to 2.6.3 if you look at the DESCRIPTION).

So, absent any other instructions we expect you to

  1. be able to install packages from source (you'd need to have all the required build tools needed to build packages on your OS),
  2. know how to either clone a repo locally or that you can download the entire repo as a zip from the big green Code button on the repo home page.
  3. know how to build and install the sources you downloaded or cloned.

This isn't easy, but 2 and 3 can be solved if you use the remotes package, as in:

install.packages("remotes")
remotes::install_github("vegandevs/vegan")

This doesn't avoid the problem that you need to be able to compile packages from sources and for that you need developer build tools.

I'll see about activating our repo on R Universe, which is a nice build tool provided by rOpenSci and which would allow you to not know anything about steps 1-3 at all, but you need to tell R to install from their package repository (which I'll explain in the README once I get it activated).

Also; you need to get this working in R, not RStudio - these aren't the same thing.

I'll circle back with a link to the specific instructions once I get the R Universe up and running for vegan.

Actually, you only need to get the new adonis2. However, here is a way to avoid the pain of installation and trick the current adonis2 give you the contrasts. The trick is to make the model.matrix, turn it to a data.frame and use this in your model:

> library(vegan)
Loading required package: permute
Loading required package: lattice
This is vegan 2.6-2
> data(dune, dune.env)
> x <- as.data.frame(model.matrix(~ Moisture, dune.env))
> adonis2(dune ~ ., x, by="terms")
Permutation test for adonis under reduced model
Terms added sequentially (first to last)
Permutation: free
Number of permutations: 999

adonis2(formula = dune ~ ., data = x, by = "terms")
           Df SumOfSqs      R2      F Pr(>F)    
Moisture.L  1   1.3782 0.32059 8.5775  0.001 ***
Moisture.Q  1   0.1443 0.03356 0.8980  0.466    
Moisture.C  1   0.2057 0.04784 1.2800  0.285    
Residual   16   2.5709 0.59801                  
Total      19   4.2990 1.00000                  
---
Signif. codes:  0***0.001**0.01*0.05.0.1 ‘ ’ 1

Thanks Jari!
I actually found the new adonis2 function here on GitHub (/R/adonis.R) and tried to just copy that in, assign a new name to the function and run it with the new name, however i get
Error in initDBRDA(lhs) : could not find function "initDBRDA"
So I'm guessing adonis is not the only new thing in vegan 2.3-6. I'll wait till you guys release it then.

In the meantime, I'll try what you suggested

Martin

@mawa86 I think there is no thing to use new adonis2, but you must have it in the vegan NAMESPACE or it cannot find other vegan functions (such as initDBRDA). Basically, you need to do:

environment(adonis2) <- environment(cca) # cca or any other old vegan function
assignInNamespace("adonis2", adonis2, "vegan") # this may not be needed, but to be sure...

You can just install the development version now using instructions in the README - maybe @jarioksa had already turned on the R Universe for vegandevs? but it's active regardless. For the record here's how to do an install from there:

# Enable repository from vegandevs
options(repos = c(
  vegandevs = 'https://vegandevs.r-universe.dev',
  CRAN = 'https://cloud.r-project.org'))
# Download and install vegan in R
install.packages('vegan')