forked from FelixMay/spatialbiodiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
194 lines (138 loc) · 5.28 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
```{r setup}
library(spatialbiodiv)
library(tidyverse)
library(vegan)
library(scam)
```
# spatialbiodiv
<!-- badges: start -->
<!-- badges: end -->
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](https://github.com/) with:
```{r}
# 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":
```{r example}
library(spatialbiodiv)
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](https://github.com/Stavooo/Fragmentation_Model).
Here, we just extract two scenarios that only differ in their degree of
fragmentation per se.
```{r}
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
```{r}
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
```{r}
frag01_spec <- frag_low %>%
select(sp_1:sp_200)
frag05_spec <- frag_high %>%
select(sp_1:sp_200)
```
Get spatial coordinates of the samples
```{r}
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.
```{r}
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
```{r}
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.
```{r}
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
```{r}
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
```{r, fig.height = 5, fig.width = 6}
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)
```
## 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.
```{r}
new_dist$diff_sSBR <- sSBR_frag05$sSBR_smooth$S - sSBR_frag01$sSBR_smooth$S
```
```{r}
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:
* Integrate the permutation test into this mini package (likely with a lot of
copy-paste from mobr code), or
* Integrate these distance-based curves in mobr, which so far always has sampling
effort (no. of samples or no. of individuals) on the x-axis and as basis for the
comparison among curves.