vegandevs / vegan

R package for community ecologists: popular ordination methods, ecological null models & diversity analysis
https://vegandevs.github.io/vegan/
GNU General Public License v2.0
445 stars 96 forks source link

Calculate all possible permutations in restricted PERMANOVA #308

Closed yanxianl closed 5 years ago

yanxianl commented 5 years ago

Hi,

I had an experiment comparing the performance of two diets for fish. Each diet was randomly allocated to triplicate tanks each containing 90 fish. The intestinal contents of fish were collected and sequenced for the profiling of gut microbiota. As samples taken from the same tank are not independent, a nested PERMANOVA was run to compare the difference in microbial community structure. Below is a reproducible illustration of the analysis I've done.

library(vegan)
#> Loading required package: permute
#> Loading required package: lattice
#> This is vegan 2.5-4

# Make metadata
metadata <- cbind.data.frame(SampleID = seq(from = 1, to = 18, by = 1),
                             Diet = rep(c("Ref", "Soy"), each = 9),
                             Tank = rep(c("T1", "T2", "T3", "T4", "T5", "T6"), each = 3))

# Make feature table
low <- data.frame(replicate(50,sample(0:10,9,rep=TRUE)))

high <- data.frame(replicate(50,sample(0:100,9,rep=TRUE)))

table <- rbind(low, high)

# Define permutation scheme for running nested PERMANOVA
perm <- how(complete = TRUE, 
            within   = Within(type = "none"),
            plots    = with(metadata, Plots(strata = Tank, type = "free")))    

# Nested PERMANOVA
adonis2(table ~ Diet, data = metadata, permutations = perm, method="bray")
#> Set of permutations < 'minperm'. Generating entire set.
#> Permutation test for adonis under reduced model
#> Terms added sequentially (first to last)
#> Plots: Tank, plot permutation: free
#> Permutation: none
#> Number of permutations: 199
#> 
#> adonis2(formula = table ~ Diet, data = metadata, permutations = perm, method = "bray")
#>          Df SumOfSqs      R2      F Pr(>F)  
#> Diet      1   2.5831 0.73313 43.955   0.09 .
#> Residual 16   0.9403 0.26687                
#> Total    17   3.5234 1.00000                
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Created on 2019-03-12 by the reprex package (v0.2.1)

Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R version 3.5.2 (2018-12-20) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz Europe/Berlin #> date 2019-03-12 #> #> - Packages -------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.0 2017-04-11 [2] CRAN (R 3.5.1) #> backports 1.1.3 2018-12-14 [2] CRAN (R 3.5.2) #> callr 3.1.1 2018-12-21 [2] CRAN (R 3.5.2) #> cli 1.0.1 2018-09-25 [2] CRAN (R 3.5.1) #> cluster 2.0.7-1 2018-04-13 [3] CRAN (R 3.5.2) #> crayon 1.3.4 2017-09-16 [2] CRAN (R 3.5.1) #> desc 1.2.0 2018-05-01 [2] CRAN (R 3.5.1) #> devtools 2.0.1 2018-10-26 [2] CRAN (R 3.5.1) #> digest 0.6.18 2018-10-10 [2] CRAN (R 3.5.1) #> evaluate 0.13 2019-02-12 [1] CRAN (R 3.5.2) #> fs 1.2.6 2018-08-23 [2] CRAN (R 3.5.1) #> glue 1.3.0 2018-07-17 [2] CRAN (R 3.5.1) #> highr 0.7 2018-06-09 [2] CRAN (R 3.5.1) #> htmltools 0.3.6 2017-04-28 [2] CRAN (R 3.5.1) #> knitr 1.21 2018-12-10 [1] CRAN (R 3.5.2) #> lattice * 0.20-38 2018-11-04 [3] CRAN (R 3.5.2) #> magrittr 1.5 2014-11-22 [2] CRAN (R 3.5.1) #> MASS 7.3-51.1 2018-11-01 [2] CRAN (R 3.5.1) #> Matrix 1.2-15 2018-11-01 [3] CRAN (R 3.5.2) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.2) #> mgcv 1.8-26 2018-11-21 [2] CRAN (R 3.5.1) #> nlme 3.1-137 2018-04-07 [3] CRAN (R 3.5.2) #> permute * 0.9-4 2016-09-09 [2] CRAN (R 3.5.1) #> pkgbuild 1.0.2 2018-10-16 [2] CRAN (R 3.5.1) #> pkgload 1.0.2 2018-10-29 [2] CRAN (R 3.5.1) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.2) #> processx 3.2.1 2018-12-05 [2] CRAN (R 3.5.2) #> ps 1.3.0 2018-12-21 [2] CRAN (R 3.5.2) #> R6 2.4.0 2019-02-14 [1] CRAN (R 3.5.2) #> Rcpp 1.0.0 2018-11-07 [2] CRAN (R 3.5.1) #> remotes 2.0.2 2018-10-30 [2] CRAN (R 3.5.1) #> rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.2) #> rmarkdown 1.11 2018-12-08 [1] CRAN (R 3.5.2) #> rprojroot 1.3-2 2018-01-03 [2] CRAN (R 3.5.1) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 3.5.1) #> stringi 1.3.1 2019-02-13 [1] CRAN (R 3.5.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.5.2) #> testthat 2.0.1 2018-10-13 [2] CRAN (R 3.5.1) #> usethis 1.4.0 2018-08-14 [2] CRAN (R 3.5.1) #> vegan * 2.5-4 2019-02-04 [1] CRAN (R 3.5.2) #> withr 2.1.2 2018-03-15 [2] CRAN (R 3.5.1) #> xfun 0.5 2019-02-20 [1] CRAN (R 3.5.2) #> yaml 2.2.0 2018-07-25 [2] CRAN (R 3.5.1) #> #> [1] C:/R/library #> [2] \\nmbu.no/my/Home/Documents/R/win-library/3.5 #> [3] C:/Program Files/R/R-3.5.2/library ```

If I understand correctly, the number of all possible permutations under the above permutation scheme should be 6^5^4^3^2^1 = 720 (only tanks are shuffled, n = 6). However, it's only 200 which I couldn't figure out why. Did I use a wrong permutation scheme for my experiment setup?

Any helps are greatly appreciated.

Yanxian

gavinsimpson commented 5 years ago

I can't reproduce this

> adonis2(table ~ Diet, data = metadata, permutations = perm, method="bray")
Set of permutations < 'minperm'. Generating entire set.
Permutation test for adonis under reduced model
Terms added sequentially (first to last)
Plots: Tank, plot permutation: free
Permutation: none
Number of permutations: 719

adonis2(formula = table ~ Diet, data = metadata, permutations = perm, method = "bray")
         Df SumOfSqs      R2      F Pr(>F)
Diet      1   2.5381 0.70482 38.205    0.1
Residual 16   1.0630 0.29518              
Total    17   3.6011 1.00000

with

Session info ``` ─ Session info ─────────────────────────────────────────────────────────────── setting value version R version 3.5.2 Patched (2019-01-24 r76013) os Fedora 29 (Workstation Edition) system x86_64, linux-gnu ui X11 language (EN) collate en_CA.UTF-8 ctype en_CA.UTF-8 tz America/Regina date 2019-03-12 ─ Packages ─────────────────────────────────────────────────────────────────── package * version date lib source assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.1) backports 1.1.3 2018-12-14 [1] CRAN (R 3.5.1) callr 3.1.1 2018-12-21 [1] CRAN (R 3.5.1) cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.1) cluster 2.0.7-1 2018-04-13 [1] CRAN (R 3.5.2) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.1) desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.1) devtools 2.0.1 2018-10-26 [1] CRAN (R 3.5.1) digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.1) fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.1) glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.1) lattice * 0.20-38 2018-11-04 [1] CRAN (R 3.5.2) magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.1) MASS 7.3-51.1 2018-11-01 [1] CRAN (R 3.5.2) Matrix 1.2-15 2018-11-01 [1] CRAN (R 3.5.2) memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.1) mgcv 1.8-27 2019-02-06 [1] CRAN (R 3.5.2) nlme 3.1-137 2018-04-07 [1] CRAN (R 3.5.2) permute * 0.9-5 2019-03-11 [1] local pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.1) pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.1) prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.1) processx 3.2.1 2018-12-05 [1] CRAN (R 3.5.1) ps 1.3.0 2018-12-21 [1] CRAN (R 3.5.1) R6 2.4.0 2019-02-14 [1] CRAN (R 3.5.2) Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.1) remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.1) rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.1) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.1) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.1) testthat 2.0.1 2018-10-13 [1] CRAN (R 3.5.1) usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.1) vegan * 2.5-4 2019-02-04 [1] CRAN (R 3.5.2) withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.1 ```

but this might be due to my running a slightly updated version of permute which is making it's way on to CRAN now (it has been approved, but I don't think Windows builds are available just yet).

gavinsimpson commented 5 years ago

Confirming that this was fixed in permute many months ago https://github.com/gavinsimpson/permute/issues/23 but I didn't push a new version to CRAN at that time. A new version (0.9-5) went to CRAN yesterday and has been accepted which included this fix, which is why I couldn't reproduce the described problem with this version installed locally.

@yanxianl Once you update permute to 0.9-5 the problem you reported will go away. Apologies for the confusion over this.

yanxianl commented 5 years ago

Hi Gavin, updating permute package to 0.9-5 solved the problem. Thanks!