Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev nh #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(BRI)
export(BRI_)
export(CSI)
export(IBI)
export(LRM)
Expand Down Expand Up @@ -38,6 +39,7 @@ importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(dplyr,summarize)
importFrom(lubridate,ymd)
importFrom(plyr,rbind.fill)
importFrom(purrr,map)
importFrom(purrr,map_dfr)
Expand Down
43 changes: 18 additions & 25 deletions R/BRI.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
#' the BRI score, the more degraded the benthic community represented by the sample.
#'
#' @details
#' The BRI is the abundance weighted pollution tolerance score of the organisms present in a benthic sample. The higher
#' The BRI is the 4th root relative abundance weighted pollution tolerance score of the organisms present in a benthic sample. The higher
#' the BRI score, the more degraded the benthic community represented by the sample.
#'
#' Two types of data are needed to calculate the BRI:
#'
#' (1) the abundance of each species and
#' (2) its pollution tolerance score, P.
#'
#' The P values are available for most species present in the assemblage. Only species for which P values are avialable
#' are used in the BRI calculations. P values showld be obtained for the appropriate habitat and from the most
#' up-to-date list available.
#' (1) the abundance of each species
#' (2) species-specific pollution tolerance score (aka, P Value)
#'
#' The first step in the BRI calculation is to compute the 4th root of the abundance of each taxon in the sample for
#' which P values are available. The next step is to multiply the 4th root abundance value by the P value, for each
#' taxon.
#' Tolerance Values are stored in the Southern California SQO Species List provided with this coded. Species names are periodically
#' updated by benthic experts.
#'
#' Next, separately sum all of the 4th roots of the abundances and all of the products of the 4th roots of abundance
#' and P values. Taxa that lack P values are not included in either sum. The next step is to calculate the BRI score
#' as:
#' The BRI is only calculated from those taxa with a tolerance score. The first step in the BRI calculation is to compute the 4th root
#' of the abundance of each taxon in the sample that have an associated tolerance score
#' The next step is to multiply the 4th root abundance value by the tolerance score for each taxon.
#' The next step is to sum all of the 4th root abundance values in a given sample.
#' The actual BRI score is calculated as:
#'
#' \deqn{ \frac{\sum \left(\sqrt[p]{\textrm{Abundance}} \right) \times P}{\sum \sqrt[p]{\textrm{Abundance}}} }
#'
#' The last step is to compare the BRI score to the BRI threshold values in Table 5 to determine the BRI category and
#' category score.
#' The last step is to convert the BRI score to condition category using the category thresholds listed in Table 5.
#'
#' <Table 5. To be included in R markdown file>
#'
Expand Down Expand Up @@ -64,7 +60,6 @@
#'
#' @export


BRI <- function(BenthicData, logfile = file.path(getwd(), 'logs', format(Sys.time(), "%Y-%m-%d_%H:%M:%S"), 'log.txt' ), verbose = T)
{

Expand Down Expand Up @@ -128,16 +123,16 @@ BRI <- function(BenthicData, logfile = file.path(getwd(), 'logs', format(Sys.tim
# CASQO Technical Manual 3rd Edition Page 72 - Table 4.24
mutate(
Category = case_when( (Score < 39.96) ~ "Reference",
(Score >= 39.96 & Score < 49.15) ~ "Low Disturbance",
(Score >= 49.15 & Score < 73.27) ~ "Moderate Disturbance",
(Score >= 73.27) ~ "High Disturbance"
)) %>%
(Score >= 39.96 & Score < 49.15) ~ "Low Disturbance",
(Score >= 49.15 & Score < 73.27) ~ "Moderate Disturbance",
(Score >= 73.27) ~ "High Disturbance"
)) %>%
# Output the BRI category score given the category for thresholds for Southern CA Marine Bays
mutate(
`Category Score` = case_when( (Category == "Reference") ~ 1,
(Category == "Low Disturbance") ~ 2,
(Category == "Moderate Disturbance") ~ 3,
(Category == "High Disturbance") ~ 4 )
(Category == "Low Disturbance") ~ 2,
(Category == "Moderate Disturbance") ~ 3,
(Category == "High Disturbance") ~ 4 )
) %>%
dplyr::mutate(Index = "BRI")

Expand All @@ -150,5 +145,3 @@ BRI <- function(BenthicData, logfile = file.path(getwd(), 'logs', format(Sys.tim

return(out)
}


21 changes: 16 additions & 5 deletions R/SQO BRI - generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,34 @@
#' NoOrganismsPresent with 0 abundance;
#'
#' @usage
#' BRI(benthic_data)
#' BRI_(benthic_data)
#'
#' @examples
#' data(benthic_sampledata) # load sample data
#' BRI(benthic_sampledata) # see the output
#' BRI_(benthic_sampledata) # see the output
#'
#' @import vegan
#' @import reshape2
#' @importFrom dplyr left_join filter rename select mutate group_by summarize summarise case_when
#' @importFrom lubridate ymd
#'
#' @export


BRI <- function(BenthicData) #BenthicData will need to be the species abundances for each sample in the correct format noted above and in support material
BRI_ <- function(BenthicData) #BenthicData will need to be the species abundances for each sample in the correct format noted above and in support material
{


# I put these in the import statements in the roxygen comments
# I think the only function that may have came from tidyverse that this function needed was ymd from lubridate
# If others come up we can add them as needed, or if it becomes too much we can import the whole tidyverse package (@import tidyverse)
# - Robert 06.10.2024

#loading in packages needed to run function
require(tidyverse)
# require(tidyverse)

# It appears that this version of the function works with all lowercase column names - Robert 06.10.2024
names(BenthicData) <- names(BenthicData) %>% tolower()

#loading in SQO species list that contains p codes, amongst other things

load("data/SoCal SQO LU 4_7_20.RData")
Expand Down
8 changes: 7 additions & 1 deletion R/SQOUnified.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ SQOUnified <- function(benthic = NULL, chem = NULL, tox = NULL, logfile = file.p

# ---- Chemistry ----
if (!is.null(chem)) {
chem <- chem.sqo(chem, logfile = file.path( dirname(logfile), 'Chemistry', 'log.txt' ), verbose = verbose) %>%

chemlogfile <- file.path( dirname(logfile), 'Chemistry', 'chemlog.Rmd' )
chemlibs <- c('tidyverse', 'DT', 'knitr', 'rmarkdown', 'SQOUnified')

init.log(chemlogfile, base.func.name = sys.call(), type = 'RMarkdown', current.time = Sys.time(), is.base.func = length(sys.calls()) == 1, verbose = verbose, libraries = chemlibs)

chem <- chem.sqo(chem, logfile = chemlogfile, verbose = verbose) %>%
mutate(LOE = 'Chemistry') %>%
select(StationID, LOE, Index, Score, Category, `Category Score`)
} else {
Expand Down
Loading