vignettes/articles/covariates_data.Rmd
covariates_data.RmdPrior to mermaidr version 2.0.0, you had the option to
access environmental covariates along with data from MERMAID Collect
directly in mermaidr. These covariates have been
centralized into the MERMAID covariates platform, and the
ability to access them has been moved to a dedicated package,
mermaidrcovariates, and removed from
mermaidr.
The newly released MERMAID covariates platform is a
centralized, regularly updated catalogue of remote-sensing datasets —
climate, environmental, and social — which are normalized so they can be
paired directly with MERMAID survey locations. You can browse the full
catalogue at mermaid.prescient.earth, and
access the data programmatically through
mermaidrcovariates, a companion to mermaidr,
used throughout this vignette.
For full usage details of mermaidrcovariates, please
visit its website.
This vignette will specifically walk through accessing the
environmental covariates that were previously included alongside data
accessed from mermaidr. Note that not all datasets were
migrated:
| Source | Data |
mermaidr column(s)
|
Prescient source | Prescient dataset ID |
|---|---|---|---|---|
| Geomorphic zonation |
aca_geomorphic
|
ACA Reef Extent |
aca_extent
|
|
| Benthic habitat |
aca_benthic
|
ACA Benthic Habitat |
aca_benthic
|
|
|
A
global map of human pressures on tropical coral reefs by
Andrello et al., 2022
|
Market gravity |
andrello_grav_nc
|
Market gravity (fishing pressure) |
market_gravity
|
| Water pollution (sediments and nitrogen) |
andrello_sediment, andrello_nutrient
|
— | — | |
| Coastal population |
andrello_pop_count
|
Human population within 5km (coastal population) |
human_pop_5km
|
|
| Industrial development (number of ports) |
andrello_num_ports
|
Number of ports within 5m (industrial development) |
num_ports_5km
|
|
| Tourism (reef value) |
andrello_reef_value
|
Tourism index |
tourism_index
|
|
| Cumulative pressure index |
andrello_cumul_score
|
— | — | |
| Risk-sensitive planning for conserving coral reefs under rapid climate change by Beyer et al., 2018. | Scores |
beyer_score, beyer_scorecn,
beyer_scorecy, beyer_scorepfc,
beyer_scoreth, beyer_scoretr
|
— | — |
We first need to access ecological data from MERMAID to link our environmental covariates to. We can link them directly to survey sites, using their latitude and longitude:
library(mermaidr)
library(tidyverse)
sites <- mermaid_get_sites(limit = 2000) %>%
filter(country == "Belize") %>%
distinct(name, latitude, longitude)
sites#> # A tibble: 12 × 3
#> name latitude longitude
#> <chr> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8
#> 2 BCMR 07 18.2 -87.8
#> 3 BCMR 22 18.1 -87.8
#> 4 BCMR 24 18.1 -87.8
#> 5 BCMR 29 18.1 -87.9
#> 6 BCMR 33 18.1 -87.9
#> 7 BCMR BR 19 18.1 -87.8
#> 8 AGGRA CAYMAN 16.0 -88.3
#> 9 BCMR 31 18.1 -87.8
#> 10 BCMR 39 18.1 -87.9
#> 11 BCMR 46 18.1 -87.9
#> 12 BCMR - Alleys 18.2 -87.8
The Allen Coral Atlas covariates (Reef Extent and Benthic Habitat) are in a raster format, while the Andrello et al. covariates contain vector data. We use different functions to access each.
Again, the mermaidrcovariates
package website contains more information on the differences between
these types, and how to determine if you are accessing raster or vector
data.
To access raster covariates, we use
get_zonal_statistics(). We will request all data within 100
metres of each site, and then spatially aggregate that data
using the mean.
Using Reef Extent as an example, the function returns the sites along
with a column, zonal_statistics, that we can unnest to look
at the data within:
library(mermaidrcovariates)
reef_extent <- get_zonal_statistics(sites, "aca_extent", spatial_stats = "mean", radius = 100)
reef_extent
#> # A tibble: 12 × 4
#> name latitude longitude zonal_statistics
#> <chr> <dbl> <dbl> <list>
#> 1 BCMR 06 18.2 -87.8 <tibble [1 × 5]>
#> 2 BCMR 07 18.2 -87.8 <tibble [1 × 5]>
#> 3 BCMR 22 18.1 -87.8 <tibble [1 × 5]>
#> 4 BCMR 24 18.1 -87.8 <tibble [1 × 5]>
#> 5 BCMR 29 18.1 -87.9 <tibble [1 × 5]>
#> 6 BCMR 33 18.1 -87.9 <tibble [1 × 5]>
#> 7 BCMR BR 19 18.1 -87.8 <tibble [1 × 5]>
#> 8 AGGRA CAYMAN 16.0 -88.3 <tibble [1 × 5]>
#> 9 BCMR 31 18.1 -87.8 <tibble [1 × 5]>
#> 10 BCMR 39 18.1 -87.9 <tibble [1 × 5]>
#> 11 BCMR 46 18.1 -87.9 <tibble [1 × 5]>
#> 12 BCMR - Alleys 18.2 -87.8 <tibble [1 × 5]>
reef_extent <- reef_extent %>%
unnest(zonal_statistics)
reef_extent
#> # A tibble: 12 × 8
#> name latitude longitude covariate date band spatial_stat
#> <chr> <dbl> <dbl> <chr> <date> <dbl> <chr>
#> 1 BCMR 06 18.2 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 2 BCMR 07 18.2 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 3 BCMR 22 18.1 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 4 BCMR 24 18.1 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 5 BCMR 29 18.1 -87.9 ACA Reef Extent 2026-01-01 1 mean
#> 6 BCMR 33 18.1 -87.9 ACA Reef Extent 2026-01-01 1 mean
#> 7 BCMR BR 19 18.1 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 8 AGGRA CAYMAN 16.0 -88.3 ACA Reef Extent 2026-01-01 1 mean
#> 9 BCMR 31 18.1 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> 10 BCMR 39 18.1 -87.9 ACA Reef Extent 2026-01-01 1 mean
#> 11 BCMR 46 18.1 -87.9 ACA Reef Extent 2026-01-01 1 mean
#> 12 BCMR - Alleys 18.2 -87.8 ACA Reef Extent 2026-01-01 1 mean
#> value
#> <dbl>
#> 1 1
#> 2 1
#> 3 1
#> 4 1
#> 5 1
#> 6 1
#> 7 1
#> 8 NA
#> 9 NA
#> 10 1
#> 11 1
#> 12 1In order to combine this with all of the other environmental covariates, we plan to keep the site information along with the covariate ID and value, converting it into a wide format so that, eventually, there is a column for each covariate.
reef_extent %>%
mutate(covariate_id = "aca_extent") %>%
select(name, latitude, longitude, covariate_id, value) %>%
pivot_wider(names_from = covariate_id, values_from = value)
#> # A tibble: 12 × 4
#> name latitude longitude aca_extent
#> <chr> <dbl> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8 1
#> 2 BCMR 07 18.2 -87.8 1
#> 3 BCMR 22 18.1 -87.8 1
#> 4 BCMR 24 18.1 -87.8 1
#> 5 BCMR 29 18.1 -87.9 1
#> 6 BCMR 33 18.1 -87.9 1
#> 7 BCMR BR 19 18.1 -87.8 1
#> 8 AGGRA CAYMAN 16.0 -88.3 NA
#> 9 BCMR 31 18.1 -87.8 NA
#> 10 BCMR 39 18.1 -87.9 1
#> 11 BCMR 46 18.1 -87.9 1
#> 12 BCMR - Alleys 18.2 -87.8 1In order to not repeat the same code for each covariate, we write a helper function that gets the zonal statistics for each of the Allen Coral Atlas covariates, keeps the relevant columns, combines them, then converts into a wide format:
attach_all_raster_covariates <- function(sites, covariate_ids) {
covariate_res <- map(
covariate_ids,
function(covariate_id) {
get_zonal_statistics(sites, covariate_id, spatial_stats = "mean", radius = 100) %>%
unnest(zonal_statistics) %>%
mutate(covariate_id = covariate_id) %>%
select(name, latitude, longitude, covariate_id, value)
}
)
covariate_res %>%
bind_rows() %>%
pivot_wider(names_from = covariate_id, values_from = value)
}
raster_covariates <- sites %>%
attach_all_raster_covariates(c("aca_extent", "aca_benthic"))
raster_covariates
#> # A tibble: 12 × 5
#> name latitude longitude aca_extent aca_benthic
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8 1 4
#> 2 BCMR 07 18.2 -87.8 1 4
#> 3 BCMR 22 18.1 -87.8 1 1
#> 4 BCMR 24 18.1 -87.8 1 4
#> 5 BCMR 29 18.1 -87.9 1 3
#> 6 BCMR 33 18.1 -87.9 1 5
#> 7 BCMR BR 19 18.1 -87.8 1 5
#> 8 AGGRA CAYMAN 16.0 -88.3 NA NA
#> 9 BCMR 31 18.1 -87.8 NA NA
#> 10 BCMR 39 18.1 -87.9 1 NA
#> 11 BCMR 46 18.1 -87.9 1 NA
#> 12 BCMR - Alleys 18.2 -87.8 1 NANext, we repeat a similar process for the vector covariates. This
time, we use the mermaidrcovariates function
attach_covariate_data(), using Human population within 5km
(coastal population) as an example. We do not need to specify a radius
or spatial aggregation method for vector data.
human_pop_5km <- attach_covariate_data(sites, "human_pop_5km")
human_pop_5km
#> # A tibble: 12 × 5
#> name latitude longitude pop_count pop_count_raw
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8 0.600 631.
#> 2 BCMR 07 18.2 -87.8 0.600 631.
#> 3 BCMR 22 18.1 -87.8 0.632 868.
#> 4 BCMR 24 18.1 -87.8 0.632 868.
#> 5 BCMR 29 18.1 -87.9 0.724 2070.
#> 6 BCMR 33 18.1 -87.9 0.696 1584.
#> 7 BCMR BR 19 18.1 -87.8 0.632 868.
#> 8 AGGRA CAYMAN 16.0 -88.3 NA NA
#> 9 BCMR 31 18.1 -87.8 0.632 868.
#> 10 BCMR 39 18.1 -87.9 0.696 1584.
#> 11 BCMR 46 18.1 -87.9 0.696 1584.
#> 12 BCMR - Alleys 18.2 -87.8 0.600 631.This data is already unnested and is in a wide format; but in order to easily combine it with the other covariates, we will convert it into a long format, combine, and then re-widen. Again, creating a helper function for this:
attach_all_vector_covariates <- function(sites, covariate_ids) {
covariate_res <- map(
covariate_ids,
function(covariate_id) {
attach_covariate_data(sites, covariate_id) %>%
pivot_longer(
cols = -names(sites),
names_to = "covariate_id",
values_to = "value"
)
}
)
covariate_res %>%
bind_rows() %>%
pivot_wider(names_from = covariate_id, values_from = value)
}
vector_covariates <- sites %>%
attach_all_vector_covariates(c("human_pop_5km", "market_gravity", "num_ports_5km", "tourism_index"))
vector_covariates
#> # A tibble: 12 × 11
#> name latitude longitude pop_count pop_count_raw grav_NC grav_NC_raw
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8 0.600 631. 0.449 0.518
#> 2 BCMR 07 18.2 -87.8 0.600 631. 0.449 0.518
#> 3 BCMR 22 18.1 -87.8 0.632 868. 0.452 0.526
#> 4 BCMR 24 18.1 -87.8 0.632 868. 0.452 0.526
#> 5 BCMR 29 18.1 -87.9 0.724 2070. 0.486 0.642
#> 6 BCMR 33 18.1 -87.9 0.696 1584. 0.475 0.602
#> 7 BCMR BR 19 18.1 -87.8 0.632 868. 0.452 0.526
#> 8 AGGRA CAYMAN 16.0 -88.3 NA NA NA NA
#> 9 BCMR 31 18.1 -87.8 0.632 868. 0.452 0.526
#> 10 BCMR 39 18.1 -87.9 0.696 1584. 0.475 0.602
#> 11 BCMR 46 18.1 -87.9 0.696 1584. 0.475 0.602
#> 12 BCMR - Alleys 18.2 -87.8 0.600 631. 0.449 0.518
#> num_ports num_ports_raw reef_value reef_value_raw
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0 0 0.929 1412
#> 2 0 0 0.929 1412
#> 3 0 0 0.922 1162
#> 4 0 0 0.922 1162
#> 5 0 0 0.831 146
#> 6 0 0 0.950 2654
#> 7 0 0 0.922 1162
#> 8 NA NA NA NA
#> 9 0 0 0.922 1162
#> 10 0 0 0.950 2654
#> 11 0 0 0.950 2654
#> 12 0 0 0.929 1412Now that we have the two sets of covariates,
raster_covariates and vector_covariates, we
can easily combine them by joining them back onto the sites!
sites %>%
left_join(raster_covariates, by = c("name", "latitude", "longitude")) %>%
left_join(vector_covariates, by = c("name", "latitude", "longitude"))
#> # A tibble: 12 × 13
#> name latitude longitude aca_extent aca_benthic pop_count pop_count_raw
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 BCMR 06 18.2 -87.8 NA NA 0.600 631.
#> 2 BCMR 07 18.2 -87.8 NA NA 0.600 631.
#> 3 BCMR 22 18.1 -87.8 NA NA 0.632 868.
#> 4 BCMR 24 18.1 -87.8 NA NA 0.632 868.
#> 5 BCMR 29 18.1 -87.9 NA NA 0.724 2070.
#> 6 BCMR 33 18.1 -87.9 NA NA 0.696 1584.
#> 7 BCMR BR 19 18.1 -87.8 1 5 0.632 868.
#> 8 AGGRA CAYMAN 16.0 -88.3 NA NA NA NA
#> 9 BCMR 31 18.1 -87.8 NA NA 0.632 868.
#> 10 BCMR 39 18.1 -87.9 NA NA 0.696 1584.
#> 11 BCMR 46 18.1 -87.9 1 NA 0.696 1584.
#> 12 BCMR - Alleys 18.2 -87.8 1 NA 0.600 631.
#> grav_NC grav_NC_raw num_ports num_ports_raw reef_value reef_value_raw
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.449 0.518 0 0 0.929 1412
#> 2 0.449 0.518 0 0 0.929 1412
#> 3 0.452 0.526 0 0 0.922 1162
#> 4 0.452 0.526 0 0 0.922 1162
#> 5 0.486 0.642 0 0 0.831 146
#> 6 0.475 0.602 0 0 0.950 2654
#> 7 0.452 0.526 0 0 0.922 1162
#> 8 NA NA NA NA NA NA
#> 9 0.452 0.526 0 0 0.922 1162
#> 10 0.475 0.602 0 0 0.950 2654
#> 11 0.475 0.602 0 0 0.950 2654
#> 12 0.449 0.518 0 0 0.929 1412mermaidr
The covariates argument in
mermaid_get_project_sites() and
mermaid_get_project_data() in mermaidr will
now produce a message if you set it to TRUE, and returns
the data without the covariates.
p <- mermaid_get_my_projects(limit = 1)
mermaid_get_project_sites(p, covariates = TRUE)
#> ℹ `covariates` argument is deprecated as of mermaidr version 2.0.0. Please use the `mermaidrcovariates` package to access covariates. See the vignette: <https://data-mermaid.github.io/mermaidr/articles/covariates_data.html>. Your results will be returned without any covariates.
#> # A tibble: 77 × 12
#> project id name latitude longitude notes country reef_type reef_zone
#> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr> <chr>
#> 1 Great Sea R… 9c2f… BA02 -17.4 178. Sour… Fiji atoll back reef
#> 2 Great Sea R… c8bd… BA03 -17.4 178. <NA> Fiji atoll back reef
#> 3 Great Sea R… aa47… BA04 -17.4 178. <NA> Fiji atoll back reef
#> 4 Great Sea R… 87ab… BA05 -17.4 178. <NA> Fiji atoll back reef
#> 5 Great Sea R… dbd9… BA06 -17.4 178. <NA> Fiji atoll back reef
#> 6 Great Sea R… a684… BA07 -17.5 178. <NA> Fiji atoll back reef
#> 7 Great Sea R… 5cc3… BA08 -17.4 178. <NA> Fiji atoll back reef
#> 8 Great Sea R… 0235… BA09 -17.4 178. <NA> Fiji atoll back reef
#> 9 Great Sea R… 2f08… BA10 -17.3 178. <NA> Fiji atoll back reef
#> 10 Great Sea R… 20ae… BA11 -17.3 178. <NA> Fiji atoll back reef
#> # ℹ 67 more rows
#> # ℹ 3 more variables: exposure <chr>, created_on <chr>, updated_on <chr>