-
Notifications
You must be signed in to change notification settings - Fork 0
/
2. Forward Projections.R
226 lines (146 loc) · 5.98 KB
/
2. Forward Projections.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
library(SAMSE)
MPs <- c('StatusQuo', 'RecEff20', 'RecEff40', 'Ftarget', 'MLL20_25', 'MLL25_25')
incRecEMPs <- paste0(MPs, '_IncRecEff')
# ---- Loop through OMs and Run Projections ----
OM_hists <- list.files('Hist_Objects', pattern='.hist')
for (i in seq_along(OM_hists)) {
multiHist <- readRDS(file.path('Hist_Objects', OM_hists[i]))
if (i == 6) {
MSE <- ProjectMOM(multiHist, MPs=incRecEMPs,
dropHist = FALSE)
} else {
MSE <- ProjectMOM(multiHist, MPs=MPs,
dropHist = FALSE)
}
rm(multiHist)
fl <- paste0(tools::file_path_sans_ext(OM_hists[i]), '.mmse')
saveRDS(MSE, file.path('MSE_Objects', fl))
rm(MSE)
}
# ---- Calculate Summary Statistics & Performance Metrics ----
library(openMSE)
OMs <- paste0("OM_", unlist(strsplit(OM_hists, '.hist')))
MSE_info <- vector('list', length(OMs))
names(MSE_info) <- OMs
for (i in seq_along(OMs)) {
fl <- paste0(unlist(strsplit(OM_hists[i], '.hist')), '.mmse')
MSE <- readRDS(file.path('MSE_Objects', fl))
structure_fleets <- function(df) {
fleet_names <- as.character(unique(df$Fleet))
fleet_names2 <- lapply(strsplit(fleet_names, ':'), '[', 1) %>% unlist()
for (i in seq_along(fleet_names)) {
df <- df %>% mutate(Fleet=dplyr::case_match(Fleet, fleet_names[i]~fleet_names2[i], .default=Fleet))
}
if (!is.null(df$MP)) {
df <- df %>% group_by(Year, Sim, Stock, Fleet, MP, Variable) %>% summarise(Value=sum(Value),
.groups='drop')
} else {
df <- df %>% group_by(Year, Sim, Stock, Fleet, Variable) %>% summarise(Value=sum(Value),
.groups='drop')
}
df$Fleet <- factor(df$Fleet, levels=unique(fleet_names2), ordered = TRUE)
df
}
add_factor <- function(df) {
df$Stock <- factor(df$Stock, levels=unique(df$Stock), ordered = TRUE)
if (!is.null(df[['MP']])) {
ind <- grepl('_IncRecEff', unique(df$MP))
if (any(ind)) {
df$MP <- strsplit(df$MP, '_IncRecEff') %>% unlist()
}
df$MP <- factor(df$MP, levels=unique(df$MP), ordered = TRUE)
}
if (!is.null(df$Fleet)) {
df <- structure_fleets(df)
}
df
}
# Historical
Ref_Points <- Calculate_Ref_Points(MSE@multiHist) %>% add_factor()
MSE_info[[i]]$Ref_Points <- Ref_Points
SB <- get_SSB(MSE@multiHist) %>% filter(Sim==1) %>% add_factor()
Landings <- get_Landings(MSE@multiHist) %>% filter(Sim==1) %>% add_factor()
Removals <- get_Removals(MSE@multiHist) %>% filter(Sim==1) %>% add_factor()
Fishing_Mortality <- get_F(MSE@multiHist) %>% filter(Sim==1) %>% add_factor()
Discards <- Removals
Discards$Variable <- 'Discards'
Discards$Value <- Removals$Value - Landings$Value
Discards$Value[ Discards$Value<0] <- 0
Fishing_Mortality <- get_F(MSE@multiHist) %>% filter(Sim==1) %>% add_factor()
MSE_info[[i]]$Historical <- dplyr::bind_rows(Landings, Discards, SB, Fishing_Mortality)
# Projection
SB <- get_SSB(MSE) %>% add_factor()
Landings <- get_Landings(MSE) %>% add_factor()
Removals <- get_Removals(MSE) %>% add_factor()
Discards <- Removals
Discards$Variable <- 'Discards'
Discards$Value <- Removals$Value - Landings$Value
Discards$Value[ Discards$Value<0] <- 0
Fishing_Mortality <- get_F(MSE) %>% add_factor()
MSE_info[[i]]$Projection <- dplyr::bind_rows(Landings, Discards, SB, Fishing_Mortality)
}
saveRDS(MSE_info, 'inst/shiny_app/Data/MSE_info.rda')
tt = MSE_info$OM_01$Projection %>% filter(Stock=='Gag Grouper', MP=='Ftarget', Variable=='Landings',
Year==2025)
tt = tt %>% group_by(Year, Sim) %>% summarise(Value=sum(Value))
openMSE::kg2_1000lb(median(tt$Value))
tt =MSE_info$OM_01$Projection %>% filter(Stock=='Gag Grouper', MP=='Ftarget', Variable=='Landings',
Fleet=='Recreational Headboat') %>%
group_by(Year) %>%
summarise(median=openMSE::kg2_1000lb(median(Value)))
plot(tt$Year, tt$median, type='l')
Ref_Points <- MSE_info[[1]]$Ref_Points
SSBhist <- MSE_info[[1]]$Historical$SSB
Catch <- MSE_info[[1]]$Historical$Catch
plot_SB_hist <- function(SSBhist, Ref_Points, inc_ref_point=TRUE, rel_to=NA, ymax=NULL) {
ref_point <- Ref_Points %>%
tidyr::pivot_longer(., cols=1:4, names_to = 'Reference Point') %>%
filter(`Reference Point` %in% c("SBtarg", 'MSST'))
SSBhist <- left_join(SSBhist, Ref_Points, by = join_by(Stock)) %>%
tidyr::pivot_longer(., cols=c('F', 'SPR', 'SBtarg', 'MSST'),
names_to = 'Reference Point') %>%
filter(`Reference Point` %in% c("SBtarg", 'MSST'))
if (!is.na(rel_to)) {
SSBhist <- SSBhist %>% group_by(Stock) %>% mutate(Value=Value/value[`Reference Point`==rel_to],
value=value/value[`Reference Point`==rel_to])
}
p <- ggplot(SSBhist) +
facet_wrap(~Stock, scale='free', ncol=1) +
geom_line( aes(x=Year, y=Value)) +
expand_limits(y=0) +
theme_bw() +
labs(y=unique(SSBhist$Variable)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
strip.text = element_text(size=16,face="bold"),
legend.position="bottom")
if (!is.null(ymax)) {
dummy <- data.frame(x=rep(range(SSBhist$Year),2),
Stock =rep(unique(SSBhist$Stock), each=2),
y=c(0, ymax[[1]], 0, ymax[[2]]))
p <- p + geom_blank(data=dummy, aes(x=x, y=y))
}
if (inc_ref_point) {
p <- p + geom_hline(aes(yintercept=value, linetype=`Reference Point`))
}
p
}
p + geom_hline(aes(yintercept=MSST), linetype=2) +
geom_hline(aes(yintercept=SBtarg), linetype=3)
# Make list of MSE results to load into Shiny
# Historical OM
## SSB
## Catch
## Fishing Mortality
# Projections
## SSB
## Catch
## Fishing Mortality
# Performance Metrics
F_DF <- get_F(MSE)
plot_Fmort(MSE)
plot_Catch(MSE)
plot_SB(MSE)
Landings_10(MSE)
Landings_20(MSE)
avail('PM', 'SAMSE')