knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
library(macroinvertebrateMetrics)
library(knitr)
library(dplyr)

macroinvertebrateMetrics

R-CMD-check codecov

Work in Progress

Please don't use in production

aquaMetrics package

An R package to calculate macroinverebrate metrics

In R: Install the development version from GitHub:
install.packages("devtools")
library(devtools)
install_github("aquaMetrics/macroinvertebrateMetrics")

Quick-start

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)

Concepts

To calculate any metric you need:

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)

Input Metadata

kable(head(macroinvertebrateMetrics::column_attributes,4))

Metric scores

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 Metadata

kable(head(macroinvertebrateMetrics::metric_cols ,4))

Calculate

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:

  1. The data input is passed to a calc_metric() function.

  2. 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.

  3. 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.



aquaMetrics/macroinvertebratesMetrics documentation built on Feb. 3, 2024, 2:35 a.m.