Code used in Parameterizations for Bayesian state-space models
Status: Accepted at Fisheries Research
Prerequisites
System
make
: Optional, allows for automating fitting and postprocessing- Rtools: Windows, required for
rstan
R packages
-
rstan
: Interface to Stan -
later
: Required for among-chain parallelization (rstan
issue 556) -
tidyverse
: Fit management, postprocessing management, and plotting -
gsl
: Provides Lambert$W$ function used in prior on Pella-Tomlinson shape parameter -
sn
: Provides skew normal functions -
ggridges
: Additional plot types forggplot2
-
gridExtra
: Combining and manipulating plots fromggplot2
-
flextable
: Output pretty tables to MS Word
Reproducing the model fits
This is easy if you have make
installed. Just use
make fits
in the top-level directory of this repository (typically tunabayes
, otherwise the directory with Makefile
.
If you don't have make
, you can manually source
each of the relevant R scripts. Be sure that the working directory is the top directory of this repository. From the R
prompt, this would be
source("src/31_fitfullPT.R")
source("src/32_fitfixedPT.R")
source("src/33_fitSchaefer.R")
Either way, the result will be three (large) files in the results
directory
fullPT_fits.Rds
fixedPT_fits.Rds
Schaefer_fits.Rds
Postprocessing the fits
The make
approach
make results
Note that if the three *_fits.Rds
results are not available, this will also run the fits.
Otherwise, from the same working directory in R
source("src/41_fullPT_summaries.R")
source("src/43_fixedPT_summaries.R")
source("src/45_Schaefer_summaries.R")
Note that each of these scripts require loading the *_fits.Rds
files into memory. Without a large amount of memory, it will probably be easiest to restart R between running these. The make
version already does this.
Either of these will add six new files to the results
directory
fullPT_summaries.Rds
fullPT_diagnostics.Rds
fixedPT_summaries.Rds
fixedPT_diagnostics.Rds
Schaefer_summaries.Rds
Schaefer_diagnostics.Rds
Figures
There is no make
option for the figures (yet), but they are fairly straightforward and quick to produce. In R from the same working directory,
source("src/51_fig1_data.R")
source("src/52_fig2_diagplots.R")
source("src/53_fig3_effplots.R")
source("src/54_fig4_biopost.R")
source("src/55_fig5_mgtpost.R")
All five figures will be in the figs
directory, as both TIFF and PDF types. The TIFF files are inserted into the Word manuscript, and the PDFs are high quality vector images for the published version. Figure 2 also outputs an SVG version. This is lightly edited in Inkscape for the publication version. The axes for the two constrained P parameterization that were not fit are moved to the nearest facets in each direction.
Supplementary Appendices
Supplementary Appendix A is a Word file with a description and table.
Supplementary Appendix B summarizes the priors for the Pella-Tomlinson shape parameter, the coefficient of variation of the catch observations, and the prior on each instantaneous fishing mortality rate. This can be generated by running
rmarkdown::render("notes/Manuscript/Appendix_B_Priors.Rmd")
Supplementary Appendix C plots priors for parameters and derived quantities not presented in the manuscript. Note that this loads the full fits and may be slow and/or use large amounts of memory during rendering.
rmarkdown::render("notes/Manuscript/Appendix_C_posteriors.Rmd")
Again, these should be run with the top level directory in this repository as the working directory. This is not the default when knitting documents in RStudio.