--- title: "Statistical Tests" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Statistical Tests} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4.5 ) ``` ## Introduction An ordination plot can suggest that groups of samples differ. ecodive's permutation tests check whether the difference is real: does a variable, such as treatment or body site, explain the distances between samples? Each test takes a distance matrix (`dm`) from any beta diversity metric, plus the variable to test: either a vector with one value per sample, or the name of a column in a data frame of sample metadata (`data`). | Function | Test | Variable | Question | | :------------ | :------------------------------------------------ | :---------- | :------------------------------------------------------------ | | `permanova()` | Permutational Multivariate Analysis of Variance | either | Do group centroids differ? How much variance (R²) does it explain? | | `permdisp()` | Homogeneity of Multivariate Dispersions | categorical | Do groups differ in their spread? | | `anosim()` | Analysis of Similarities | categorical | Are distances within groups smaller than between them (ranks)? | | `mrpp()` | Multi-Response Permutation Procedure | categorical | Are distances within groups smaller than expected? | | `mantel()` | Mantel Test | numeric | Do distances correlate with differences in a numeric variable, or with another distance matrix? | | `mantel_partial()` | Partial Mantel Test | numeric | Do they still, after removing the effect of a third distance matrix? | They give the same statistics as `vegan`'s `adonis2()`, `betadisper()`, `anosim()`, `mrpp()`, `mantel()`, and `mantel.partial()`, with permutations run in parallel C, and the p-values are identical for any number of threads. The examples use R's built-in `iris` data, as in `vignette('ordination')`: 150 flowers, four measurements each, and three species. ```{r} library(ecodive) dm <- bray(iris[, 1:4]) ``` ## PERMANOVA `permanova()` is the most widely used test. Give the grouping variable as a column name of `data`, or as a vector. ```{r} res <- permanova(dm, 'Species', iris) res ``` Species explain most of the variation between flowers (`R2`), and no permutation of the labels did as well (`p`). The p-value cannot be smaller than 1 / (`permutations` + 1), so use more permutations for a more precise one. `plot()` shows the pseudo-\(F\) of every permutation, and the observed one far beyond them: ```{r} plot(res) ``` A `formula` tests several variables at once. `by = 'terms'` tests each in turn, as `adonis2()` does, and `strata` restricts permutations to within blocks (such as subjects in a repeated-measures design). ```{r} permanova(dm, data = iris, formula = ~ Species + Sepal.Width, by = 'terms') ``` ## Checking the spread PERMANOVA can also give a small p-value when groups have the same center but different spreads. `permdisp()` tests the spread on its own; a significant result means a PERMANOVA difference may come partly or wholly from dispersion. ```{r} res <- permdisp(dm, 'Species', iris) res plot(res) ``` Here the three species are equally spread, so the PERMANOVA result above reflects a real difference in their centers. The warning comes from Bray-Curtis distances not being Euclidean: a flower can then sit at a negative squared distance from its species' center, which `permdisp()` sets to zero (Anderson 2006). ## ANOSIM and MRPP `anosim()` compares the ranks of distances within and between groups. `R` near 1 means every group is distinct, and near 0 means no difference. `mrpp()` compares the average distance within groups with what random groups would give. ```{r} anosim(dm, 'Species', iris) res <- mrpp(dm, 'Species', iris) res ``` `plot()` of an MRPP result shows each group's mean within-group distance, with their weighted mean (the observed \(\delta\)) and the mean of all distances (the \(\delta\) expected by chance); `type = 'null'` shows the permutations instead, as for the other tests. ```{r} plot(res) ``` ## Pairwise comparisons When an overall test finds that the groups differ, the next question is usually which ones. `pairwise = TRUE` reruns the test on each pair of groups' samples, and adjusts the pairs' p-values for multiple testing (Holm's method by default; `p_adjust = 'BH'` controls the false discovery rate instead). The overall result is unchanged. ```{r} res <- permanova(dm, 'Species', iris, pairwise = TRUE) res$pairwise ``` `p_value` is each pair's own permutation p-value, and `p_adjusted` the value to report. `anosim()` and `mrpp()` take the same arguments, and `permdisp()` adds adjusted p-values to its pairwise t tests: ```{r} permdisp(dm, 'Species', iris, pairwise = TRUE)$pairwise ``` ## Mantel test `mantel()` correlates `dm` with a numeric variable, or with a second distance matrix: here, whether flowers with similar petals also have similar sepals. `method` can be `'pearson'`, `'spearman'`, or `'kendall'`. ```{r} sepals <- bray(iris[, 1:2]) petals <- bray(iris[, 3:4]) res <- mantel(sepals, petals, method = 'spearman') res plot(res) ``` `mantel_partial()` removes the effect of a third set of distances, `given`: here, whether sepals and petals still match after accounting for petal width alone. ```{r} mantel_partial(sepals, petals, given = 'Petal.Width', data = iris, method = 'spearman') ``` ## Running tests by name `dist_test()` runs any test by name, as `dist_ord()` does for ordinations. It checks that the variable suits the test (categorical or numeric), and names are matched forgivingly: `'adonis2'` finds `permanova` and `'betadisper'` finds `permdisp`. ```{r} dist_test(dm, 'adonis2', 'Species', iris) ``` Each result converts to a one-row data frame with the same columns, so several tests combine into one table. ```{r} tests <- list_dist_tests(val = 'id', groups = 'categorical') do.call(rbind, lapply(tests, function (test) as.data.frame(dist_test(dm, test, 'Species', iris)) )) ``` ## Reproducibility Permutations come from ecodive's own random number generator, set by `seed` (default `0`), so the same call always gives the same p-value, on any computer and with any number of `cpus`. R's `set.seed()` has no effect. ```{r} identical( permanova(dm, 'Species', iris, seed = 1, cpus = 1), permanova(dm, 'Species', iris, seed = 1, cpus = 2) ) ```