|
| 1 | +#NOTES: Things to discuss |
| 2 | +# -- Code assumes no seasons so will not work correctly with a seasonal model do |
| 3 | +# we need this capacity for any species we may want to include? |
| 4 | +# |
| 5 | +# -- No recruitment variability included at the moment. Is this important to add or |
| 6 | +# just more noise to confuse the issue. Should we wait for reviewer feedback and |
| 7 | +# just add it if they as for it? I don't think it will have any practical impact. |
| 8 | +# |
| 9 | +# -- Current approach uses fixed future F values for the simulation with approximates |
| 10 | +# a best case scenario where F_target stays the same and the fishery OFL is constantly |
| 11 | +# being updated to achieve this (such as through accurate interim assessments). I |
| 12 | +# think adding catch based removals will probably just exaggerate the impacts and |
| 13 | +# distract from the red tide effect by allowing claims that future assessments |
| 14 | +# would correct for the impacts. |
| 15 | +# |
| 16 | +# -- Which species should we try to implement this for (Red Grouper, Gag, ???) |
| 17 | +# |
| 18 | +# -- What outputs do we want to record (Landings, SSB, ???) |
| 19 | + |
| 20 | +library(r4ss) |
| 21 | +#Source setup file that should be named local.setup so that it will be |
| 22 | +#ignored by github tracking. |
| 23 | +# |
| 24 | +local.setup.location <- "C:/Users/Nathan/Documents/GitHub/Red_tide_benchmarks/local.setup" |
| 25 | +source(local.setup.location) |
| 26 | + |
| 27 | +#Source in the SEFSC projections function |
| 28 | +source(projection_script) |
| 29 | + |
| 30 | +#Copy files to the simulation folder |
| 31 | +if(dir.exists(file.path(working_dir))){ |
| 32 | + unlink(file.path(working_dir), recursive = TRUE) |
| 33 | +} |
| 34 | +dir.create(file.path(working_dir)) |
| 35 | +dir.create(file.path(working_dir,"Base")) |
| 36 | +temp.files <- list.files(path=file.path(assessment_dir)) |
| 37 | +file.copy(from = file.path(assessment_dir,temp.files), to = file.path(working_dir,"Base",temp.files)) |
| 38 | + |
| 39 | +#Read forecast file to get N_forecast years and base file for overwriting other runs |
| 40 | +forecast_base <- r4ss::SS_readforecast(file=file.path(working_dir,"Base","forecast.ss")) |
| 41 | +base_output <- r4ss::SS_output(file.path(working_dir,"Base"),covar = FALSE) |
| 42 | + |
| 43 | +#Projection red tide values |
| 44 | +rt_proj_ave <- sort(seq(0,0.1,0.01)) |
| 45 | + |
| 46 | +#True red tide averages |
| 47 | +rt_mean <- sort(c(0.01,0.03,0.06)) |
| 48 | + |
| 49 | +#How many random red tide replicates to run |
| 50 | +n_rand_reps <- 500 |
| 51 | + |
| 52 | +#Set seed to allow replication of results |
| 53 | +global.seed <- 1234 |
| 54 | +set.seed(global.seed) |
| 55 | +rand_offset <- 0 #offset to avoid using same seed as previous runs |
| 56 | +rand_seed <- floor(runif((n_rand_reps+rand_offset),100000,9999999))[(rand_offset+1):(n_rand_reps+rand_offset)] |
| 57 | + |
| 58 | +#Identify the fleet associated with red tide |
| 59 | +rt_fleet <- 5 |
| 60 | + |
| 61 | +#Identify fleets to include in landings calculations |
| 62 | +landings_fleets <- 1:4 |
| 63 | + |
| 64 | +fleet_landings_cols <- grep("retain(B)",colnames(base_output$timeseries),fixed=TRUE)[landings_fleets] |
| 65 | + |
| 66 | +#Setup the random red tide mortality vector details |
| 67 | +#Set the range for the number of red tide events in the projection period of 100 years |
| 68 | +n_rt_events_min <- 5 #The minimum number of red tide events during the projection period |
| 69 | +n_rt_events_max <- 20 #The maximum number of red tide events during the projection period |
| 70 | + |
| 71 | +#Set the relative range for red tide in a single year these values will be rescaled |
| 72 | +#in each simulation so the total red tide mortality is always sums to the target mean |
| 73 | +rt_min <- 0.1 #Relative value of the minimum red tide in a single year |
| 74 | +rt_max <- 0.4 #Relative value of the maximum red tide in a single year |
| 75 | + |
| 76 | +#Set up output matrices for storing values of interest |
| 77 | +#Data frame to track the iteration settings for each row of the results for indexing |
| 78 | +results_setting <- data.frame(rt_projected=c(sort(rep(rt_proj_ave,length(rt_mean)*n_rand_reps))), |
| 79 | + rt_mean=c(rep(sort(rep(rt_mean,n_rand_reps)),length(rt_proj_ave))), |
| 80 | + replicate=c(rep(1:n_rand_reps,length(rt_mean)*length(rt_proj_ave)))) |
| 81 | +#Achieved OFL landings |
| 82 | +results_landings <- matrix(data=NA,nrow=(length(rt_proj_ave)*length(rt_mean)*n_rand_reps),ncol=(forecast_base$Nforecastyrs+base_output$endyr-base_output$startyr+1)) |
| 83 | +#Achieved SSB |
| 84 | +results_SSB <- matrix(data=NA,nrow=(length(rt_proj_ave)*length(rt_mean)*n_rand_reps),ncol=(forecast_base$Nforecastyrs+base_output$endyr-base_output$startyr+1)) |
| 85 | +#Target SPR |
| 86 | +results_SPR <- matrix(data=NA,nrow=(length(rt_proj_ave)*length(rt_mean)*n_rand_reps),ncol=(forecast_base$Nforecastyrs+base_output$endyr-base_output$startyr+1)) |
| 87 | +#Target depletion |
| 88 | +results_dep <- matrix(data=NA,nrow=(length(rt_proj_ave)*length(rt_mean)*n_rand_reps),ncol=(forecast_base$Nforecastyrs+base_output$endyr-base_output$startyr+1)) |
| 89 | + |
| 90 | +#Index to track row for filling results data |
| 91 | +index_row <- 1 |
| 92 | + |
| 93 | + |
| 94 | +#First loop over the red tide rate included in projections as this will only |
| 95 | +#need the projections to be calculated once. |
| 96 | +for(i in seq_along(rt_proj_ave)){ |
| 97 | + #remove exisiting base folders if found |
| 98 | + proj_dir <- file.path(working_dir,paste0("rtproj_",i)) |
| 99 | + if(dir.exists(proj_dir)){ |
| 100 | + unlink(proj_dir, recursive = TRUE) |
| 101 | + } |
| 102 | + #Create new base folders and copy over the original model files |
| 103 | + dir.create(proj_dir) |
| 104 | + dir.create(file.path(proj_dir,"Base")) |
| 105 | + temp.files <- list.files(path=file.path(working_dir,"Base")) |
| 106 | + file.copy(from = file.path(working_dir,"Base",temp.files), to = file.path(proj_dir,"Base",temp.files)) |
| 107 | + |
| 108 | + #Adjust the redtide values for the base projection and rerun the projections |
| 109 | + #to estimate OFL. |
| 110 | + #For now I'm leaving out ABC as I think it distracts from the intent of the |
| 111 | + #simulation because ABC is supposed to account for unknown uncertainty not |
| 112 | + #offset an avoidable bias such as this. |
| 113 | + #This uses the average red tide rate in every projection year |
| 114 | + forecast_base$ForeCatch[forecast_base$ForeCatch$Fleet==rt_fleet,4] <- rep(rt_proj_ave[i],forecast_base$Nforecastyrs) |
| 115 | + |
| 116 | + r4ss::SS_writeforecast(mylist=forecast_base,dir=file.path(proj_dir,"Base"),overwrite=TRUE) |
| 117 | + |
| 118 | + base_proj <- run.projections(file.path(proj_dir,"Base")) |
| 119 | + |
| 120 | + #Loop over all mean red tide level scenarios |
| 121 | + for(j in seq_along(rt_mean)){ |
| 122 | + #remove exisiting mean red tide folders if found |
| 123 | + rt_dir <- file.path(proj_dir,paste0("rt_mean_",j)) |
| 124 | + if(dir.exists(rt_dir)){ |
| 125 | + unlink(rt_dir, recursive = TRUE) |
| 126 | + } |
| 127 | + #Create new folder for each mean red tide level and copy over the original model files |
| 128 | + dir.create(rt_dir) |
| 129 | + |
| 130 | + #Loop over all random red tide sequences |
| 131 | + for(k in 1:n_rand_reps){ |
| 132 | + #reset random seed for each random replicate seeds will be replicated across |
| 133 | + #projected red tide levels and mean red tide levels |
| 134 | + |
| 135 | + set.seed(rand_seed[k]) |
| 136 | + #Create folders for each random sequence |
| 137 | + dir.create(file.path(rt_dir,k)) |
| 138 | + temp.files <- list.files(path=file.path(proj_dir,"Base","OFL_target")) |
| 139 | + file.copy(from = file.path(proj_dir,"Base","OFL_target",temp.files), to = file.path(rt_dir,k,temp.files)) |
| 140 | + |
| 141 | + #Calculate a random red tide mortality vector based on specified mean and frequency |
| 142 | + #draw a random number of red tide events from a uniform distribution between min and max number specified |
| 143 | + n_rt_events <- sample(n_rt_events_min:n_rt_events_max,1) # |
| 144 | + #calculate the total red tide mortality expected from the specified mean and number of projection years |
| 145 | + rt_total <- rt_mean[j]*forecast_base$Nforecastyrs |
| 146 | + #calculate random mortality rates from each event from a uniform distribution between min and max number specified |
| 147 | + rt_mags <- runif(n_rt_events,rt_min,rt_max) |
| 148 | + #rescale the red tide magnitudes so that they sum to the expected total mortality |
| 149 | + rt_mags <- rt_mags*(rt_total/sum(rt_mags)) |
| 150 | + #create a zero mortality vector for all years |
| 151 | + rand_red_tide <- rep(0,forecast_base$Nforecastyrs) |
| 152 | + #randomly select years for the red tide mortality to occur and replace zero's with random mortality rates |
| 153 | + rand_red_tide[sample(1:forecast_base$Nforecastyrs,n_rt_events)] <- rt_mags |
| 154 | + |
| 155 | + |
| 156 | + #Modify forecast file to include random red tide mortality sequence |
| 157 | + forecast_rt <- r4ss::SS_readforecast(file=file.path(rt_dir,k,"forecast.ss")) |
| 158 | + forecast_rt$ForeCatch[forecast_rt$ForeCatch$Fleet==rt_fleet,4] <- rand_red_tide |
| 159 | + #Write out the new forecast file and run model with new random mortality vector |
| 160 | + r4ss::SS_writeforecast(mylist=forecast_rt,dir=file.path(rt_dir,k),overwrite=TRUE) |
| 161 | + shell(paste("cd /d ",file.path(rt_dir,k)," && ss -nohess",sep="")) |
| 162 | + |
| 163 | + #Read in results and save values of interest for analysis |
| 164 | + run_output <- r4ss::SS_output(dir=file.path(rt_dir,k),covar = FALSE) |
| 165 | + |
| 166 | + spr_series <- run_output$sprseries |
| 167 | + |
| 168 | + time_series <- run_output$timeseries |
| 169 | + time_series_virg <- time_series[time_series$Era=="VIRG",] |
| 170 | + time_series <- time_series[time_series$Era!="VIRG" & time_series$Era!="INIT",] |
| 171 | + years <- unique(time_series$Yr) |
| 172 | + for(i in seq_along(years)){ |
| 173 | + time_series_sub <- time_series[time_series$Yr==years[i],,drop=FALSE] |
| 174 | + spr_series_sub <- spr_series[spr_series$Yr==years[i],,drop=FALSE] |
| 175 | + results_landings[index_row,i] <- sum(time_series_sub[,fleet_landings_cols]) |
| 176 | + results_SSB[index_row,i] <- sum(time_series_sub[,'SpawnBio']) |
| 177 | + results_SPR[index_row,i] <- sum(spr_series_sub[,'SPR']) |
| 178 | + results_dep[index_row,i] <- sum(spr_series_sub[,'Deplete']) |
| 179 | + } |
| 180 | + index_row <- index_row+1 |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +all_results <- list() |
| 186 | +all_results[[1]] <- results_landings |
| 187 | +all_results[[2]] <- results_SSB |
| 188 | +all_results[[3]] <- results_SPR |
| 189 | +all_results[[4]] <- results_dep |
| 190 | + |
| 191 | +save(all_results,file=save_file) |
| 192 | + |
| 193 | +#Summarize results for display |
| 194 | +summary_index <- 1 |
| 195 | + |
| 196 | +results_summary_setup <- data.frame(rt_projected=c(sort(rep(rt_proj_ave,length(rt_mean)))), |
| 197 | + rt_mean=c(rep(sort(rt_mean),length(rt_proj_ave)))) |
| 198 | + |
| 199 | +results_landings_summary_mean <- results_landings[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 200 | +results_landings_summary_sd <- results_landings[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 201 | + |
| 202 | +results_SSB_summary_mean <- results_SSB[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 203 | +results_SSB_summary_sd <- results_SSB[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 204 | + |
| 205 | +results_SPR_summary_mean <- results_SPR[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 206 | +results_SPR_summary_sd <- results_SPR[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 207 | + |
| 208 | +results_dep_summary_mean <- results_dep[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 209 | +results_dep_summary_sd <- results_dep[1:(length(rt_proj_ave)*length(rt_mean)),] |
| 210 | + |
| 211 | +for(i in seq_along(rt_proj_ave)){ |
| 212 | + for(j in seq_along(rt_mean)){ |
| 213 | + rows <- which(results_setting[,"rt_projected"]==rt_proj_ave[i] & results_setting[,"rt_mean"]==rt_mean[j]) |
| 214 | + |
| 215 | + results_landings_summary_mean[summary_index,] <- apply(results_landings[rows,],2,mean) |
| 216 | + results_landings_summary_sd[summary_index,] <- apply(results_landings[rows,],2,sd) |
| 217 | + |
| 218 | + results_SSB_summary_mean[summary_index,] <- apply(results_SSB[rows,],2,mean) |
| 219 | + results_SSB_summary_sd[summary_index,] <- apply(results_SSB[rows,],2,sd) |
| 220 | + |
| 221 | + results_SPR_summary_mean[summary_index,] <- apply(results_SPR[rows,],2,mean) |
| 222 | + results_SPR_summary_sd[summary_index,] <- apply(results_SPR[rows,],2,sd) |
| 223 | + |
| 224 | + results_dep_summary_mean[summary_index,] <- apply(results_dep[rows,],2,mean) |
| 225 | + results_dep_summary_sd[summary_index,] <- apply(results_dep[rows,],2,sd) |
| 226 | + |
| 227 | + summary_index <- summary_index + 1 |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | + |
| 232 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_landings),max(results_landings))) |
| 233 | +for(i in seq_along(results_landings_summary_mean[,1])) |
| 234 | +{ |
| 235 | + if(results_summary_setup[i,"rt_mean"]==0.06){ |
| 236 | + if(results_summary_setup[i,"rt_projected"]<results_summary_setup[i,"rt_mean"]){ |
| 237 | + lines(x=years,y=results_landings_summary_mean[i,],col="red") |
| 238 | + }else if(results_summary_setup[i,"rt_projected"]>results_summary_setup[i,"rt_mean"]){ |
| 239 | + lines(x=years,y=results_landings_summary_mean[i,],col="blue") |
| 240 | + }else{ |
| 241 | + lines(x=years,y=results_landings_summary_mean[i,],col="green") |
| 242 | + } |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_SSB),max(results_SSB))) |
| 247 | +for(i in seq_along(results_SSB_summary_mean[,1])) |
| 248 | +{ |
| 249 | + if(results_summary_setup[i,"rt_mean"]==0.06){ |
| 250 | + if(results_summary_setup[i,"rt_projected"]<results_summary_setup[i,"rt_mean"]){ |
| 251 | + lines(x=years,y=results_SSB_summary_mean[i,],col="red") |
| 252 | + }else if(results_summary_setup[i,"rt_projected"]>results_summary_setup[i,"rt_mean"]){ |
| 253 | + lines(x=years,y=results_SSB_summary_mean[i,],col="blue") |
| 254 | + }else{ |
| 255 | + lines(x=years,y=results_SSB_summary_mean[i,],col="green") |
| 256 | + } |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_SPR),max(results_SPR))) |
| 261 | +for(i in seq_along(results_SPR_summary_mean[,1])) |
| 262 | +{ |
| 263 | + if(results_summary_setup[i,"rt_mean"]==0.06){ |
| 264 | + if(results_summary_setup[i,"rt_projected"]<results_summary_setup[i,"rt_mean"]){ |
| 265 | + lines(x=years,y=results_SPR_summary_mean[i,],col="red") |
| 266 | + }else if(results_summary_setup[i,"rt_projected"]>results_summary_setup[i,"rt_mean"]){ |
| 267 | + lines(x=years,y=results_SPR_summary_mean[i,],col="blue") |
| 268 | + }else{ |
| 269 | + lines(x=years,y=results_SPR_summary_mean[i,],col="green") |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_dep),max(results_dep))) |
| 275 | +for(i in seq_along(results_dep_summary_mean[,1])) |
| 276 | +{ |
| 277 | + if(results_summary_setup[i,"rt_mean"]==0.06){ |
| 278 | + if(results_summary_setup[i,"rt_projected"]<results_summary_setup[i,"rt_mean"]){ |
| 279 | + lines(x=years,y=results_dep_summary_mean[i,],col="red") |
| 280 | + }else if(results_summary_setup[i,"rt_projected"]>results_summary_setup[i,"rt_mean"]){ |
| 281 | + lines(x=years,y=results_dep_summary_mean[i,],col="blue") |
| 282 | + }else{ |
| 283 | + lines(x=years,y=results_dep_summary_mean[i,],col="green") |
| 284 | + } |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | + |
| 291 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_landings),max(results_landings))) |
| 292 | +for(i in seq_along(results_landings[,1])) |
| 293 | +{ |
| 294 | + lines(x=years,y=results_landings[i,]) |
| 295 | +} |
| 296 | + |
| 297 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_SSB),max(results_SSB))) |
| 298 | +for(i in seq_along(results_SSB[,1])) |
| 299 | +{ |
| 300 | + lines(x=years,y=results_SSB[i,]) |
| 301 | +} |
| 302 | + |
| 303 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_SPR),max(results_SPR))) |
| 304 | +for(i in seq_along(results_SPR[,1])) |
| 305 | +{ |
| 306 | + lines(x=years,y=results_SPR[i,]) |
| 307 | +} |
| 308 | + |
| 309 | +plot(x=NA,y=NA,xlim=c(min(years),max(years)),ylim=c(min(results_dep),max(results_dep))) |
| 310 | +for(i in seq_along(results_dep[,1])) |
| 311 | +{ |
| 312 | + lines(x=years,y=results_dep[i,]) |
| 313 | +} |
| 314 | + |
0 commit comments