FelixMay / spatialbiodiv

Calculate Spatial Biodiversity Patterns
GNU General Public License v3.0
0 stars 2 forks source link
library(spatialbiodiv)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
#> ✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
#> ✔ tibble  3.1.8      ✔ dplyr   1.0.10
#> ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
#> ✔ readr   2.1.3      ✔ forcats 0.5.2 
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
library(vegan)
#> Lade nötiges Paket: permute
#> Lade nötiges Paket: lattice
#> This is vegan 2.6-4
library(scam)
#> Lade nötiges Paket: mgcv
#> Lade nötiges Paket: nlme
#> 
#> Attache Paket: 'nlme'
#> 
#> Das folgende Objekt ist maskiert 'package:dplyr':
#> 
#>     collapse
#> 
#> This is mgcv 1.8-41. For overview type 'help("mgcv-package")'.
#> This is scam 1.2-13.

spatialbiodiv

The goal of spatialbiodiv is to calculate spatial and sample-based species rarefaction curves (sSBR) and distance-decay of similarity curve from spatially-explicit biodiversity data (i.e.) species presences-absences or abundances from standardized samples in landscapes.

Installation

You can install the development version of spatialbiodiv from GitHub with:

# install.packages("devtools")
# devtools::install_github("FelixMay/spatialbiodiv", build_vignettes = TRUE)

Example 1

For an example application see the package vignette “Test analysis of simulated data”:

library(spatialbiodiv)
browseVignettes("spatialbiodiv")
#> Keine Vignetten gefunden durch browseVignettes("spatialbiodiv")

Example 2: Get differences between distance-based curves

Read and prepare the data

The data was generated by a dynamic and spatial model for community dynamics in fragmented landscapes. Here, we just extract two scenarios that only differ in their degree of fragmentation per se.

simdat1 <- read.table("data_raw/GeDo_test5_rep_10_output_sample.txt", header = TRUE,
                      sep = ",")
simdat1 <- simdat1 %>% select(-X)

Filter high and low fragmentation scenarios

frag_low <- simdat1 %>%
  filter(step == 31 & fragmentation == 0.1)

frag_high <- simdat1 %>%
  filter(step == 31 & fragmentation == 0.5)

Prepare data for spatial biodiversity curves

Get abundance data tables with rows in samples and species in columns

frag01_spec <- frag_low %>%
  select(sp_1:sp_200)

frag05_spec <- frag_high %>%
  select(sp_1:sp_200)

Get spatial coordinates of the samples

xy_frag01 <-  frag_low %>%
  select(loc_x, loc_y)

xy_frag05 <-  frag_high %>%
  select(loc_x, loc_y)

Define distances fo sSBR evaluation

We need to define distances for the evaluation of the sSBR curves. For this, we find the maximum distances in both data sets and take the smaller value of these maxima as maximum distances for the comparison of the sSBR curves.

dist_frag_low <- dist(xy_frag01)
dist_frag_high <- dist(xy_frag05)
min_dist <- min(c(max(dist_frag_low), max(dist_frag_high)))

For interpolation we need the same distance-values for both curves

new_dist <- data.frame(distance = seq(0, min_dist, length = 100))

Calculate spatial sample-based rarefaction curves (sSBR)

Actually the code for sSBR was adopted from the corresponding curves in mobr. The only difference is that mobr has the cumulative sampling effort on the x-axis, while here cumulative nearest neighbour distances are on the x-axis.

sSBR_frag01 <- sSBR(comm = frag01_spec, xy_coords = xy_frag01, distvec = new_dist)
sSBR_frag05 <- sSBR(comm = frag05_spec, xy_coords = xy_frag05, distvec = new_dist)

Create dataframe for plotting with ggplot

sSBR_frag01$sSBR_data$fragmentation <- "Low"
sSBR_frag01$sSBR_smooth$fragmentation <- "Low"

sSBR_frag05$sSBR_data$fragmentation <- "High"
sSBR_frag05$sSBR_smooth$fragmentation <- "High"

sSBR_data <- bind_rows(sSBR_frag01$sSBR_data, sSBR_frag05$sSBR_data)
sSBR_smooth <- bind_rows(sSBR_frag01$sSBR_smooth, sSBR_frag05$sSBR_smooth)

Plot the sSBR curves

ggplot(sSBR_data, aes(distance, S, group = interaction(id, fragmentation),
                      color = fragmentation)) +
  geom_line() +
  geom_line(aes(distance, S, color = fragmentation, group = fragmentation),
            data = sSBR_smooth, size = 2) +
  geom_ribbon(aes(x = distance, y = S, ymin = S_low, ymax = S_high,
                  fill = fragmentation, group = fragmentation), color = "black",
              data = sSBR_smooth, alpha = 0.2)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.

Calculate difference between the curves

Now, our key question if the two curves differ significantly. To answer this, we first need to calculate the difference between the two curves.

new_dist$diff_sSBR <- sSBR_frag05$sSBR_smooth$S - sSBR_frag01$sSBR_smooth$S
ggplot(new_dist, aes(distance, diff_sSBR)) +
  geom_point() + geom_line()

This is the difference for the red and blue curves in the previous figure. This seems to indicate that for high fragmentation we find more species for a given cumulative distance than with low fragmentation.

But of course, we cannot say anything about the significance of this difference so far.

My opinion is that the confidence bands in the first figure are potentially by far to narrow, because there is a lot of non-independence among the points and curves in the ssBR.

I guess a permutation test that shuffles samples among the fragmentation scenarios is the way to go.

With respect to coding the question is: