-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
242 lines (194 loc) · 9.08 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = file.path("man", "figures", "readme-"),
out.width = "80%",
dpi = 300
)
```
# gcube <a href="https://b-cubed-eu.github.io/gcube/"><img src="man/figures/logo.png" align="right" height="139" alt="gcube website" /></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/gcube)](https://CRAN.R-project.org/package=gcube)
[![Release](https://img.shields.io/github/release/b-cubed-eu/gcube.svg)](https://github.com/b-cubed-eu/gcube/releases)
[![R-CMD-check](https://github.com/b-cubed-eu/gcube/actions/workflows/check_on_different_r_os.yml/badge.svg)](https://github.com/b-cubed-eu/gcube/actions/workflows/check_on_different_r_os.yml) [![codecov](https://codecov.io/gh/b-cubed-eu/gcube/branch/main/graph/badge.svg)](https://app.codecov.io/gh/b-cubed-eu/gcube/)
[![repo status](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![DOI](https://zenodo.org/badge/777812249.svg)](https://doi.org/10.5281/zenodo.14038996)
<!-- badges: end -->
The goal of **gcube** is to provide a simulation framework for biodiversity data cubes using the R programming language. This can start from simulating multiple species distributed in a landscape over a temporal scope. In a second phase, the simulation of a variety of observation processes and effort can generate actual occurrence datasets. Based on their (simulated) spatial uncertainty, occurrences can then be designated to a grid to form a data cube.
Simulation studies offer numerous benefits due to their ability to mimic real-world scenarios in controlled and customizable environments. Ecosystems and biodiversity data are very complex and involve a multitude of interacting factors. Simulations allow researchers to model and understand the complexity of ecological systems by varying parameters such as spatial and/or temporal clustering, species prevalence, etc.
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("b-cubed-eu/gcube")
```
## Package name rationale and origin story
The name **gcube** stands for "generate cube" since it can be used to generate biodiversity data cubes from minimal input.
It was first developed during the hackathon "Hacking Biodiversity Data Cubes for Policy", where it won the first price in the category "Visualization and training".
You can read the full story here: <https://doi.org/10.37044/osf.io/vcyr7>
## Example
This is a basic example which shows the workflow for simulating a biodiversity data cube.
It is divided in three steps or processes:
1. Occurrence process
2. Detection process
3. Grid designation process
The functions are set up such that a single polygon as input is enough to go through this workflow using default arguments.
The user can change these arguments to allow for more flexibility.
```{r packages, message=FALSE, warning=FALSE}
# Load packages
library(gcube)
library(sf) # working with spatial objects
library(dplyr) # data wrangling
library(ggplot2) # visualisation with ggplot
```
We create a polygon as input. It represents the spatial extend of the species.
```{r polygon}
#| fig.alt: >
#| Spatial extend in which we will simulate species occurrences.
# Create a polygon to simulate occurrences within
polygon <- st_polygon(list(cbind(c(5, 10, 8, 2, 3, 5), c(2, 1, 7,9, 5, 2))))
# Visualise
ggplot() +
geom_sf(data = polygon) +
theme_minimal()
```
### Occurrence process
We generate occurrence points within the polygon using the `simulate_occurrences()` function.
In this function, the user can specify different levels of spatial clustering, and define the trend of number of occurrences over time.
The default is a random spatial pattern and a single time point with `rpois(1, 50)` occurrences.
```{r simulate-occurrences}
#| fig.alt: >
#| Spatial distribution of occurrences within the polygon.
# Simulate occurrences within polygon
occurrences_df <- simulate_occurrences(
species_range = polygon,
initial_average_occurrences = 50,
spatial_pattern = c("random", "clustered"),
n_time_points = 1,
seed = 123)
# Visualise
ggplot() +
geom_sf(data = polygon) +
geom_sf(data = occurrences_df) +
theme_minimal()
```
### Detection process
In the second step we define the sampling process, based on the detection probability of the species and the sampling bias.
This is done using the `sample_observations()` function.
The default sampling bias is `"no_bias"`, but bias can be added using a polygon or a grid as well.
```{r detect-occurrences}
#| fig.alt: >
#| Spatial distribution of occurrences with indication of sampling status.
# Detect occurrences
detections_df_raw <- sample_observations(
occurrences = occurrences_df,
detection_probability = 0.5,
sampling_bias = c("no_bias", "polygon", "manual"),
seed = 123)
# Visualise
ggplot() +
geom_sf(data = polygon) +
geom_sf(data = detections_df_raw,
aes(colour = sampling_status)) +
theme_minimal()
```
We select the detected occurrences and add an uncertainty to these observations.
This can be done using the `filter_observations()` and `add_coordinate_uncertainty()` functions, respectively.
```{r uncertainty-occurrences}
#| fig.alt: >
#| Spatial distribution of detected occurrences with coordinate uncertainty.
# Select detected occurrences only
detections_df <- filter_observations(
observations_total = detections_df_raw)
# Add coordinate uncertainty
set.seed(123)
coord_uncertainty_vec <- rgamma(nrow(detections_df), shape = 2, rate = 6)
observations_df <- add_coordinate_uncertainty(
observations = detections_df,
coords_uncertainty_meters = coord_uncertainty_vec)
# Created and sf object with uncertainty circles to visualise
buffered_observations <- st_buffer(
observations_df,
observations_df$coordinateUncertaintyInMeters)
# Visualise
ggplot() +
geom_sf(data = polygon) +
geom_sf(data = buffered_observations,
fill = alpha("firebrick", 0.3)) +
geom_sf(data = observations_df, colour = "firebrick") +
theme_minimal()
```
### Grid designation process
Finally, observations are designated to a grid with `grid_designation()` to create an occurrence cube.
We create a grid over the spatial extend using `sf::st_make_grid()`.
```{r create-grid}
# Define a grid over spatial extend
grid_df <- st_make_grid(
buffered_observations,
square = TRUE,
cellsize = c(1.2, 1.2)
) %>%
st_sf() %>%
mutate(intersect = as.vector(st_intersects(geometry, polygon,
sparse = FALSE))) %>%
dplyr::filter(intersect == TRUE) %>%
dplyr::select(-"intersect")
```
To create an occurrence cube, `grid_designation()` will randomly take a point within the uncertainty circle around the observations.
These points can be extracted by setting the argument `aggregate = FALSE`.
```{r grid-designation}
#| fig.alt: >
#| Distribution of random samples within uncertainty circle.
# Create occurrence cube
occurrence_cube_df <- grid_designation(
observations = observations_df,
grid = grid_df,
seed = 123)
# Get sampled points within uncertainty circle
sampled_points <- grid_designation(
observations = observations_df,
grid = grid_df,
aggregate = FALSE,
seed = 123)
# Visualise grid designation
ggplot() +
geom_sf(data = occurrence_cube_df, linewidth = 1) +
geom_sf_text(data = occurrence_cube_df, aes(label = n)) +
geom_sf(data = buffered_observations,
fill = alpha("firebrick", 0.3)) +
geom_sf(data = sampled_points, colour = "blue") +
geom_sf(data = observations_df, colour = "firebrick") +
labs(x = "", y = "", fill = "n") +
theme_minimal()
```
The output gives the number of observations per grid cell and minimal coordinate uncertainty per grid cell.
```{r visualise-designation}
#| fig.alt: >
#| Distribution of minimal coordinate uncertainty.
# Visualise minimal coordinate uncertainty
ggplot() +
geom_sf(data = occurrence_cube_df, aes(fill = min_coord_uncertainty),
alpha = 0.5, linewidth = 1) +
geom_sf_text(data = occurrence_cube_df, aes(label = n)) +
scale_fill_continuous(type = "viridis") +
labs(x = "", y = "") +
theme_minimal()
```
### Cubes for multiple species
Each cube simulation function mentioned earlier has a corresponding mapping function.
These mapping functions are designed to handle operations for multiple species simultaneously by using the `purrr::pmap()` function.
Please consult the documentation for detailed information on how these mapping functions are implemented.
| single species | multiple species |
|-----------------------------|---------------------------------|
| simulate_occurrences() | map_simulate_occurrences() |
| sample_observations() | map_sample_observations() |
| filter_observations() | map_filter_observations() |
| add_coordinate_uncertainty()| map_add_coordinate_uncertainty()|
| grid_designation() | map_grid_designation() |