Skip to content

Commit

Permalink
Tidy sections in covariates.qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
btupper committed Oct 4, 2023
1 parent c20a00c commit b7b46fa
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 50 deletions.
10 changes: 10 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ execute:

website:
title: "Species distribution modeling with R"
page-navigation: true
back-to-top-navigation: true
page-footer: "Copyright 2023, Bigelow Laboratory for Ocean Science"
sidebar:
style: "docked"
search: true
Expand All @@ -27,6 +30,13 @@ website:
text: Extracting covariates
- href: about.qmd
text: About
tools:
- icon: github
menu:
- text: Source Code
url: https://github.com/BigelowLab/mola-mola
- text: Report a bug or ask a question
url: https://github.com/BigelowLab/mola-mola/issues

format:
html:
Expand Down
27 changes: 16 additions & 11 deletions covariates.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ cache: true

Here we do the multi-step task of associating observations with environmental covariates and creating a background point data set used by the model to characterize the environment.

## Background point to characterize the environment
## Background points to characterize the environment

Presence only modeling using [MaxEnt](https://biodiversityinformatics.amnh.org/open_source/maxent/) or the pure R implementation [maxnet](https://github.com/BigelowLab/maxnet) require that a sample representing "background" are provided to the model building stage. Background points are used to characterize the environment in which the presence points are found, the modeling algorithm uses that information to discriminate between suitability and unsuitability of the environment. It is good practice to sample "background" in the same spatial and temporal range as the presence data. That means we need to define a bounding polygon around the presence locations from which we can sample, as well as sampling through time.


## Loading data

We have two data sources to load: point observation data and rasterized environmental predicitor data.

### Load the observation data
We'll load in our [OBIS](https://obis.org/taxon/127405) observations as a flat table, and immediately filter the data to any occurring from 2000 to present.

Expand All @@ -23,8 +25,7 @@ obs = read_obis(form = "sf") |>
dplyr::glimpse()
```


## Load the environmental predictors
### Load the environmental predictors

Next we load the environmental predictors, `sst` and `wind` (as `windspeed`, `u_wind` and `v_wind`). For each we first read in the database, then compose filenames, read in the rasters and organize into layers by date, then rename to a pretty-name and finally shift the rasters from their native 0-to-360 longitude range to longitude -180-to-180 which matches our observations.

Expand Down Expand Up @@ -72,6 +73,10 @@ v_wind = v_wind_db |>
We'll set these aside for a moment and come back to them after we have established our background points.


## Sampling background data

We need to create a random sample of background in both time and space.

### Sampling time

Sampling time requires us to consider that the occurrences are not even distributed through time. We can see that using a histogram of observation dates by month.
Expand Down Expand Up @@ -219,27 +224,27 @@ plot(sf::st_geometry(bkg), pch = ".", col = "blue", add = TRUE)
```


### Extract environmental covariates for `sst` and `wind`
## Extract environmental covariates for `sst` and `wind`

#### Wait, what about dates?
### Wait, what about dates?

You may have considered already an issue connecting our background points which have daily dates with our covariates which are monthly (identified by the first of each month.) We can manage that by adding a second date, `month_id`, to the `bkg` table. We'll use the `current_month()` function from the [oisster](https://github.com/BigelowLab/oisster) package to compute that.

```{r}
bkg = dplyr::mutate(bkg, month_id = oisster::current_month(date))
```

#### First extract background points
### First extract background points

##### Extract `sst`
#### Extract `sst`

Now we need to read in the complete `sst` data. We have already read in all of the `sst` data, and then transform the longitude coordinates to the -180 to 180 range. Then we extract, specifying which variable in `bkg` is mapped to the time domain in `sst` - in our case the newly computed `month_id` matches the `time` dimension in `sst`.

```{r}
sst_values = stars::st_extract(sst, bkg, time_column = 'month_id')
```

##### Extract `wind`
#### Extract `wind`

For wind we have three parameters (`windspeed`, `u_wind` and `v_wind`). Presumably `windspeed` is a function of `u_wind` and `v_wind` and will be correlated with them. Nonetheless, for completeness, we'll extract all three.

Expand All @@ -251,7 +256,7 @@ u_wind_values = stars::st_extract(u_wind, bkg, time_column = "month_id")
v_wind_values = stars::st_extract(v_wind, bkg, time_column = "month_id")
```

#### Now put them together and save
### Now put them together and save

Now we merge the three extractions for `sst`, `u_wind` and `v_wind` into one object, and then save to disk for later retrieval. It might be tempting to use `merge()` or one of [dplyr's join functions](https://dplyr.tidyverse.org/reference/mutate-joins.html), but we really have an easy task as all of our extractions have the same number of records in the same order. We need only mutate `bkg` to include each of the extracted values.

Expand All @@ -265,9 +270,9 @@ bkg = bkg |>
sf::write_sf(file.path("data", "bkg", "bkg-covariates.gpkg"))
```

#### Next extract observation points
### Next extract observation points

It's the same workflow to extract covariates for the observations, but let's not forget to add in a variable to identify the month that matches those in the predictors.
It's the same workflow to extract covariates for the observations as it was for the background, but let's not forget to add in a variable to identify the month that matches those in the predictors.

```{r}
obs = dplyr::mutate(obs, month_id = oisster::current_month(date))
Expand Down
Empty file added docs/.nojekyll
Empty file.
40 changes: 39 additions & 1 deletion docs/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./covariates.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
Expand Down Expand Up @@ -89,6 +90,23 @@
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Species distribution modeling with R</a>
<div class="sidebar-tools-main">
<div class="dropdown">
<a href="" title="" id="quarto-navigation-tool-dropdown-0" class="quarto-navigation-tool dropdown-toggle px-1" data-bs-toggle="dropdown" aria-expanded="false" aria-label=""><i class="bi bi-github"></i></a>
<ul class="dropdown-menu" aria-labelledby="quarto-navigation-tool-dropdown-0">
<li>
<a class="dropdown-item sidebar-tools-main-item" href="https://github.com/BigelowLab/mola-mola">
Source Code
</a>
</li>
<li>
<a class="dropdown-item sidebar-tools-main-item" href="https://github.com/BigelowLab/mola-mola/issues">
Report a bug or ask a question
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
Expand Down Expand Up @@ -160,7 +178,7 @@ <h1 class="title">About</h1>



</main> <!-- /main -->
<a onclick="window.scrollTo(0, 0); return false;" role="button" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a></main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
Expand Down Expand Up @@ -394,7 +412,27 @@ <h1 class="title">About</h1>
}
});
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="./covariates.html" class="pagination-link">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Extracting covariates</span>
</a>
</div>
<div class="nav-page nav-page-next">
</div>
</nav>
</div> <!-- /content -->
<footer class="footer">
<div class="nav-footer">
<div class="nav-footer-left">
&nbsp;
</div>
<div class="nav-footer-center">Copyright 2023, Bigelow Laboratory for Ocean Science</div>
<div class="nav-footer-right">
&nbsp;
</div>
</div>
</footer>



Expand Down
Loading

0 comments on commit b7b46fa

Please sign in to comment.