/
anlz_tbbiscr.R
143 lines (124 loc) · 5.68 KB
/
anlz_tbbiscr.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#' Get Tampa Bay Benthic Index scores
#'
#' Get Tampa Bay Benthic Index scores
#'
#' @param benthicdata nested \code{\link[tibble]{tibble}} formatted from \code{\link{read_importbenthic}}
#'
#' @details This function calculates scores for the TBBI based on station, taxa, and field sample data. The total TBBI scores are returned as \code{TBBI} and \code{TBBICat}, where the latter is a categorical description of the scores.
#'
#' @return A single data frame of TBBI scores for each site.
#' @export
#'
#' @concept analyze
#'
#' @importFrom dplyr %>%
#'
#' @examples
#' anlz_tbbiscr(benthicdata)
anlz_tbbiscr <- function(benthicdata){
stations <- benthicdata %>%
tibble::deframe() %>%
.[['stations']]
fieldsamples <- benthicdata %>%
tibble::deframe() %>%
.[['fieldsamples']]
taxacounts <- benthicdata %>%
tibble::deframe() %>%
.[['taxacounts']]
# TaxaSums ----------------------------------------------------------------
# taxa counts aggregated by station & taxa list id
taxasums <- taxacounts %>%
dplyr::filter(!TaxaListID %in% c(209, 2175, 2176, 2177, 2178, 2179, 2087, 1995, 1942)) %>%
# filter(!(COUNT == 0 & TaxaListID != 5)) %>%
dplyr::filter(StationID %in% unique(stations$StationID)) %>%
dplyr::group_by(StationID, FAMILY, NAME, TaxaListID) %>%
dplyr::summarise(
SumofCount = sum(TaxaCount, na.rm = T),
SumofAdjCount = sum(AdjCount, na.rm = T),
.groups = 'drop'
)
# BioStats ----------------------------------------------------------------
# biology stats aggregated by station
biostats <- taxasums %>%
dplyr::group_by(StationID) %>%
dplyr::summarise(
SpeciesRichness = length(na.omit(NAME)),
RawCountAbundance = sum(SumofCount, na.rm = T),
AdjCountAbundance = sum(SumofAdjCount, na.rm = T),
.groups = 'drop'
)
# BioStatsPopulation ------------------------------------------------------
spionid <- taxasums %>%
dplyr::filter(FAMILY %in% 'Spionidae') %>%
dplyr::group_by(StationID) %>%
dplyr::summarise(SpionidAbundance = sum(SumofAdjCount, na.rm = T), .groups = 'drop')
capitellid <- taxasums %>%
dplyr::filter(FAMILY %in% 'Capitellidae') %>%
dplyr::group_by(StationID) %>%
dplyr::summarise(CapitellidAbundance = sum(SumofAdjCount, na.rm = T), .groups = 'drop')
# calculate biology populations/abundance by station
biostatspopulation <- biostats %>%
dplyr::left_join(spionid, by = 'StationID') %>%
dplyr::left_join(capitellid, by = 'StationID') %>%
dplyr::left_join(fieldsamples, by = 'StationID') %>%
dplyr::mutate(
StandPropLnSpecies = dplyr::case_when(
is.na(SpeciesRichness) | is.na(Salinity) ~ 0,
T ~ ((log(SpeciesRichness + 1) / log(10))
/ ((( 3.2983 - 0.23576 * Salinity ) + 0.01081 * Salinity^2) - 0.00015327 * Salinity^3)
- 0.84227
) / 0.18952
),
SpeciesRichness = ifelse(is.na(SpeciesRichness), 0, SpeciesRichness),
RawCountAbundance = ifelse(is.na(RawCountAbundance), 0, RawCountAbundance),
TotalAbundance = ifelse(is.na(AdjCountAbundance), 0, AdjCountAbundance),
SpionidAbundance = ifelse(is.na(SpionidAbundance), 0, SpionidAbundance),
CapitellidAbundance = ifelse(is.na(CapitellidAbundance), 0, CapitellidAbundance)
) %>%
dplyr::select(StationID, SpeciesRichness, RawCountAbundance, TotalAbundance, SpionidAbundance, CapitellidAbundance, StandPropLnSpecies)
# BioStatsTBBI ------------------------------------------------------------
biostatstbbi <- biostatspopulation %>%
dplyr::mutate(
TBBI = dplyr::case_when(
CapitellidAbundance == 0 & SpionidAbundance != 0 ~
(((-0.11407) + (StandPropLnSpecies * 0.32583 ) +
(((asin(SpionidAbundance / TotalAbundance) - 0.11646 ) / (0.18554)) *
(-0.1502)) + ((-0.51401) * (-0.60943))) - (-3.3252118)) / (0.7578544 + 3.3252118),
CapitellidAbundance != 0 & SpionidAbundance == 0 ~
(((-0.11407) + (StandPropLnSpecies * 0.32583) + ((-0.62768) * (-0.1502)) +
((( asin( CapitellidAbundance / TotalAbundance) - 0.041249) / 0.08025) *
(-0.60943))) - (-3.3252118)) / (0.7578544 + 3.3252118),
CapitellidAbundance == 0 & SpionidAbundance == 0 & TotalAbundance != 0 ~
(((-0.11407) + (StandPropLnSpecies * 0.32583) + ((-0.62768) * (-0.1502)) +
((-0.51401) * (-0.60943))) - (-3.3252118)) / ( 0.7578544 + 3.3252118),
TotalAbundance == 0 ~ 0,
T ~ ((( -0.11407) + (StandPropLnSpecies * 0.32583) +
(((asin(SpionidAbundance / TotalAbundance) - 0.11646) / 0.18554) * (-0.1502)) +
(((asin( CapitellidAbundance / TotalAbundance) - 0.041249) / 0.08025) *
(-0.60943))) - (-3.3252118)) / (0.7578544 + 3.3252118)
),
TBBI = round(100 * TBBI, 2)
) %>%
dplyr::select(StationID, TotalAbundance, SpeciesRichness, TBBI) %>%
dplyr::filter(!is.na(StationID))
# empty samples -----------------------------------------------------------
empts <- taxacounts %>%
dplyr::filter(TaxaListID %in% 1942) %>%
dplyr::mutate(TotalAbundance = 0, SpeciesRichness = 0, TBBI = 0) %>%
dplyr::select(StationID, TotalAbundance, SpeciesRichness, TBBI)
# final output ------------------------------------------------------------
out <- biostatstbbi %>%
dplyr::bind_rows(empts) %>%
dplyr::mutate(
TBBICat = dplyr::case_when(
TBBI == 0 ~ 'Empty Sample',
TBBI < 73 ~ 'Degraded',
TBBI >= 73 & TBBI < 87 ~ 'Intermediate',
TBBI >= 87 ~ 'Healthy',
T ~ NA_character_
)
) %>%
dplyr::full_join(fieldsamples, by = 'StationID') %>%
dplyr::left_join(stations, ., by = c('StationID', 'date'))
return(out)
}