{"id":19421009,"url":"https://github.com/pbs-assess/recruitea","last_synced_at":"2026-06-12T04:31:35.151Z","repository":{"id":231747962,"uuid":"782604887","full_name":"pbs-assess/recruitea","owner":"pbs-assess","description":"An R package to investigate the impacts of ecosystem variability on recruitment using full MCMC results","archived":false,"fork":false,"pushed_at":"2024-04-10T20:17:22.000Z","size":20062,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-11-19T04:19:15.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pbs-assess.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-05T16:31:20.000Z","updated_at":"2024-04-05T17:01:28.000Z","dependencies_parsed_at":"2024-11-10T13:28:58.133Z","dependency_job_id":"b4745fed-1358-4b0a-8305-1904ace5d63b","html_url":"https://github.com/pbs-assess/recruitea","commit_stats":null,"previous_names":["pbs-assess/recruitea"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pbs-assess/recruitea","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbs-assess%2Frecruitea","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbs-assess%2Frecruitea/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbs-assess%2Frecruitea/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbs-assess%2Frecruitea/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pbs-assess","download_url":"https://codeload.github.com/pbs-assess/recruitea/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbs-assess%2Frecruitea/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34229624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T13:26:30.164Z","updated_at":"2026-06-12T04:31:35.126Z","avatar_url":"https://github.com/pbs-assess.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file.\nBuild with\n\nload_all()\nrmarkdown::render(\"README.Rmd\")\n\nwhich builds the .html that can be viewed locally (but isn't pushed to GitHub;\nGitHub uses README.md to make the page you see on GitHub). See pacea if want to\nsave figures.\n--\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"60%\"\n)\nlibrary(tibble)\n```\n\n# recruitea\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/pbs-assess/recruitea/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/pbs-assess/recruitea/actions/workflows/R-CMD-check.yaml)\n[![Codecov test coverage](https://codecov.io/gh/pbs-assess/recruitea/branch/main/graph/badge.svg)](https://app.codecov.io/gh/pbs-assess/recruitea?branch=main)\n\u003c!-- badges: end --\u003e\n\n\u003c!-- ![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgithub.com%2Fpbs-assess%2Frecruitea\u0026label=VISITORS\u0026countColor=%23263759\u0026style=flat\u0026labelStyle=lower) --\u003e\n\nAn R package to investigate the impacts of ecosystem variability on recruitment using full MCMC results\n\nThis packages functionalises, generalises, and extends our methods developed by\nHaigh et al. (2019, Appendix F), to quantitatively detect environmental\ninfluences on annual recruitment of a stock , using a suite of indicators\navaiable in our [pacea](https://github.com/pbs-assess/pacea) R package.\n\nCrucially, the methods use the full uncertainties of recruitment (and mortality\nif desired) as estimated by an assessment model, not just average estimates for\neach year. Technically, this is done by using all Markov chain Monte Carlo\n(MCMC) samples, rather than just summary statistics. This aspect of the method was particularly well received at a national DFO workshop (Edwards et al. 2017).\n\nThe package is currently under construction and not intended to be operational\nyet.\n\nAdapting code from what I have locally (from previous laptop) in `POP_QCS_ClimateFB`, the non-FB\nfolder is older and has less in it, so just using this one. FB stands for\nFully Bayesian (iirc).\n\n## Outline of older code and plan for converting\n\nMy `POP_QCS_ClimateFB` fork is 34 ahead of Jean-Baptiste's; only branch is\ncalled Uncertainty. README describes workflow, so need to copy useful files\nover and functionalise them; copying that here to then edit and keep track of\nwhat we need to move over and rewrite.\n\n\nNext steps:\n D save previous POP mcmc values as a data object, so can use to repeat\n - use npgo from pacea\n D work through steps from **Main_Descriptive_Model.R** - a lot is book-keeping\n   and hardwired values. Think don't get too hung up on reusing the old code,\n   maybe simpler to mostly start again. Done below.\n\nD decide on JAGS or Stan. Probably Stan. Or RTMB [no, as want the MCMC, not the\n   approximation]? Or TMBStan?.\n\n- Catarina and Carrie: go with RTMB and then TMBStan to get\n   the samples.\n\n-  Catarina: Stan will be hard to include in a package, hence need\n   TMBStan.\n\n-  Philina: could even just use sdmTMB and take out the spatial bit. See\n   start of Sean's talks (Robyn mentioned that wasn't using RTMB, just\n   demonstrating the sparse matrix stuff).\n\n- find some template RTMB code and TMBStan, maybe:\n   https://openmse.com/tutorial-rcm/4-case-study-mcmc/\n\n- This looks promising, just rstan:\n   https://agabrioblog.onrender.com/tutorial/multiple-linear-regression-stan/multiple-linear-regression-stan/\n\n- Though did we have trouble including tmbstan in the salmon simulation work\n   package with Carrie? I have tmbstan installed okay. In EDMsimulate we used\n   cmdstanr, but it's not used in any of the R functions (it is imported\n   though), or in the report/ folder.\n\n----\n\n'work through steps' from above list:\n\nD go through **Main_DescriptiveModel.R** first, highlighting subfiles here and\n  what might be useful:\n  - `R/Fun/Boot.R`:\n    - `R/Class_Breaks.R` might be useful, for breaking variable up into classes,\n      gives `fun_Class_Breaks()`\n\t- `Bayesian/create_model_script.R` complex code for writing model code (not\n      clear which language), using function `create_model_script()`.\n    - `Bayesian/plot_posteriors_HDI.R` and\n      `Bayesian/plot_posteriors_HDI_exports.R` might be useful to adapt for plotting.\n    - `ggs_data_frame` is a properly documented function `ggs()`, but turns out\n      it's taken from the `ggmcmc` package.\n\t- `Bayesian/HighDensityIntervals_mcmc.R` makes function `HDIofMCMC()` but\n      code documented clearly (though help info is populated, though\n      incorrectly); can't see how the code calcs the HDI though.\n    - `Fun/Bayesian/Decision/..` I think is for the later work on Decisions;\n      prob not needed.\n - `R/Load_Data/Load_Data.R` - hardwired coding of data including environmental\n - `R/DescriptiveModel/Compute_descriptivemodels.R` - code for the linear\n analysis, but still very cumbersome.\n- conclude from looking through the above that it will be easier to start fresh,\n using the clear write up in Haigh et al., and do RTMB and TMBStan as mentioned\n below.\n\nLots of hard-wired names everywhere. Let's just focus on doing the POP values\nwith NPGO, as those results looked okay, 'almost significant' (so may possibly change with updated data).\n\n----\n\n## Ideas\n\nFrom Rowan: Note that R in Awatea is for age-1 recruits, while R in SS3 is for\nage-0 recruits. The big Bocaccio recruitment spike shows up in 2017 for the 2016\ncohort. This cohort was reported by a number of agencies up and down the coast\n(see 2019 Bocaccio stock assessment). There was some speculation that the large\nrecruitment event was connected with 'The Blob' (2014-2016). See Appendix G of\nthe BOR stock assessment (screenshot in his email, and read Appendix).\n\n\n\n## Directories\n\n**Main_DescriptiveModel.R** is the master R file which controls the\nanalysis. AE: start with this.\n\n**Data/**   - datasets organize in sub-directories (i.e. Climate). AE: shouldn't\nneed these as want to link directly from pacea.\n\n**Doc/**  - Documentations of data and analysis, which are pdf files generated with knitr and latex:\n\n      1. Climate: Description of available climate variables for the QCS.\n      2. DescriptiveModels: Methods available to model the association between POP productivity and climate variables.\n      3. 3rd-InternationalSymposiun presentation\n      4. Paper_Conceptual_Mechanism submitted to Fisheries Oceanography\n      5. Other: bib files, post-doc adds, TSC 2015\n\n**figure/** - save figures in the right sub-directory (i.e. Climate).\n\n**R/** - All R codes organized in sub-directories:\n\n- **Fun/** R functions required to run the analysis   AE: will need some\n- **Climate/** R code to load and plot climate data   AE: again, just use pacea\n- **Load_Data/** R code to load and prepare data      AE: may need some\n- **DescriptiveModel/** R code to make inference and post-inference computations\n  AE: will need some\n\n\nHow to start\n----------------------------\nRequirements:\n\n- Put the file `Bmcmc-pop-5ABC.rda` in the following directory:\n/POP\\_QCS\\_Climate/Data/Recruitment    AE: make this a data object to rerun old\nanalyses if desired\n- JAGS need to be installed\n  [http://mcmc-jags.sourceforge.net/](http://mcmc-jags.sourceforge.net/)  AE:\n  might be tricky in a package; better to use Stan?\n\n**Main.R**\n\nMain.R is the main file, which is controlling all the other R files. It calls three files:\n\n\t1. Boot.R loads useful R functions and packages.     AE: will use some of\n\tbut in a package form\n\t2. Load_Data.R loads climatic and recruitment data.  AE: just pacea\n\t3. Compute_descriptivemodels.R performs inference and post-inference.  AE:\n\twill need\n\nA lot of options are available within the Main.R file, to active the option set\nit to `TRUE`:  AE: not sure about these; keep it simple at first\n\n-  Saving Tex files and figures:  `Tex \u003c- TRUE ; SaveFig \u003c- TRUE`\n-  Standardized covariates: `Cov_Std \u003c- TRUE`\n-  Split covariates into `nClass` classes:\n           `Class \u003c- TRUE ;\n       if(Class){\n      nClass \u003c- 3 ;\n      Cov_Std \u003c- FALSE\n    }`\n- Set the lag between the covariates and the recruitment. Default t-1: `Cov_lag \u003c- NULL`\n- Choose covariates type (i.e.  \"Haida\" \"LargeScale\" \"NPCurrent\" \"LocalScale\"): `Cov_Type \u003c- \"Haida\"`\n- Choose time period: `Years \u003c- c(1940:2010)`\n- Choose observational model: Normal with a link function `'Norm_Log'` OR Log-Normal `'LogN'` OR Poisson `'Poisson'`\n- Choose latent model: just covariates `''` OR covariates + trend analysis `'YearTd'` OR covariates + polynomial for a non-linear effect `'NonLi'` OR covariates + trend analysis + polynomial `'YearTdNonLi'`\n-  Parallel computing: `Paralell \u003c- TRUE`\n-  Number of MCMC recruitment samples used in the inference process of `nMCMC=188`\n\n\n\nWorkflow\n----------------------------\n`Main.R` first calls the `Load_Data.R` which loads the recruitment and climatic data.\n`Load_Data.R` creates a data.frame `df_data_sample`, which contains `nMCMC` samples of recruitment values and all the available climatic and environmental covariates. A `df_data_sample` is saved on the first time that `Main.R` is launched with a name:  `df_data_sample_nMCMC.RData`, where `nMCMC` is an integer i.e. 150.\n\nTo select a covariate or a list of covariates, which will be included in the models, you can choose to assign a value at the object `Cov_Type` in the `Main.R` (i.e. \"Haida\", \"LargeScale\", \"LocalScale\", \"SpawningBiomass\", ...). Or you can edit the `make_Cov_name_list.R` file in the Load_Data sub-directory.\n\nTo run the inference, execute the `Compute_descriptivemodels.R` from the `Main.R` file.\n`Compute_descriptivemodels.R` consists of a loop, over the models set in `Main.R` and over the number of recruitment sample `nMCMC`. It performs `nMCMC` inferences of the same model with the R file `make_bayes_infer.R` located in R/DescriptiveModel/. Results are stored in the sub-directory R/DescriptiveModel/Estimates/ by model name (i.e.  R/DescriptiveModel/Estimates/Poisson\\_CovStd/) and by the covariates that are included in it (i.e.  R/DescriptiveModel/Estimates/Poisson\\_CovStd/Model\\_ALPI\\_km2\\_EPNPW\\_NPGO\\_indexW).\n\nThe post-inference computations are centralized in the R/DescriptiveModel/Post_Inference/ sub-directory and controlled with the file `PostInference_Processing.R`. It merges the  `nMCMC` inferences in one data.frame `df_mcmcChain_all`, and produces figures and tables of the posterior distributions. The tables are stored with the estimates, and the figures are saved in the directory figures/ following the same naming rule as the estimates (i.e.  figure/DescriptiveModel/Estimates/Poisson\\_CovStd/Model\\_ALPI\\_km2\\_EPNPW\\_NPGO\\_indexW).\n\n---\n\n\n\nEdwards, A.M., Haigh, R., Tallman, R., Swain, D.P., Carruthers, T.R., Cleary, J.S., Stenson, G. and Doniol-Valcroze, T. (2017). Proceedings of the Technical Expertise in Stock Assessment (TESA) National Workshop on ‘Incorporating an ecosystem approach into single-species stock assessments’ 21-25 November 2016, Nanaimo, British Columbia. Can. Tech. Rep. Fish. Aquat. Sci. 3213: vi + 53 p. https://waves-vagues.dfo-mpo.gc.ca/Library/40595754.pdf\n\nHaigh, R., Starr, P.J., Edwards, A.M., King, J.R., and Lecomte, J.-B. (2019). Stock assessment for Pacific Ocean Perch (Sebastes alutus) in Queen Charlotte Sound, British Columbia in 2017. DFO Can. Sci. Advis. Sec. Res. Doc. 2018/038. v + 227 p. http://www.dfo-mpo.gc.ca/csas-sccs/Publications/ResDocs-DocRech/2018/2018_038-eng.pdf\n\n\n## Installation\n\nTo install the latest version just:\n\n```\ninstall.packages(\"remotes\")    # If you do not already have the \"remotes\" package\n\nremotes::install_github(\"pbs-assess/recruitea\")\n```\n\nIf you get an error like\n```\nError in utils::download.file(....)\n```\nthen the connection may be timing out (happens to us on the DFO network). Try\n\n```\noptions(timeout = 1200)\n```\nand then try and install again. If you get a different error then post an Issue\nor contact \u003ca href=\"mailto:andrew.edwards@dfo-mpo.gc.ca\"\u003eAndy\u003c/a\u003e for help.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbs-assess%2Frecruitea","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpbs-assess%2Frecruitea","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbs-assess%2Frecruitea/lists"}