|
| 1 | +# example_SimASAP.R |
| 2 | +# walks the user through a set of examples |
| 3 | + |
| 4 | +library("simASAP") |
| 5 | +library("ggplot2") # shouldn't need to do this but doesn't seem to work otherwise |
| 6 | +library("dplyr") # shouldn't need to do this but doesn't seem to work otherwise |
| 7 | + |
| 8 | +# first define where your original ASAP run will be located |
| 9 | +base.dir <- "C:\\Users\\chris.legault\\Desktop\\testSimASAP" |
| 10 | +my.asap.name <- "simplelogistic" |
| 11 | +# assumes you have already run simASAP.dir <- find.package("simASAP") |
| 12 | +file.copy(from = file.path(simASAP.dir, "extdata", paste0(my.asap.name, ".dat")), to = base.dir) |
| 13 | +# get ASAP from the NOAA Fisheries Toolbox and run through the GUI |
| 14 | + |
| 15 | +# now ready to start using simASAP package |
| 16 | +# run SimASAP with defaults to create 10 data sets |
| 17 | +SimASAP(wd=base.dir, asap.name=my.asap.name, nsim=10) |
| 18 | +# the C:\\Users\\chris.legault\\Desktop\\testSimASAP\\sim directory should have been created |
| 19 | +# in this new directory open up two or more of the simplelogistic_simX.dat files |
| 20 | +# and compare the Fleet-1 Catch Data (lines 164-184) and Index-1 and Index-2 Data (lines 281-322) |
| 21 | +# with each other and the original run to see that they have changed |
| 22 | +# can do this by hand or using the ReadASAP3DatFile function |
| 23 | +datorig <- ReadASAP3DatFile(file.path(base.dir, paste0(my.asap.name, ".dat"))) |
| 24 | +datsim1 <- ReadASAP3DatFile(file.path(base.dir, "sim", paste0(my.asap.name, "_sim1.dat"))) |
| 25 | +datsim2 <- ReadASAP3DatFile(file.path(base.dir, "sim", paste0(my.asap.name, "_sim2.dat"))) |
| 26 | +rbind(datorig$dat$CAA_mats[[1]][1, ], |
| 27 | + datsim1$dat$CAA_mats[[1]][1, ], |
| 28 | + datsim2$dat$CAA_mats[[1]][1, ]) |
| 29 | +# note the original file has the catch at age in proportions (the first 10 columns), |
| 30 | +# while the simulated files have the catch at age in numbers (which will be converted to proportions by ASAP automatically when the program is run) |
| 31 | + |
| 32 | +# now actually run ASAP for a new set of random errors by setting runflag to TRUE |
| 33 | +SimASAP(wd=base.dir, asap.name=my.asap.name, nsim=10, runflag=TRUE) |
| 34 | +# the console should show the simulation number and objective function value |
| 35 | +# it will also show when a particular simulation did not converge |
| 36 | + |
| 37 | +# to see how the results from the simulated data compared to the true values use PlotSimASAP |
| 38 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=1) |
| 39 | +# this should produce a plot showing Freport, Recruits, and SSB over time |
| 40 | +# comparing the True values and the estimates from the first simulated data set |
| 41 | + |
| 42 | +# to see the results from multiple simulations change whichsim |
| 43 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=c(1, 3, 5, 7)) |
| 44 | + |
| 45 | +# to see the results from all 10 simulations use whichcim=1:10 |
| 46 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=1:10) |
| 47 | +# note the legend is not shown when there are more than five lines plotted |
| 48 | +# the True values are still shown with dots |
| 49 | + |
| 50 | +# to save the plot set save.plots to TRUE |
| 51 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=1:10, save.plots=TRUE) |
| 52 | + |
| 53 | +# can go wild examining the results, for example, |
| 54 | +# to show just the simulations with the highest and lowest SSB in the terminal year |
| 55 | +res <- PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=1:10, returnwhat="results") |
| 56 | +mysims <- res %>% |
| 57 | + filter(Year == max(Year), metric == "SSB", Source != "True") %>% |
| 58 | + filter(value == min(value) | value == max(value)) %>% |
| 59 | + mutate(simnum = substr(Source, 4, 99)) %>% |
| 60 | + select(simnum) %>% |
| 61 | + unlist(.) %>% |
| 62 | + as.numeric(.) |
| 63 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=mysims) |
| 64 | + |
| 65 | +# what if take away a bunch of data and crank up the CVs? |
| 66 | +base.dir <- "C:\\Users\\chris.legault\\Desktop\\testSimASAP" |
| 67 | +my.asap.name <- "badmodel" |
| 68 | +file.copy(from = file.path(simASAP.dir, "examples", paste0(my.asap.name, ".dat")), to = base.dir) |
| 69 | +# need to run badmodel.dat through ASAP GUI before continuing |
| 70 | +SimASAP(wd=base.dir, asap.name=my.asap.name, nsim=10, runflag=TRUE) |
| 71 | +PlotSimASAP(wd=base.dir, asap.name=my.asap.name, whichsim=1:10) |
| 72 | + |
| 73 | +#------------------------------------------------------ |
| 74 | +# what if use the wrong M in the simulated input files? |
| 75 | +# this is an advanced use of the package requiring more user coding |
| 76 | +file.copy(from = file.path(base.dir, paste0(my.asap.name, ".dat")), |
| 77 | + to = file.path(base.dir, "wrongM.dat")) |
| 78 | +file.copy(from = file.path(base.dir, paste0(my.asap.name, ".rdat")), |
| 79 | + to = file.path(base.dir, "wrongM.rdat")) |
| 80 | + |
| 81 | +wrongM.dir <- file.path(base.dir, "sim", "wrongM") |
| 82 | +if (!dir.exists(wrongM.dir)) dir.create(wrongM.dir) |
| 83 | +file.copy(from = file.path(base.dir, "ASAP3.EXE"), |
| 84 | + to = file.path(wrongM.dir, "ASAP3.EXE")) |
| 85 | +orig.dir <- getwd() |
| 86 | +setwd(wrongM.dir) # unfortunately need to do this to run ASAP3 |
| 87 | + |
| 88 | +for (isim in 1:10){ |
| 89 | + asap.dat <- ReadASAP3DatFile(file.path(base.dir, "sim", paste0(my.asap.name, "_sim", isim, ".dat"))) |
| 90 | + asap.dat$dat$M <- asap.dat$dat$M / 2 # use M=0.15 instead of true M of 0.3 |
| 91 | + wname <- paste0("wrongM_sim", isim) |
| 92 | + my.bad.name <- file.path(base.dir, "sim", "wrongM", paste0(wname, ".dat")) |
| 93 | + WriteASAP3DatFile(my.bad.name, asap.dat, "double true M") |
| 94 | + dname <- paste0(wname, ".dat") |
| 95 | + file.remove("asap3.rdat") |
| 96 | + file.remove("asap3.std") |
| 97 | + shell(paste("ASAP3.exe -ind", dname), intern=TRUE) |
| 98 | + # use presence of .std file to indicate converged run |
| 99 | + if (file.exists("asap3.std")){ |
| 100 | + file.copy(from = "asap3.rdat", to =paste0(wname, ".rdat")) |
| 101 | + asap <- dget("asap3.rdat") |
| 102 | + objfxn <- asap$like$lk.total |
| 103 | + print(paste("simulation", isim, "complete, objective function =", objfxn)) |
| 104 | + }else{ |
| 105 | + print(paste("simulation", isim, "did not converge")) |
| 106 | + } |
| 107 | +} |
| 108 | +setwd(orig.dir) |
| 109 | +wrongM.plot <- PlotSimASAP(base.dir, "wrongM", 1:10, wrongM.dir, FALSE, "plot") |
| 110 | +wrongM.plot <- wrongM.plot + ggtitle("Wrong M (0.15 instead of 0.30) used in assessment") |
| 111 | +ggsave(file = file.path(wrongM.dir, "wrongM.png"), wrongM.plot) |
| 112 | + |
| 113 | +#------------------------------------------------------ |
| 114 | + |
| 115 | +# the following lines are just to copy the file into my examples directory and rename |
| 116 | +# file.copy(from = file.path(od, "comparisonplots.png"), |
| 117 | +# to = paste0("./examples/comparisonplots_", my.asap.name, ".png")) |
| 118 | +# file.copy(from = file.path(base.dir, "simplelogistic.dat"), to = "./examples") |
| 119 | +# file.copy(from = file.path(base.dir, "badmodel.dat"), to = "./examples") |
| 120 | +# file.copy(from = file.path(wrongM.dir, "wrongM.png"), to = "./examples") |
| 121 | + |
| 122 | +# ################################################################### |
| 123 | +# # took a look at a number of actual assessments to see what happens |
| 124 | +# # all this is commented out so users don't try to do it themselves |
| 125 | +# # included here to demonstrate how it can be done |
| 126 | +# |
| 127 | +# # groundfish |
| 128 | +# # ASAP assessment input files from https://www.nefsc.noaa.gov/saw/sasi/sasi_report_options.php |
| 129 | +# base.dir <- "C:\\Users\\chris.legault\\Desktop\\jitter_asap\\" |
| 130 | +# gstocks <- c("gomcod", "gomhaddock", "pollock", "redfish", "snemawinter", "snemayt", "whitehake") |
| 131 | +# nstocks <- length(gstocks) |
| 132 | +# gname <- "base" # did not have to do this, just an easier way of running through many cases in a loop |
| 133 | +# |
| 134 | +# # run each model 10 times |
| 135 | +# for (istock in 1:nstocks){ |
| 136 | +# wd <- file.path(base.dir, gstocks[istock]) |
| 137 | +# SimASAP(wd=wd, asap.name=gname, nsim=10, runflag=TRUE) |
| 138 | +# } |
| 139 | +# |
| 140 | +# # make the plots and modify them to include stock name as title (nice feature of ggplot) |
| 141 | +# gres <- list() |
| 142 | +# for (istock in 1:nstocks){ |
| 143 | +# wd <- file.path(base.dir, gstocks[istock]) |
| 144 | +# myplot <- PlotSimASAP(wd, gname, 1:10, returnwhat="plot") |
| 145 | +# gres[[istock]] <- myplot + ggtitle(gstocks[istock]) |
| 146 | +# } |
| 147 | +# # no indications of problems with any of these |
| 148 | +# |
| 149 | +# pdf(file=file.path(base.dir, "groundfish_comparison_plots.pdf")) |
| 150 | +# for (istock in 1:nstocks){ |
| 151 | +# print(gres[[istock]]) |
| 152 | +# } |
| 153 | +# dev.off() |
| 154 | +# file.copy(from=file.path(base.dir, "groundfish_comparison_plots.pdf"), to="./examples") |
| 155 | +# |
| 156 | + |
0 commit comments