RcppParallel (NA -> 5.1.9 ) [CRAN]
numDeriv (NA -> 2016.8-1.1) [CRAN]
ps (NA -> 1.8.0 ) [CRAN]
distribut... (NA -> 0.5.0 ) [CRAN]
tensorA (NA -> 0.36.2.1 ) [CRAN]
abind (NA -> 1.4-8 ) [CRAN]
backports (NA -> 1.5.0 ) [CRAN]
processx (NA -> 3.8.4 ) [CRAN]
desc (NA -> 1.4.3 ) [CRAN]
callr (NA -> 3.7.6 ) [CRAN]
posterior (NA -> 1.6.0 ) [CRAN]
matrixStats (NA -> 1.4.1 ) [CRAN]
checkmate (NA -> 2.3.2 ) [CRAN]
BH (NA -> 1.84.0-0 ) [CRAN]
QuickJSR (NA -> 1.4.0 ) [CRAN]
pkgbuild (NA -> 1.4.4 ) [CRAN]
loo (NA -> 2.8.0 ) [CRAN]
gridExtra (NA -> 2.3 ) [CRAN]
inline (NA -> 0.3.19 ) [CRAN]
StanHeaders (NA -> 2.32.10 ) [CRAN]
rstan (NA -> 2.32.6 ) [CRAN]
tmbstan (NA -> 1.0.91 ) [CRAN]
The downloaded binary packages are in
/var/folders/q5/6nls1_g145s2fl1lbvx2zby40000gn/T//RtmpKx0QqR/downloaded_packages
── R CMD build ─────────────────────────────────────────────────────────────────
* checking for file ‘/private/var/folders/q5/6nls1_g145s2fl1lbvx2zby40000gn/T/RtmpKx0QqR/remotes11b64e824194/kaskr-TMB_contrib_R-d275e52/TMBhelper/DESCRIPTION’ ... OK
* preparing ‘TMBhelper’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘TMBhelper_1.4.0.tar.gz’
Code
remotes::install_github("NOAA-FIMS/FIMS")
── R CMD build ─────────────────────────────────────────────────────────────────
* checking for file ‘/private/var/folders/q5/6nls1_g145s2fl1lbvx2zby40000gn/T/RtmpKx0QqR/remotes11b6253f88a2/NOAA-FIMS-FIMS-f0d4e76/DESCRIPTION’ ... OK
* preparing ‘FIMS’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘FIMS_0.2.0.0.tar.gz’
Code
remotes::install_github("r4ss/r4ss")
systemfonts (NA -> 1.1.0 ) [CRAN]
sys (NA -> 3.4.3 ) [CRAN]
askpass (NA -> 1.2.1 ) [CRAN]
openssl (NA -> 2.2.2 ) [CRAN]
curl (NA -> 5.2.3 ) [CRAN]
parallelly (NA -> 1.38.0 ) [CRAN]
listenv (NA -> 0.9.1 ) [CRAN]
globals (NA -> 0.16.3 ) [CRAN]
svglite (NA -> 2.1.3 ) [CRAN]
rstudioapi (NA -> 0.17.0 ) [CRAN]
xml2 (NA -> 1.3.6 ) [CRAN]
ini (NA -> 0.3.1 ) [CRAN]
httr2 (NA -> 1.0.5 ) [CRAN]
gitcreds (NA -> 0.1.2 ) [CRAN]
future (NA -> 1.34.0 ) [CRAN]
viridis (NA -> 0.6.5 ) [CRAN]
kableExtra (NA -> 1.4.0 ) [CRAN]
gh (NA -> 1.4.1 ) [CRAN]
furrr (NA -> 0.3.1 ) [CRAN]
forcats (NA -> 1.0.0 ) [CRAN]
corpcor (NA -> 1.6.10 ) [CRAN]
coda (NA -> 0.19-4.1) [CRAN]
The downloaded binary packages are in
/var/folders/q5/6nls1_g145s2fl1lbvx2zby40000gn/T//RtmpKx0QqR/downloaded_packages
── R CMD build ─────────────────────────────────────────────────────────────────
* checking for file ‘/private/var/folders/q5/6nls1_g145s2fl1lbvx2zby40000gn/T/RtmpKx0QqR/remotes11b633286784/r4ss-r4ss-47f6659/DESCRIPTION’ ... OK
* preparing ‘r4ss’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘r4ss_1.50.0.tar.gz’
The model presented in this case study was changed substantially from the operational version and should not be considered reflective of the pollock stock. This is intended as a demonstration and nothing more.
To get the opertional model to more closely match FIMS I:
Droped surveys 1, 4, and 5
Removed ageing error
Removed length compositions
Removed bias correction in log-normal index likelihoods
Simplified catchability of survey 3 to be constant in time (removed random walk)
Updated maturity to be parametric rather than empirical
Used constant weight at age for all sources: spawning, fishery, surveys, and biomass calculations. The same matrix was used throughout.
Changee timing to be Jan 1 for spawning and all surveys
Removed age accumulation for fishery age compositions
Script to prepare data for building FIMS object
Code
## define the dimensions and global variablesyears <-1970:2023nyears <-length(years)nseasons <-1nages <-10ages <-1:nages## nfleets <- 1## This will fit the models bridging to FIMS (simplifying)## source("fit_bridge_models.R")## compare changes to modelpkfitfinal <-readRDS("data_files/pkfitfinal.RDS")pkfit0 <-readRDS("data_files/pkfit0.RDS")parfinal <- pkfitfinal$obj$env$parList()pkinput0 <-readRDS('data_files/pkinput0.RDS')fimsdat <- pkdat0 <- pkinput0$datpkinput <-readRDS('data_files/pkinput.RDS')
Two extra analyses are demonstrated. First is a likelihood profile over lnR0, showing component contributions and testing for data conflict (a Piner plot). The second is to run the model through the ‘Stan’ software using the ‘tmbstan’ R package. This samples from the posterior, which are put back into the model to get the posterior distribution for spawning stock biomass. Given its long run time the results are saved to a file and read in for post-hoc plotting.
Code
## Try a likelihood profilei <-68# this will break if model is changed at allmap <- parametersmap$p[i] <-NA# map off R0 specified belowmap$p <-as.factor(map$p)xseq <-as.numeric(c(opt$par[i], seq(22,24, len=30)))res <-list()for(j in1:length(xseq)){print(j) parameters$p[i] <- xseq[j] obj2 <-MakeADFun(data =list(), parameters, DLL ="FIMS", silent =TRUE, map=map) opt2 <- TMBhelper::fit_tmb(obj2, getsd=FALSE, newtonsteps=0, control=list(trace=0)) out <- obj2$report(obj2$env$last.par.best) res[[j]] <-data.frame(j=j, lnR0=xseq[j], total=out$jnll, index=out$index_nll,age=out$age_comp_nll,recruit=out$rec_nll, maxgrad=max(abs(obj$gr())))}res <-bind_rows(res) %>%pivot_longer( cols=c(total, index, age, recruit)) %>%group_by(name) %>%mutate(deltaNLL=value-min(value))g <-ggplot(res, aes(lnR0, deltaNLL, color=name)) +geom_line()g <- g+geom_point(data=filter(res, deltaNLL==0), size=2) +labs(y='Delta NLL', color='NLL component')ggsave('figures/AFSC_PK_like_profile_R0.png', g, width=7, height=3.5)## ## Try Bayesian## library(tmbstan)## library(shinystan)## ## Some paraemters wandering off to Inf so fix those (need## ## priors). Needs a ton of work but is proof of concept. Major## ## problem is parallel fails.## map <- parameters## ## parameters$p[65:66]## map$p[65:66] <- NA # map off R0 specified below## map$p <- as.factor(map$p)## obj3 <- MakeADFun(data = list(), parameters, DLL = "FIMS", silent=TRUE, map=map)## opt3 <- TMBhelper::fit_tmb(obj3, getsd=FALSE, newtonsteps=0, control=list(trace=0))## ## Fails when trying to do this in parallel unfortunately## fit <- tmbstan(obj3, chains=1, cores=1, open_progress=FALSE,## init='last.par.best', control=list(max_treedepth=10))## launch_shinystan(fit)## df <- as.data.frame(fit)## df <- df[,-ncol(df)] # drop lp__## ## for each posterior draw, report to get SSB## postreps <- list()## for(ii in 1:nrow(df)){## if(ii %% 10==0) print(ii)## postreps[[ii]] <- obj3$rep(df[ii,])## }## ssbpost <- lapply(postreps, function(x) data.frame(year=years, ssb=x$ssb[[1]][-55]))%>%## bind_rows %>% mutate(rep=rep(1:nrow(df), each=54))## saveRDS(ssbpost, file='pk_SSB_posteriors.RDS')ssbpost <-readRDS('data_files/pk_pollock_SSB_posteriors.RDS')g <-ggplot(ssbpost, aes(year, ssb/1e9, group=rep)) +geom_line(alpha=.1) +ylim(0,NA) +labs(x=NULL, y='SSB (M t)', title='Posterior demo (unconverged!)')ggsave('figures/AFSC_PK_ssb_posterior.png', g, width=7, height=4, units='in')
Plots for extra analyses
Comparison table
The likelihood components from the TMB model do not include constants and thus are not directly comparable. To be fixed later. Relative differences between the modified TMB model and FIMS implementation are given in the figure above.
What was your experience using FIMS? What could we do to improve usability?
To do
List any issues that you ran into or found
Output more derived quantities like selectivity, maturity, etc.
NLL components are not separated by fleet and need to be. So age comp NLL for fleets 1 and 2 need to be separate to make, e.g., the likelihood profile plot above.
Need more ADREPORTed values like SSB
What features are most important to add based on this case study?
More sophisticated control over selectivity so that ages 1 and 2 can be zeroed out for a double-logistic form, overriding the selectivity curve.