Analysing the distance decay of community similarity in river networks using Bayesian methods

The distance decay of community similarity (DDCS) is a pattern that is widely observed in terrestrial and aquatic environments. Niche-based theories argue that species are sorted in space according to their ability to adapt to new environmental conditions. The ecological neutral theory argues that community similarity decays due to ecological drift. The continuum hypothesis provides an intermediate perspective between niche-based theories and the neutral theory, arguing that niche and neutral factors are at the opposite ends of a continuum that ranges from competitive to stochastic exclusion. We assessed the association between niche-based and neutral factors and changes in community similarity measured by Sorensen’s index in riparian plant communities. We assessed the importance of neutral processes using network distances and flow connection and of niche-based processes using Strahler order differences and precipitation differences. We used a hierarchical Bayesian approach to determine which perspective is best supported by the results. We used dataset composed of 338 vegetation censuses from eleven river basins in continental Portugal. We observed that changes in Sorensen indices were associated with network distance, flow connection, Strahler order difference and precipitation difference but to different degrees. The results suggest that community similarity changes are associated with environmental and neutral factors, supporting the continuum hypothesis.


Introduction
In this document we include the R code for reproducing the analysis presented in "Distance decay of community similarity in river networks: a Bayesian approach". Make sure to download "data.csv", "model.stan" and "stan_utility.R".

Dataset
Then, we load the dataset.

Data preparation
We need to filter observations for which the ratio between the network distance and the euclidean distance is <= 2.

Model validation
5.5.1 Run diagnostics We validated the model by checking 1) transitions that ended with a divergence, 2) transitions that ended prematurely due to maximum tree depth limit, 3) the energy Bayesian fraction of missing information (E-BFMI), 4) the effective sample size per iteration and 5) the potential scale reduction factors.

Posterior retrodictive checks
We plotted predicted Sorensen indices and residuals against observed Sorensen indices and covariates, and looked for systematic deviations in the plots. We start by preparing the required data for running the posterior retrodictive checks.

Residuals and predicted Sorensen indices against network distance
This plot shows 1) Residuals (dark blue points) and its distribution (light blue lines) plotted against network distance, and 2) Predicted Sorenson indices (dark blue points) and its distribution (light blue lines) against network distance.

Residuals and predicted Sorensen indices against precipitation difference
This plot shows 1) Residuals (dark blue points) and its distribution (light blue lines) plotted against precipitation difference, and 2) Predicted Sorensen indices (dark blue points) and its distribution (light blue lines) against precipitation difference.

Predicted Sorensen indices
We see no systematic deviations.

Residuals and predicted Sorensen indices against sample intercepts
This plot shows 1) Residuals (dark blue points) and its distribution (light blue lines) plotted against basin sample intercepts (1 and 2), and 2) Predicted Sorenson indices (dark blue points) and its distribution (light blue lines) against sample intercepts (1 and 2).

Strahler order difference
## Scale for y is already present. Adding another scale for y , which will ## replace the existing scale. Strahler order difference slopes