knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library(macroinvertebrateMetrics) library(knitr) library(dplyr)
Please don't use in production
An R package to calculate macroinverebrate metrics
In R: Install the development version from GitHub:
install.packages("devtools")
library(devtools)
install_github("aquaMetrics/macroinvertebrateMetrics")
Calculate all metrics:
library(macroinvertebrateMetrics) all_metrics <- calc_metric(demo_data) head(all_metrics, 4)
Calculate specific metrics:
selected_metrics <- calc_metric(demo_data, metrics = c("whpt","awic")) head(selected_metrics, 4)
Use metric specific function:
specific_metric <- calc_epsi(demo_data) head(specific_metric, 4)
To calculate any metric you need:
label
(taxon name) and response
(abundance - some metrics don't require abundance).sample_id
, parameter
, question
- these book-keeping variables give more info on the type of data and metrics which can be calculated based on the input data.For example:
demo_data <- macroinvertebrateMetrics::demo_data data <- demo_data[demo_data$parameter %in% "River Family Inverts" & demo_data$question == "Taxon abundance", c("sample_id", "label", "response", "parameter")] kable(head(data, 4))
Here's the precise definition of each recommended input column see column_attributes
:
kable(column_attributes)
unique(metric_cols$parameter)
kable(unique(select(metric_cols, metric, parameter)), row.names = FALSE)
kable(head(macroinvertebrateMetrics::column_attributes,4))
macroinvertebrateTaxa
For example:
taxa <- macroinvertebrateMetrics::macroinvertebrateTaxa taxa <- taxa[, c("TAXON_NAME", "WHPT_A", "EPSI_WEIGHT_FAM", "SPEAR_SPECIES")] taxa <- taxa[complete.cases(taxa), ] kable(head(taxa, 4))
metric_cols
kable(head(macroinvertebrateMetrics::metric_cols ,4))
There are two ways to calculate a given metric, for example WHPT can be
calculated using either calc_metric(demo_data, metric = "whpt")
or the metric
specific function calc_whpt(demo_data)
. These two function execute the same
code except that calc_awic()
uses calc_metric()
behind the scenes and hard
codes the metric = "whpt"
parameter to save a little bit of typing.
All metrics follow the same calculation process:
The data
input is passed to a calc_metric()
function.
Input data
is validated in an internal function called validate_data()
. This function checks the data is in the correct format and then joins the data to taxonomic table containing metrics values.
The data
(now with the metric values attached) is then passed to specific internal metric functions. These metric function calculates the specific metrics. If more then one metric is requested, the calc_metric()
function calculates only the requested metrics in-turn and then binds and returns the outputs in a final output dataframe.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.