-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.qmd
80 lines (53 loc) · 4.56 KB
/
index.qmd
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
---
title: Home
execute:
cache: FALSE
---
![OpenCage</a>, <a href="https://creativecommons.org/licenses/by-sa/2.5">CC BY-SA 2.5</a>, via Wikimedia Commons](https://upload.wikimedia.org/wikipedia/commons/1/1e/Mola_mola2.jpg){width=20%}
## Overview
Here we present a possible workflow for modeling and predicting human interactions with the world's largest bony fish, the [*Mola mola*](https://en.wikipedia.org/wiki/Ocean_sunfish). We'll use R and a number of important packages to retrieve observations from [OBIS](https://obis.org/taxon/127405), sea surface temperature from [OISST](https://www.ncei.noaa.gov/products/optimum-interpolation-sst) and wind estimates from [NBS v2](https://coastwatch.noaa.gov/cwn/products/noaa-ncei-blended-seawinds-nbs-v2.html). With these we'll build a presence-only model using a pure-R implementation of [MaxEnt](https://biodiversityinformatics.amnh.org/open_source/maxent/). We'll also try our hand at predicting (hindcast).
The cartoon below generalizes the two-step process that we'll walk through. The first step is where most the coding efforts occurs as we gather our observations and predictor variables we have chosen. The product of this first step, the model, is often saved to disk for later use making predictions.
![overview](images/modeling.png)
## Prerequisites
We are making an assumption that the reader is familiar with [R programming language](https://www.r-project.org/), the tools available in the [tidyverse](https://www.tidyverse.org/), and [spatial data handling](https://r-spatial.org/book/).
## Getting started
> "According to the ancient Chinese proverb, A journey of a thousand miles must begin with a single step." ~ John F. Kennedy
### Handling spatial data
In this tutorial we use the [sf](https://CRAN.R-project.org/package=sf) and [stars](https://CRAN.R-project.org/package=stars) packages to handle spatial data. These tutorials [for sf](https://r-spatial.github.io/sf/) and [for stars](https://r-spatial.github.io/stars/) will help you off to a great start.
### Project-specific functions
We have developed a suite of functions that facilitate accessing and working with data. These can be loaded into your R session by `source()`-ing the `setup.R` file. Here's an example where we show the study area using the ancillary function `get_bb()` to retrieve the project's bounding box.
```{r}
#| cache: true
source("setup.R", echo = FALSE)
bb = get_bb(form = 'polygon')
coast = rnaturalearth::ne_coastline(scale = 'large', returnclass = 'sf')
plot(sf::st_geometry(coast), extent = bb, axes = TRUE, reset = FALSE)
plot(bb, lwd = 2, border = 'orange', add = TRUE)
```
The setup file also checks for the required packages, and will attempt to install them into the user's R library directory if not already installed.
### Fetching data
The [robis](https://CRAN.R-project.org/package=robis) package facilitates easy access to [OBIS](https://obis.org/) which is a huge public database for oceanographic species information. We have written a wrapper function to download the *Mola mola* species records in our study region. To simplify our task we drop may columns of data from that delivered by OBIS, but there is much in the original worth exploring. Note you can use this function to access other species in other parts of the world.
:::{.callout-note}
Note that we have already fetched the data, so we don't run this next step (but you can if you like to get updated data.)
:::
```{r fetch-data}
if (!file.exists('data/obis/Mola_mola.gpkg')){
x = fetch_obis(scientificname = 'Mola mola')
}
```
Since we already have the data, we need only read it which we do below. We also "glimpse" at the data so we can get a sense of the variables within the data frame and their respective data types.
```{r read_obis}
x = read_obis(scientificname = 'Mola mola') |>
dplyr::glimpse()
```
Let's see what we found on a map. We first plot the coastline, but provide the bounding box to establish the limits of the plot. Then we add the box itself and then the points.
```{r}
plot(sf::st_geometry(coast), extent = bb, axes = TRUE, reset = FALSE)
plot(bb, lwd = 2, border = 'orange', add = TRUE)
plot(sf::st_geometry(x), pch = "+", col = 'blue', add = TRUE)
```
### Data storage
We have set up a data directory, `data`, for storing data collected for the project. To start out there isn't much more than the downloaded data set, but we'll added to it as we go. We try to keep the data storage area tidy by using subdirectories. Below we print the directory tree, your's might look slightly different until you have run this code.
```{r}
fs::dir_tree("data", recurse = 1)
```