Skip to content

Commit 6824880

Browse files
committed
add esa_salmon data sets
1 parent 4b1219f commit 6824880

File tree

6 files changed

+195
-3
lines changed

6 files changed

+195
-3
lines changed

R/atsalibrary-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
#' * [neon_barc]
1313
#' * [MLCO2]
1414
#' * [NHTemp]
15-
#'
15+
#' * [esa_salmon]
1616
#'
1717
#' @name atsalibrary-package
18-
#' @aliases atsalibrary
18+
#' @aliases atsalibrary-package
1919
#' @docType package
2020
#' @keywords package
2121
NULL

R/esa-salmon-data.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#' Annual spawner data from Endangered and Threatened PNW salmonids
2+
#'
3+
#' @description The data set has yearly spawner counts for endangered and threatened ESU (Evolutionary Significant Units) and DPS (Distinct Population Segments) in the Washington and Oregon. Data were downloaded from [Coordinated Assessments API](https://www.streamnet.org/resources/exchange-tools/rest-api-documentation/). Coordinated
4+
#' Assessments data eXchange (CAX) is developed by the Coordinated Assessments Partnership (CAP).
5+
#'
6+
#' @details There are two datasets included: `esa.salmon` and `columbia.river`. The Columbia River data set is a subset of the `esa.salmon` dataset that has all the ESUs and DPSs.
7+
#' The dataset has the following columns
8+
#' * species: Chinook, Coho, Steelhead, Chum, Sockeye
9+
#' * esu_dps: name of the ESU
10+
#' * majorpopgroup: biological major group
11+
#' * commonpopname: common population name, generally a stream or river
12+
#' * run: run-timing
13+
#' * spawningyear: the year that the spawners were counted on the spawning grounds
14+
#' * spawner: total (natural-born and hatchery-born) spawners on the spawning ground. Generally some type of redd-count expansion or some other stream count of spawners. Redd = a gravel nest.
15+
#' * log.spawners: log of value
16+
#'
17+
#' @docType data
18+
#'
19+
#' @name esa_salmon
20+
#'
21+
#' @aliases esa.salmon columbia.river
22+
#'
23+
#' @usage data(esa_salmon)
24+
#'
25+
#' @format Objects of class \code{"data.frame"}. Columns are species,
26+
#' esu_dps, majorpopgroup, commonpopname, run, spawningyear, spawner, log.spawner
27+
#'
28+
#' @keywords datasets
29+
#'
30+
#' @source
31+
#' \href{https://www.streamnet.org/cap/current-hli/}{CAP} StreamNet Coordinated Assessments Partnership (CAP) standardized high-level indicators (HLIs) for Natural Origin Spawner Abundance (NOSA).
32+
#'
33+
#' @references
34+
#' rCAX: https://zenodo.org/records/10214433
35+
#'
36+
#' @examples
37+
#' data(esa.salmon)
38+
#' df <- esa.salmon %>% subset(species == "Steelhead" & run == "Winter")
39+
#' ggplot(df, aes(x=spawningyear, y=log.spawner, color=majorpopgroup)) +
40+
#' geom_point(size=0.2) +
41+
#' theme(strip.text.x = element_text(size = 2)) +
42+
#' theme(axis.text.x = element_text(size = 5, angle = 90)) +
43+
#' facet_wrap(~esapopname)
44+
"esa.salmon"
45+
"columbia.river"

data/esa_salmon.RData

77.4 KB
Binary file not shown.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Create data files
2+
# remotes:::install_github("nwfsc-cb/rCAX@*release")
3+
library(rCAX)
4+
library(tidyr)
5+
library(stringr)
6+
library(dplyr)
7+
x <- rCAX:::caxesu
8+
valid_esu <- which(!str_detect(x, "XN") & !str_detect(x,"N/A"))
9+
esa.salmon <- NULL
10+
for(i in valid_esu){
11+
esuname <- rCAX:::caxesu[i]
12+
print(esuname)
13+
a <- rcax_hli("NOSA", type="colnames")
14+
tab <- rcax_hli("NOSA", flist = list(esu_dps = esuname))
15+
# error no data
16+
if (!is.data.frame(tab)) next
17+
# find the pops with no data and remove
18+
tab <- tab %>%
19+
subset((datastatus == "Final" | datastatus == "Reviewed") & bestvalue=="Yes")
20+
if(tab$tsaej[1]=="") tab$value <- tab$tsaij else tab$value <- tab$tsaej
21+
aa <- tab %>%
22+
group_by(esapopname, run) %>%
23+
summarize(n = sum(value!= "" & value!="0" & majorpopgroup != ""))
24+
bad <- aa[which(aa$n==0),]
25+
aa <- tab %>%
26+
group_by(esapopname, run) %>%
27+
summarize(n = any(duplicated(spawningyear)))
28+
df <- tab %>%
29+
subset(!(esapopname %in% bad$esapopname & run %in% bad$run)) %>%
30+
mutate(value = as.numeric(value))
31+
32+
# get the min and max years in data
33+
years <- min(df$spawningyear[!is.na(df$value)]):max(df$spawningyear[!is.na(df$value)])
34+
# fill out the missing years with NAs
35+
df <- df %>%
36+
select(species, esu_dps, majorpopgroup, esapopname, commonpopname, spawningyear, value, run) %>%
37+
group_by(species, esu_dps, majorpopgroup, esapopname, commonpopname, run) %>%
38+
complete(spawningyear=years, fill=list(value=NA))
39+
40+
# Deal with pops with multiple data
41+
if(any(aa$n)){
42+
cat(aa$esapopname[aa$n], "has duplicated years\n")
43+
df <- df %>% ungroup() %>%
44+
group_by(species, esu_dps, majorpopgroup, esapopname, run, spawningyear) %>%
45+
summarize(value = mean(value, na.rm = TRUE,
46+
commonpopname = commonpopname[1]))
47+
}
48+
esa.salmon <- bind_rows(esa.salmon, df)
49+
}
50+
esa.salmon <- esa.salmon %>% subset(species != "") %>% ungroup()
51+
esa.salmon$log.spawner <- log(esa.salmon$value)
52+
esa.salmon <- rename(esa.salmon, spawner = value)
53+
54+
columbia.river <- NULL
55+
for(i in c(17, 20, 15, 11, 2)){
56+
esuname <- rCAX:::caxesu[i]
57+
a <- rcax_hli("NOSA", type="colnames")
58+
tab <- rcax_hli("NOSA", flist = list(esu_dps = esuname))
59+
# find the pops with no data and remove
60+
tab <- tab %>%
61+
subset((datastatus == "Final" | datastatus == "Reviewed") & bestvalue=="Yes")
62+
if(i == 17 | i == 20) tab$value <- tab$tsaij else tab$value <- tab$tsaej
63+
aa <- tab %>%
64+
group_by(esapopname, run) %>%
65+
summarize(n = sum(value!= "" & value!="0" & majorpopgroup != ""))
66+
bad <- aa[which(aa$n==0),]
67+
aa <- tab %>%
68+
group_by(esapopname, run) %>%
69+
summarize(n = any(duplicated(spawningyear)))
70+
df <- tab %>%
71+
subset(!(esapopname %in% bad$esapopname & run %in% bad$run)) %>%
72+
mutate(value = as.numeric(value))
73+
74+
# get the min and max years in data
75+
years <- min(df$spawningyear[!is.na(df$value)]):max(df$spawningyear[!is.na(df$value)])
76+
# fill out the missing years with NAs
77+
df <- df %>%
78+
select(species, esu_dps, majorpopgroup, esapopname, commonpopname, spawningyear, value, run) %>%
79+
group_by(species, esu_dps, majorpopgroup, esapopname, commonpopname, run) %>%
80+
complete(spawningyear=years, fill=list(value=NA))
81+
82+
# Deal with pops with multiple data
83+
if(any(aa$n)){
84+
cat(aa$esapopname[aa$n], "has duplicated years\n")
85+
df <- df %>% ungroup() %>%
86+
group_by(species, esu_dps, majorpopgroup, esapopname, run, spawningyear) %>%
87+
summarize(value = mean(value, na.rm = TRUE,
88+
commonpopname = commonpopname[1]))
89+
}
90+
if(i == 17 | i == 20) df$value_type <- "tsaij" else df$value_type <- "tsaej"
91+
columbia.river <- bind_rows(columbia.river, df)
92+
}
93+
columbia.river <- columbia.river %>% subset(species != "") %>% ungroup()
94+
columbia.river$log.spawner <- log(columbia.river$value)
95+
columbia.river <- rename(columbia.river, spawner = value)
96+
save(esa.salmon, columbia.river, file=here::here("data", "esa_salmon.RData"))
97+

man/atsalibrary-package.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/esa_salmon.Rd

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)