Skip to content

Commit fb7b651

Browse files
committed
add lab 2
1 parent ab7ced8 commit fb7b651

26 files changed

+5125
-4
lines changed
1.68 MB
Loading
1.9 MB
Loading
2.79 MB
Loading
1.74 MB
Loading
11.8 KB
Binary file not shown.
25.8 KB
Binary file not shown.
25.8 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Create data files
2+
library(rCAX)
3+
library(tidyr)
4+
columbia.river <- NULL
5+
for(i in c(17, 20, 15, 11, 2)){
6+
esuname <- rCAX:::caxesu[i]
7+
a <- rcax_hli("NOSA", type="colnames")
8+
tab <- rcax_hli("NOSA", flist = list(esu_dps = esuname))
9+
# find the pops with no data and remove
10+
tab <- tab %>%
11+
subset((datastatus == "Final" | datastatus == "Reviewed") & bestvalue=="Yes")
12+
if(i == 17 | i == 20) tab$value <- tab$tsaij else tab$value <- tab$tsaej
13+
aa <- tab %>%
14+
group_by(esapopname, run) %>%
15+
summarize(n = sum(value!= "" & value!="0" & majorpopgroup != ""))
16+
bad <- aa[which(aa$n==0),]
17+
aa <- tab %>%
18+
group_by(esapopname, run) %>%
19+
summarize(n = any(duplicated(spawningyear)))
20+
df <- tab %>%
21+
subset(!(esapopname %in% bad$esapopname & run %in% bad$run)) %>%
22+
mutate(value = as.numeric(value))
23+
24+
# get the min and max years in data
25+
years <- min(df$spawningyear[!is.na(df$value)]):max(df$spawningyear[!is.na(df$value)])
26+
# fill out the missing years with NAs
27+
df <- df %>%
28+
select(species, esu_dps, majorpopgroup, esapopname, commonpopname, spawningyear, value, run) %>%
29+
group_by(species, esu_dps, majorpopgroup, esapopname, commonpopname, run) %>%
30+
complete(spawningyear=years, fill=list(value=NA))
31+
32+
# Deal with pops with multiple data
33+
if(any(aa$n)){
34+
cat(aa$esapopname[aa$n], "has duplicated years\n")
35+
df <- df %>% ungroup() %>%
36+
group_by(species, esu_dps, majorpopgroup, esapopname, run, spawningyear) %>%
37+
summarize(value = mean(value, na.rm = TRUE,
38+
commonpopname = commonpopname[1]))
39+
}
40+
if(i == 17 | i == 20) df$value_type <- "tsaij" else df$value_type <- "tsaej"
41+
columbia.river <- bind_rows(columbia.river, df)
42+
}
43+
columbia.river <- columbia.river %>% subset(species != "") %>% ungroup()
44+
save(columbia.river, file = file.path("Lab-2", "Data_Images/columbia-river.rda"))
45+
46+
unique(lower.columbia.river$esu_dps)
47+
df <- lower.columbia.river %>% subset(species == "Steelhead" & run == "Winter")
48+
ggplot(df, aes(x=spawningyear, y=log(value), color=majorpopgroup)) +
49+
geom_point(size=0.2) +
50+
theme(strip.text.x = element_text(size = 2)) +
51+
theme(axis.text.x = element_text(size = 5, angle = 90)) +
52+
facet_wrap(~esapopname)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Create data files
2+
# remotes:::install_github("nwfsc-cb/rCAX@*release")
3+
library(rCAX)
4+
library(tidyr)
5+
x <- rCAX:::caxesu
6+
valid_esu <- which(!str_detect(x, "XN") & !str_detect(x,"N/A"))
7+
esa.salmon <- NULL
8+
for(i in valid_esu){
9+
esuname <- rCAX:::caxesu[i]
10+
print(esuname)
11+
a <- rcax_hli("NOSA", type="colnames")
12+
tab <- rcax_hli("NOSA", flist = list(esu_dps = esuname))
13+
# error no data
14+
if (!is.data.frame(tab)) next
15+
# find the pops with no data and remove
16+
tab <- tab %>%
17+
subset((datastatus == "Final" | datastatus == "Reviewed") & bestvalue=="Yes")
18+
if(tab$tsaej[1]=="") tab$value <- tab$tsaij else tab$value <- tab$tsaej
19+
aa <- tab %>%
20+
group_by(esapopname, run) %>%
21+
summarize(n = sum(value!= "" & value!="0" & majorpopgroup != ""))
22+
bad <- aa[which(aa$n==0),]
23+
aa <- tab %>%
24+
group_by(esapopname, run) %>%
25+
summarize(n = any(duplicated(spawningyear)))
26+
df <- tab %>%
27+
subset(!(esapopname %in% bad$esapopname & run %in% bad$run)) %>%
28+
mutate(value = as.numeric(value))
29+
30+
# get the min and max years in data
31+
years <- min(df$spawningyear[!is.na(df$value)]):max(df$spawningyear[!is.na(df$value)])
32+
# fill out the missing years with NAs
33+
df <- df %>%
34+
select(species, esu_dps, majorpopgroup, esapopname, commonpopname, spawningyear, value, run) %>%
35+
group_by(species, esu_dps, majorpopgroup, esapopname, commonpopname, run) %>%
36+
complete(spawningyear=years, fill=list(value=NA))
37+
38+
# Deal with pops with multiple data
39+
if(any(aa$n)){
40+
cat(aa$esapopname[aa$n], "has duplicated years\n")
41+
df <- df %>% ungroup() %>%
42+
group_by(species, esu_dps, majorpopgroup, esapopname, run, spawningyear) %>%
43+
summarize(value = mean(value, na.rm = TRUE,
44+
commonpopname = commonpopname[1]))
45+
}
46+
#if(i == 17 | i == 20) df$value_type <- "tsaij" else df$value_type <- "tsaej"
47+
esa.salmon <- bind_rows(esa.salmon, df)
48+
}
49+
esa.salmon <- esa.salmon %>% subset(species != "") %>% ungroup()
50+
save(esa.salmon, file = file.path("Lab-2", "Data_Images/esa-salmon.rda"))
51+
52+
unique(esa.salmon$esu_dps)
53+
df <- esa.salmon %>% subset(species == "Steelhead" & run == "Winter")
54+
ggplot(df, aes(x=spawningyear, y=log(value), color=majorpopgroup)) +
55+
geom_point(size=0.2) +
56+
theme(strip.text.x = element_text(size = 2)) +
57+
theme(axis.text.x = element_text(size = 5, angle = 90)) +
58+
facet_wrap(~esapopname)

0 commit comments

Comments
 (0)