Title: | FRAM Database Interface |
---|---|
Description: | A convenient tool for interfacing with FRAM access databases in R environments. |
Authors: | Ty Garber [cre, aut], Collin Edwards [aut] |
Maintainer: | Ty Garber <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.0 |
Built: | 2025-01-02 20:23:17 UTC |
Source: | https://github.com/FRAMverse/framrsquared |
Adds a column with a text version of flags for either non-retention or fishery scalers
add_flag_text(.data)
add_flag_text(.data)
.data |
fetched FisheryScalers or NonRetentions |
## Not run: mortality_table |> add_flag_text()
## Not run: mortality_table |> add_flag_text()
Either provides the step by step process of adding new stock to a FRAM database, or walks through fram database run and checks the tables for potential errors associated with adding new stock.
addstock_check( file_name = NULL, run_id = NULL, old_stockcount = 78, override_db_checks = FALSE )
addstock_check( file_name = NULL, run_id = NULL, old_stockcount = 78, override_db_checks = FALSE )
file_name |
filepath to database. If |
run_id |
RunID associated with the new stock in the FRAM database. IF left |
old_stockcount |
The number of stocks previously present to treat as the "baseline" – several checking steps will focus solely on newly added stocks. Defaults to 78. |
override_db_checks |
Ignore species, database type. When |
## Not run: ## review process addstock_check() ## check database for additional stock addstock_check("2024 Pre-Season Chinook DB - first test.mdb", run_id = 138) ## End(Not run)
## Not run: ## review process addstock_check() ## check database for additional stock addstock_check("2024 Pre-Season Chinook DB - first test.mdb", run_id = 138) ## End(Not run)
Extract AEQ mortality from Chinook FRAM database. Refactored and stripped down from the framr package written by Dan Auerbach. https://github.com/FRAMverse/framr/
aeq_mortality(fram_db, run_id = NULL, msp = TRUE)
aeq_mortality(fram_db, run_id = NULL, msp = TRUE)
fram_db |
Fram database object |
run_id |
numeric, RunID(s) as ID or ID:ID |
msp |
Do we use MSP expansion? Logical, default true. |
## Not run: fram_db |> aeq_mortality(run_id = 132) ## End(Not run)
## Not run: fram_db |> aeq_mortality(run_id = 132) ## End(Not run)
Performs error checks of a backwards FRAM run Returns nested tibble with diagnostics
bkfram_checks_coho(fram_db, backward_run_id = NULL, forward_run_id = NULL)
bkfram_checks_coho(fram_db, backward_run_id = NULL, forward_run_id = NULL)
fram_db |
fram database object, supplied through connect_fram_db |
backward_run_id |
numeric, RunID |
forward_run_id |
numeric, RunID |
## Not run: fram_db |> bkfram_checks_coho(backward_run_id = 132, forward_run_id = 133) ## End(Not run)
## Not run: fram_db |> bkfram_checks_coho(backward_run_id = 132, forward_run_id = 133) ## End(Not run)
Changes a run's ID number in a FRAM database
change_run_id(fram_db, old_run_id, new_run_id)
change_run_id(fram_db, old_run_id, new_run_id)
fram_db |
FRAM database object |
old_run_id |
FRAM run ID to be changed |
new_run_id |
New FRAM run ID |
## Not run: fram_db |> change_run_id(old_run_id = 132, new_run_id = 300)
## Not run: fram_db |> change_run_id(old_run_id = 132, new_run_id = 300)
Returns a tibble displaying predicted FRAMencounter mark rates by fishery, fishery type, and time-step.
coho_mark_rates(fram_db, run_id = NULL)
coho_mark_rates(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID (optional) |
## Not run: fram_db |> coho_mark_rates(run_id) ## End(Not run)
## Not run: fram_db |> coho_mark_rates(run_id) ## End(Not run)
Function supports QAQC practices by comparing the tables of two FRAM databases and identifying (and quantifying) differences.
compare_databases( file1, file2, runid_use = NULL, tables_use = NULL, slim = FALSE, quiet = TRUE )
compare_databases( file1, file2, runid_use = NULL, tables_use = NULL, slim = FALSE, quiet = TRUE )
file1 |
Character string. FRAM database that contains the results from running baseline FRAM runs (e.g., our "production" version). |
file2 |
Character string. FRAM database that contains the results from running modified FRAM runs (e.g., running a forked version of FRAM or using modified input values) |
runid_use |
Numeric vector of the run_ids to compare. Optional. If not provided, compare all run ids in the databases. |
tables_use |
Vector of character strings. Optional. If provided, only compare the listed tables. |
slim |
Logical. Optional, defaults to FALSE. If TRUE, do not include |
quiet |
Logical, defaults to TRUE. When TRUE, suppress messages showing individual steps. |
The key output is the $ratios
tibble, which contains every comparison of relevant
table entries in long-form. These comparisons are implemented by first aligning
corresponding table rows using appropriate key columns (e.g. run_id, fishery_id,
stock_id, age, time_step, etc).
In $ratios
, the table
and variable
columns
specify the table column being compared, respectively. prop.err
, abs.err
,
and scale.err
provide measures of the changes between the "original" value
(from file1
) and the "comparison" value (from file2
). More on those below.
The original
and comparison
columns give the actual values being compared.
run_id
through time_step
specify the rows being compared. bkfram.off.by.fish
and bkfram.off.by.prop
provide the context for the comparison (more on that below).
Quantifying error
Because FRAM involves numerical solvers, we expect some small differences in table entries
even when comparing two effectively equivalent databases. compare_databases()
provides three metrics for these changes.
In each case, it is assumed that file1
is the reference file; the "error" measures all show how much the value
in file2
changed relative to the corresponding value in file1
.
The simplest measure of error is the abs.err
. This is the absolute value of the difference between
the two values. If we're looking at an entry with table = "Mortality" and variable = "landed_catch",
then an abs.err of 5 means that the file2
entry was five fish more or less than the file1
entry. You can confirm this
by looking at the original
and comparison
columns. While abs.err
is the most easily interpreted,
it is often not very meaningful when looking across tables and variables. After all, an abs.err
value of 5 could mean a
a relatively meaningless change of five fish for a landed catch entry that was originally thousands of fish,
but the same value of 5 would be a huge change in fishing effort if it were for a fishery scaler entry.
One way to make error comparable across tables and variables is to calculate the proportional error.
If an entry changed by 0.01%, that's not meaningful, while if it changed by 10%, that is. $prop.err
provides
this proportional error, where -0.5 means the entry in file2 was 50% less than the corresponding value in file1,
and a value of 2 means the entry in file2 was 200% more than the corresponding value in file2. '
This gives error in context of the original value, and is often a good a way to look for problems. However,
we sometimes find very large $prop.err
values for changes that aren't concerning. For example,
we may have an entry for landed catch in the mortality table that was 0.00001 fish in file1,
and 0 fish in file2. In all practicality these two values are identical, and the
0.00001 fish difference is likely one of random jitter in the numerical solver or rounding differences.
However, our $prop.err
value for this cell is -1
, the most extreme negative change we can get.
We can jointly look at $abs.err
and $prop.err
to address the potential for
misleadingly large errors $prop.err
, but it would be nice to have a single error metric that
provides error in context without being sensitive to very small entries in file1.
scale.err
is an elaboration on $prop.err
that provides broader context. $prop.err
takes the absolute error and scales by the original value in file1. $scale.err
generalizes this idea,
first calculating the average error for each table-variable combination, and then scaling the
absolute error by the corresponding table-variable average. That is, if an entry for landed_catch in the
Mortality table was 0.001 in file1, and was then 0.002 in file2, and the average of all landed_catch
entries in file 1 was 1000, then the prop.err
would be 1
(since file2 had double the value of file1, or (0.002-0.001)/0.001
),
and the scale.err
would be 0.000001
((0.002-0.001)/1000
). This better captures our intuition
that a difference of 0.001 fish in the landed catch isn't a big deal, since those values are typically huge.
scale.err
is thus a measure of error that is comparable across variables and tables, essentially answer the question
"Has this entry changed a lot for this kind of variable and table?".
While scale.err
is frequently the most useful error metric, compare_databases()
provides all three.
There may be contexts in which it's important to focus on the proportional error. For example,
large proportional errors landed catch for the catch rare stocks can be important, but
the much larger catch from other stock could water down the scale_err
metric.
Addressing the backwardsFRAM wiggle
For post-season runs, the backwards FRAM algorithm is employed; its solver stops when
the estimated fish numbers are within 1 fish of the target size. This means that there is
the potential for substantial "wiggle" in bkFRAm values when comparing two databases.
This wiggle can propagate to other tables, especially for stock-age-timesteps in which
the target values were quite small (so a wiggle of +/- 1 fish would be a proportionally large
amount). For this reason, it can be useful to see how our measures of error correspond to the errors in the
corresponding bk fram table. For every table entry for which this makes sense (e.g., has a stock id, age, and timestep),
$bkfram.off.by.fish
gives the absolute error in the corresponding row of the BackwardsFram table, and
bkfram.off.by.prop
give the relative error (as a proportion)
in the corresponding row of the BackwardsFram table. If
this bkfram wiggle were the cause of observed errors, we would expect the
largest errors to correspond to the largest
$bkfram.off.by.fish
or $bkfram.off.by.prop
values.
Suggestions
For simple plotting to see if the original and comparison values fall on the 1:1
line, ggplot2::geom_point()
can be used, with $ratios$original
and $ratios$comparison
for x and y, and a facet_wrap by table
(and perhaps variable
) to
make plots readable. For identifying meaningful change, scale.err is likely the best measure of error.
It can be helpful to plot scale.err against bkfram.off.by.fish or bkfram.off.by.prop to
see if the table entries with the largest error correspond to the stock-fishery-age-timestep in which
there's the largest wiggle in the backwards fram solutions.
When digging into individual tables, it can sometimes be helpful to look at the comparisons in $ratios_detailed
,
which contains additional columns which did not fit into the standardized formatting of $ratios
.
List of lists and tibbles containing comparison information:
$ratios
tibble comparing every entry of every relevant column of every table. See "Details" for column descriptions.
$ratios_detailed
list of tibbles showing the contents of $ratios
broken into tables, with additional non-compared columns present (e.g., stock_name
in $ratios_detailed$Stock
)
$nrow_tracker
dataframe providing the number of rows in each table of file1 ($nrow.prod
), file2 ($nrow.fork
), and the joined comparison ($nrow.comp
). Useful to track down cause of many-to-many join warnings that can result from duplicated table entries; unless there are duplicate entries, $nrow.comp
should be less than or equal to the minimum of $nrow.prod
and $nrow.fork
.
$tabs_file1
List containing the original fetched tables from file1
. Not returned if argument slim
is TRUE
$tabs_file2
List containing the original fetched tables from file2
. Not returned if argument slim
is TRUE.
## Not run: file1 = "Valid2022_Round_7_1_1_11142023_REFERENCE_fixed - fork rebuild.mdb" file2 = "Valid2022_Round_7.1.1_11142023 - green river split.mdb" out = tables_compare(file1, file2) ## End(Not run)
## Not run: file1 = "Valid2022_Round_7_1_1_11142023_REFERENCE_fixed - fork rebuild.mdb" file2 = "Valid2022_Round_7.1.1_11142023 - green river split.mdb" out = tables_compare(file1, file2) ## End(Not run)
Compares the fishery flags of two runs
compare_fishery_input_flags(fram_db, run_ids, verbose = TRUE)
compare_fishery_input_flags(fram_db, run_ids, verbose = TRUE)
fram_db |
FRAM database object |
run_ids |
Two run ids |
verbose |
If |
## Not run: fram_db |> compare_fishery_input_flags(c(55, 56))
## Not run: fram_db |> compare_fishery_input_flags(c(55, 56))
Compares the fishery inputs of two runs
compare_fishery_inputs(fram_db, run_ids, tolerance = 0.01, verbose = TRUE)
compare_fishery_inputs(fram_db, run_ids, tolerance = 0.01, verbose = TRUE)
fram_db |
FRAM database object |
run_ids |
Two run ids |
tolerance |
Tolerance for detecting changes |
verbose |
If |
## Not run: fram_db |> compare_fishery_inputs(c(55, 56))
## Not run: fram_db |> compare_fishery_inputs(c(55, 56))
Generates a dataframe that compares fishery scalers table for two runs identified by run_id's
compare_inputs(fram_db, run_ids)
compare_inputs(fram_db, run_ids)
fram_db |
FRAM database object |
run_ids |
Vector of two run_ids |
Comparisons assume the first run provided is the baseline, and provide relative changes from that. This includes percent changes ($percent.diff)include percent changes (infinite when)
Data frame of differences. $percentdiff
= change in quota (comparing the appropriate quotas based on fishery flags),
$regulation_comparison
= change in regulation (NS, MSF, NS + MSF). Columns present in the
FisheriesScalers database are included, with _original
and _comparison
suffixes identifying entries associated with the first and second entries of
run_ids
, respectively.
## Not run: fram_db |> compare_inputs(c(100,101))
## Not run: fram_db |> compare_inputs(c(100,101))
Generates a heat map of changed between two run inputs. Can be a very busy chart if not filtered down. Consider using a filter.
compare_inputs_chart(.data)
compare_inputs_chart(.data)
.data |
Dataframe origination from the compare_inputs() function |
## Not run: fram_db |> compare_inputs(c(100, 101)) |> compare_inputs_chart()
## Not run: fram_db |> compare_inputs(c(100, 101)) |> compare_inputs_chart()
Compares the non retention flags of two runs
compare_non_retention_input_flags(fram_db, run_ids, verbose = TRUE)
compare_non_retention_input_flags(fram_db, run_ids, verbose = TRUE)
fram_db |
FRAM database object |
run_ids |
Two run ids |
verbose |
If |
## Not run: fram_db |> compare_non_retention_inputs(c(55, 56))
## Not run: fram_db |> compare_non_retention_inputs(c(55, 56))
Compares the non retention inputs of two runs
compare_non_retention_inputs(fram_db, run_ids, verbose = TRUE)
compare_non_retention_inputs(fram_db, run_ids, verbose = TRUE)
fram_db |
FRAM database object |
run_ids |
Two run ids |
verbose |
If |
## Not run: fram_db |> compare_non_retention_inputs(c(55, 56))
## Not run: fram_db |> compare_non_retention_inputs(c(55, 56))
Compares the recruit scalers of two runs
compare_recruits(fram_db, run_ids, tolerance = 0.01, verbose = TRUE)
compare_recruits(fram_db, run_ids, tolerance = 0.01, verbose = TRUE)
fram_db |
FRAM database object |
run_ids |
Two run ids |
tolerance |
Tolerance for detecting changes |
verbose |
If |
## Not run: fram_db |> compare_recruits()
## Not run: fram_db |> compare_recruits()
Generates a report to the console of changes to inputs between two runs
compare_runs(fram_db, run_ids, tolerance = 0.01)
compare_runs(fram_db, run_ids, tolerance = 0.01)
fram_db |
FRAM database object |
run_ids |
Two run ids |
tolerance |
Tolerance of detection, 1 percent default |
## Not run: fram_db |> compare_runs(c(55, 56))
## Not run: fram_db |> compare_runs(c(55, 56))
Compares the stock fishery rate scalers of two runs
compare_stock_fishery_rate_scalers(fram_db, run_ids)
compare_stock_fishery_rate_scalers(fram_db, run_ids)
fram_db |
FRAM database object |
run_ids |
Two run ids |
## Not run: fram_db |> compare_stock_fishery_rate_scalers(c(55, 56))
## Not run: fram_db |> compare_stock_fishery_rate_scalers(c(55, 56))
This is a connection object to a FRAM database. Returns a list used throughout the rest of this package which carries meta data.
connect_fram_db( db_path, enforce_type = c("full", "transfer"), read_only = FALSE, quiet = FALSE )
connect_fram_db( db_path, enforce_type = c("full", "transfer"), read_only = FALSE, quiet = FALSE )
db_path |
Path to a FRAM database. |
enforce_type |
Not used |
read_only |
Optional argument to flag this connection as read-only (if set to |
quiet |
Logical. Optitional argument; when TRUE, silences success message and database summary. |
Additional details...
## Not run: fram_db <- connect_fram_db('<path>')
## Not run: fram_db <- connect_fram_db('<path>')
Experimental copying scaler inputs from one run to another DANGEROUS
copy_fishery_scalers(fram_db, from_run, to_run, fishery_id = NULL)
copy_fishery_scalers(fram_db, from_run, to_run, fishery_id = NULL)
fram_db |
FRAM database object |
from_run |
Run ID to be copied from |
to_run |
Run ID to be copied to |
fishery_id |
ID or IDs for specific fishery(s) to copy inputs to/from. If not provided, interactive option to copy inputs for all fisheries. |
## Not run: framdb |> copy_fishery_scalers(132, 133, 87)
## Not run: framdb |> copy_fishery_scalers(132, 133, 87)
Safely disconnect from FRAM database
disconnect_fram_db(fram_db, quiet = TRUE)
disconnect_fram_db(fram_db, quiet = TRUE)
fram_db |
FRAM database R object |
quiet |
Logical. Optional; when true, silences success message. |
## Not run: disconnect_fram_db(fram_db)
## Not run: disconnect_fram_db(fram_db)
Tool to streamline development. Currently this checks for use of filter() without dplyr::. This would call stats::filter(), which is usually not what I intend. As possible, add additional checks for issues that cause problems but do not give an informative error message (or any error message).
error_check_code(filepath, n = Inf)
error_check_code(filepath, n = Inf)
filepath |
Path to R file to be checked |
n |
Number of rows to print. Default is to print all rows, but set to smaller values if output is overwhelming. |
## Not run: error_check_code("R/copy.R") ## End(Not run)
## Not run: error_check_code("R/copy.R") ## End(Not run)
Creates template files from specified organization in the specified path. Generally
initialize_project()
will be more useful for new R projects, while fetch_quarto_templates()
can be helpful when working with existing projects. See initialize_project()
for details on adding template files for new organizations.
fetch_quarto_templates( to.path, organization = "WDFW", color = "coffee", overwrite = FALSE )
fetch_quarto_templates( to.path, organization = "WDFW", color = "coffee", overwrite = FALSE )
to.path |
Character string. Destination file path for template files. Typically, root of Rproject directory. |
organization |
Character, defaults to "WDFW". Specifies the set of quarto templates to use. Currently only supports "WDFW". |
color |
Character string, defaults to "coffee". Specifies quarto template to use; organizations may have several. |
overwrite |
Boolean. Overwrite _quarto.yml and style.css files if they already exist? Defaults to FALSE. |
Nothing.
Fetches a complete table from a FRAM database. Returns a cleaned tibble.
fetch_table(fram_db, table_name = NULL, warn = TRUE)
fetch_table(fram_db, table_name = NULL, warn = TRUE)
fram_db |
FRAM database object |
table_name |
Table to be fetched. If not given, a list of options will be printed |
warn |
Print a warning when fetching BackwardsFRAM table from a Chinook database? Defaults to |
## Not run: fram_db |> fetch_table('Mortality')
## Not run: fram_db |> fetch_table('Mortality')
The BackwardsFRAM table uses a stock_id different numbering system from all other tables, and includes sums of joint stocks (e.g. for a marked and unmarked pair of stocks, BackwardsFRAM will typically have an additional stock which represents the sum of those two). Because the numbering is different but the column name is the same, joining the Chinook BackwardsFRAM table with other tables can cause problems.
fetch_table_bkchin(fram_db)
fetch_table_bkchin(fram_db)
fram_db |
FRAM database object |
This function augments fetch_table by renaming the stock_id
column to bk_stock_id
, and
then adding on the associated stock_id (with NAs when the bkfram stock is one of these new "sum" stocks
and the associated bkfram stock names). This function only works for Chinook databases.
The resulting dataframe will necessarily NOT be an exact match to the BackwardsFRAM table in the FRAM database. The stock_id column will differ (containing normal stock ID values instead of bk stock ID values), and there will be two additional columns.
#' @examples ## Not run: ##Potentially problematic stock_id won't align with other tables fram_db |> fetch_table('BackwardsFRAM') ## "safe" version of the table; stock_id WILL align with other tables fram_db |> fetch_table_bkchin() ## End(Not run)
#' @examples ## Not run: ##Potentially problematic stock_id won't align with other tables fram_db |> fetch_table('BackwardsFRAM') ## "safe" version of the table; stock_id WILL align with other tables fram_db |> fetch_table_bkchin() ## End(Not run)
fishery_id
column name.Filters a dataframe to Alaska fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_ak(.data, species = NULL)
filter_ak(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_ak() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_ak(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_ak(species = "COHO")
## Not run: fram_dataframe |> filter_ak() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_ak(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_ak(species = "COHO")
fishery_id
column name.Filters a dataframe to Canadian (BC) fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_bc(.data, species = NULL)
filter_bc(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_bc() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_bc(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_bc(species = "COHO")
## Not run: fram_dataframe |> filter_bc() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_bc(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_bc(species = "COHO")
fishery_id
column name.Filters a dataframe to California fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_ca(.data, species = NULL)
filter_ca(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_ca() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_ca(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_ca(species = "COHO")
## Not run: fram_dataframe |> filter_ca() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_ca(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_ca(species = "COHO")
fishery_id
column name.Filters a dataframe to Coastal fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_coast(.data, species = NULL)
filter_coast(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_coast() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_coast(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_coast(species = "COHO")
## Not run: fram_dataframe |> filter_coast() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_coast(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_coast(species = "COHO")
NA's all the information in the FisheryScalers that's not being used e.g Flag 1 only NS Scalers will be returned
filter_flag(.data)
filter_flag(.data)
.data |
Fishery Scalers table |
## Not run: fishery_scalers_table |> filter_flag()
## Not run: fishery_scalers_table |> filter_flag()
fishery_id
column name.Filters a dataframe to marine fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_marine(.data, species = NULL)
filter_marine(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_marine() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_marine(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_marine(species = "COHO")
## Not run: fram_dataframe |> filter_marine() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_marine(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_marine(species = "COHO")
fishery_id
column name.Filters a dataframe to net fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_net(.data, species = NULL)
filter_net(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_net() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_net(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_net(species = "COHO")
## Not run: fram_dataframe |> filter_net() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_net(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_net(species = "COHO")
fishery_id
column name.Filters a dataframe to Oregon fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_or(.data, species = NULL)
filter_or(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_or() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_or(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_or(species = "COHO")
## Not run: fram_dataframe |> filter_or() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_or(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_or(species = "COHO")
fishery_id
column name.Filters a dataframe to Puget Sound fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_puget_sound(.data, species = NULL)
filter_puget_sound(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_puget_sound() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_puget_sound(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_puget_sound(species = "COHO")
## Not run: fram_dataframe |> filter_puget_sound() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_puget_sound(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_puget_sound(species = "COHO")
fishery_id
column name.Filters a dataframe to sport fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_sport(.data, species = NULL)
filter_sport(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_sport() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_sport(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_sport(species = "COHO")
## Not run: fram_dataframe |> filter_sport() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_sport(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_sport(species = "COHO")
fishery_id
column name.Filters a dataframe to Washington State fisheries. Will
automatically detect whether it's working with a Chinook or Coho
dataset if the tables were generated within this package. Requires
a fishery_id
column name.
filter_wa(.data, species = NULL)
filter_wa(.data, species = NULL)
.data |
Dataframe containing |
species |
Optional argument to identify species if |
## Not run: fram_dataframe |> filter_wa() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_wa(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_wa(species = "COHO")
## Not run: fram_dataframe |> filter_wa() ## End(Not run) framrosetta::fishery_chinook_fram |> filter_wa(species = "CHINOOK") framrosetta::fishery_coho_fram |> filter_wa(species = "COHO")
Finds tables that contain a specific column name
find_tables_by_column_(fram_db, column_name)
find_tables_by_column_(fram_db, column_name)
fram_db |
FRAM database object |
column_name |
Name of a column |
## Not run: fram_db |> find_tables_by_column_('RunID')
## Not run: fram_db |> find_tables_by_column_('RunID')
Returns a tibble matching the Fishery Mortality screen.
fishery_mortality(fram_db, run_id = NULL, msp = TRUE)
fishery_mortality(fram_db, run_id = NULL, msp = TRUE)
fram_db |
FRAM database object |
run_id |
Run ID |
msp |
Model Stock Proportion, default TRUE |
## Not run: fram_db |> fishery_mortality(run_id = 101) ## End(Not run)
## Not run: fram_db |> fishery_mortality(run_id = 101) ## End(Not run)
Cleans the names of FRAM tables and coverts to a tibble
fram_clean_tables(.data)
fram_clean_tables(.data)
.data |
Dataframe |
## Not run: fram_dataframe |> fram_clean_tables()
## Not run: fram_dataframe |> fram_clean_tables()
Identifies the FRAM database species focus - Chinook or Coho
fram_database_species(con)
fram_database_species(con)
con |
Connection to FRAM database |
## Not run: fram_database_species(con)
## Not run: fram_database_species(con)
Identifies the FRAM database type - Full or Transfer
fram_database_type(con)
fram_database_type(con)
con |
Connection to FRAM database |
## Not run: fram_database_type(con)
## Not run: fram_database_type(con)
Gets all run_ids of FRAM database
get_run_ids(fram_db)
get_run_ids(fram_db)
fram_db |
Fram database object |
## Not run: fram_dataframe |> get_run_ids()
## Not run: fram_dataframe |> get_run_ids()
By default, creates suggested folder structure from best coding practices, and
adds WDFW-style yaml and style.css files to give quarto files consistent appearance. If you
belong to another organization and want this function to support your own organization-specific quarto styling,
reach out to the developers with a _quarto.yml
and (optionally) style.css
file.
initialize_project( folders = c("scripts", "original_data", "cleaned_data", "figures", "results", "results/quarto_output"), quarto = TRUE, organization = "WDFW", renv = FALSE, template.overwrite = TRUE, color = "coffee", quiet = TRUE )
initialize_project( folders = c("scripts", "original_data", "cleaned_data", "figures", "results", "results/quarto_output"), quarto = TRUE, organization = "WDFW", renv = FALSE, template.overwrite = TRUE, color = "coffee", quiet = TRUE )
folders |
Vector of folders to create |
quarto |
Boolean. If TRUE, add quarto yaml file and style.css |
organization |
Character, defaults to "WDFW". Specifies the set of quarto templates to use. Currently only supports "WDFW". |
renv |
Boolean to initialize renv. Defaults to FALSE. |
template.overwrite |
Boolean. Overwrite _quarto.yml and style.css files if they already exist? Defaults to TRUE |
color |
Character string, defaults to "coffee". Specifies quarto template to use; organizations may have several. |
quiet |
Boolean, defaults to FALSE. If TRUE, suppresses informational messages. |
Dev note: new template files for additional organizations should be added to inst/
in a subfolder matching an R-friendly organization name,
and the same name should be added to the organization
parameter description here and the supported_organizations
in fetch_quarto_templates()
.
## Not run: framrsquared::initialize_project() ## End(Not run)
## Not run: framrsquared::initialize_project() ## End(Not run)
Generates an input summary based on a FisheryScalers dataframe. Probably end up streamlining / revising this.
input_summary_(.data, run_id)
input_summary_(.data, run_id)
.data |
FisheryFishery scalers dataframe |
run_id |
Run ID number |
## Not run: fishery_scalers_dataframe |> input_summary()
## Not run: fishery_scalers_dataframe |> input_summary()
Identify how much reduction in landed catch at each fishery that would be needed to reduce the impacts on a focal stock by 1 fish.
make_impacts_per_catch_heatmap(fram_db, run_id, stock_id)
make_impacts_per_catch_heatmap(fram_db, run_id, stock_id)
fram_db |
fram database connection |
run_id |
run_id of interest |
stock_id |
stock_id of interest. |
ggplot object
## Not run: path = "FRAM compilations - readonly/2024-Pre-Season-Chinook-DB/2024 Pre-Season Chinook DB.mdb" run_id = 132 stock_id = 3 make_impacts_per_catch_heatmap(path, run_id = 132, stock_id = 5) ## End(Not run)
## Not run: path = "FRAM compilations - readonly/2024-Pre-Season-Chinook-DB/2024 Pre-Season Chinook DB.mdb" run_id = 132 stock_id = 3 make_impacts_per_catch_heatmap(path, run_id = 132, stock_id = 5) ## End(Not run)
Vectorized approach to calculating the management week, returns an integer
management_week(date)
management_week(date)
date |
A column with dates |
## Not run: data_fram |> mutate(mngmt_week = management_week(date_field)) ## End(Not run)
## Not run: data_fram |> mutate(mngmt_week = management_week(date_field)) ## End(Not run)
stock_mortality_ratio
produced by these functions.Guestimate the impact on a particular stock by multiplying its mortalities
by the stock_mortality_ratio
produced by these functions.
mortality_scalers(fram_db, run_id, stock_id)
mortality_scalers(fram_db, run_id, stock_id)
fram_db |
FRAM database object |
run_id |
Run ID |
stock_id |
A focal stock or stocks |
## Not run: fram_db |> mortality_scalers(run_id = 101, stock_id = c(17:18))
## Not run: fram_db |> mortality_scalers(run_id = 101, stock_id = c(17:18))
Produces the MSF screen report numbers for encounters. Returns different format depending database.
msf_encounters(fram_db, run_id = NULL)
msf_encounters(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID |
## Not run: fram_db |> msf_encounters(run_id = 101)
## Not run: fram_db |> msf_encounters(run_id = 101)
Returns a tibble matching the MSF screen report encounters for Chinook. This is specific for Chinook and in most cases msf_encounters() is preferable.
msf_encounters_chinook_(fram_db)
msf_encounters_chinook_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_encounters_chinook_(run_id = 101)
## Not run: fram_db |> msf_encounters_chinook_(run_id = 101)
Returns a tibble matching the MSF screen report encounters for Coho This is specific for Coho and in most cases msf_encounters() is preferable.
msf_encounters_coho_(fram_db)
msf_encounters_coho_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_encounters_coho_()
## Not run: fram_db |> msf_encounters_coho_()
Produces the MSF screen report numbers for landed catch. Returns different format depending database.
msf_landed_catch(fram_db, run_id = NULL)
msf_landed_catch(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID |
## Not run: fram_db |> msf_landed_catch(run_id = 101)
## Not run: fram_db |> msf_landed_catch(run_id = 101)
Returns a tibble matching the MSF screen report landed catch for Chinook. This is specific for Chinook and in most cases msf_landed_catch() is preferable.
msf_landed_catch_chinook_(fram_db)
msf_landed_catch_chinook_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_landed_catch_chinook_(run_id = 101)
## Not run: fram_db |> msf_landed_catch_chinook_(run_id = 101)
Returns a tibble matching the MSF screen report landed catch for Coho This is specific for Coho and in most cases msf_landed_catch() is preferable.
msf_landed_catch_coho_(fram_db)
msf_landed_catch_coho_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_landed_catch_coho_()
## Not run: fram_db |> msf_landed_catch_coho_()
Produces the MSF screen report numbers for mortalities. Returns different format depending database.
msf_mortalities(fram_db, run_id = NULL)
msf_mortalities(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID |
## Not run: fram_db |> msf_mortalities_coho_(run_id = 101)
## Not run: fram_db |> msf_mortalities_coho_(run_id = 101)
Returns a tibble matching the MSF screen report mortalities for Chinook. This is specific for Chinook and in most cases msf_mortalities() is preferable.
msf_mortalities_chinook_(fram_db)
msf_mortalities_chinook_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_mortalities_chinook_(run_id = 101)
## Not run: fram_db |> msf_mortalities_chinook_(run_id = 101)
Returns a tibble matching the MSF screen report mortalities for Coho This is specific for Coho and in most cases msf_mortalities() is preferable.
msf_mortalities_coho_(fram_db)
msf_mortalities_coho_(fram_db)
fram_db |
FRAM database object |
## Not run: fram_db |> msf_mortalities_coho_()
## Not run: fram_db |> msf_mortalities_coho_()
See https://framverse.github.io/fram_doc/calcs_data_chin.html#46_Model-Stock_Proportion.
msp_mortality(fram_db, run_id = NULL)
msp_mortality(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID (optional) |
Mortality table with mortality values expanded by msp
## Not run: fram_db |> msp_mortality(run_id = 132) ## End(Not run)
## Not run: fram_db |> msp_mortality(run_id = 132) ## End(Not run)
Provides English translation of numeric non-retention flags
NR_flag_translate(vec)
NR_flag_translate(vec)
vec |
vector of flags |
## Not run: NR_flag_translate(sample(1:4, 10, replace = T))
## Not run: NR_flag_translate(sample(1:4, 10, replace = T))
Creates an ordered bar chart with the top number of mortalities per fishery.
plot_stock_mortality( fram_db, run_id, stock_id, top_n = 10, filters_list = NULL, msp = TRUE )
plot_stock_mortality( fram_db, run_id, stock_id, top_n = 10, filters_list = NULL, msp = TRUE )
fram_db |
fram database object, supplied through connect_fram_db |
run_id |
numeric, RunID |
stock_id |
numeric, ID of focal stock |
top_n |
numeric, Number of fisheries to display |
filters_list |
list of framrsquared filter functions to apply before plotting. |
msp |
Use Model Stock Proportion expansion? Default is true |
## Not run: fram_db |> plot_stock_mortality(run_id = 132, stock_id = 17) fram_db |> plot_stock_mortality(run_id = 132, stock_id = 17, filters_list = list(filter_wa, filter_marine)) ## End(Not run)
## Not run: fram_db |> plot_stock_mortality(run_id = 132, stock_id = 17) fram_db |> plot_stock_mortality(run_id = 132, stock_id = 17, filters_list = list(filter_wa, filter_marine)) ## End(Not run)
Creates an ordered bar chart with the top number of mortalities per fishery and time step.
plot_stock_mortality_time_step( fram_db, run_id, stock_id, top_n = 10, filters_list = NULL, msp = TRUE )
plot_stock_mortality_time_step( fram_db, run_id, stock_id, top_n = 10, filters_list = NULL, msp = TRUE )
fram_db |
fram database object, supplied through connect_fram_db |
run_id |
numeric, RunID |
stock_id |
numeric, ID of focal stock |
top_n |
numeric, Number of fisheries to display |
filters_list |
list of framrsquared filter functions to apply before plotting. |
msp |
Use Model Stock Proportion expansion? Default is true |
## Not run: fram_db |> stock_mortality_time_step(run_id = 132, stock_id = 17) ## End(Not run)
## Not run: fram_db |> stock_mortality_time_step(run_id = 132, stock_id = 17) ## End(Not run)
Returns a tibble matching the Population Statistics screen.
population_statistics(fram_db, run_id = NULL)
population_statistics(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID |
## Not run: fram_db |> population_statistics(run_id = 101)
## Not run: fram_db |> population_statistics(run_id = 101)
Generates post-season January age 3 abundances by stock from post-season databases. Used for forecasting.
post_season_abundance(fram_db, units = c("ja3", "oa3"))
post_season_abundance(fram_db, units = c("ja3", "oa3"))
fram_db |
FRAM database object |
units |
Default January Age 3 'ja3', optional ocean age 3 'oa3' |
## Not run: framdb |> post_season_abundance()
## Not run: framdb |> post_season_abundance()
Provides list of FRAm database names, typically useful for internal functions.
provide_table_names(is_full = TRUE)
provide_table_names(is_full = TRUE)
is_full |
Logical. Provide names for a full FRAM database (TRUE) or a model transfer (FALSE)? |
Character string of the names of FRAM tables
provide_table_names(is_full = FALSE)
provide_table_names(is_full = FALSE)
Removes a run in a FRAM database
remove_run(fram_db, run_id)
remove_run(fram_db, run_id)
fram_db |
FRAM database object |
run_id |
FRAM run ID to be deleted |
## Not run: fram_db |> delete_run(run_id = 132)
## Not run: fram_db |> delete_run(run_id = 132)
Provides a print out of Run ID information
run_info(fram_db, run_id)
run_info(fram_db, run_id)
fram_db |
FRAM database object |
run_id |
FRAM run ID |
## Not run: fram_db |> run_info(run_id = 132)
## Not run: fram_db |> run_info(run_id = 132)
Provides English translation of numeric scalers flags
scalers_flag_translate(vec)
scalers_flag_translate(vec)
vec |
vector of flags |
## Not run: scaler_flags_translate(sample(c(1, 2, 7, 8, 17, 18, 27, 28), 10, replace = T))
## Not run: scaler_flags_translate(sample(c(1, 2, 7, 8, 17, 18, 27, 28), 10, replace = T))
Statistical weeks start on mondays, so the first statistical week of the year starts on the first monday of the year. (Contrast with management weeks which start on Sundays).
statistical_week(date)
statistical_week(date)
date |
A vector of dates |
## Not run: data_fram |> mutate(mngmt_week = statistical_week(date_field)) ## End(Not run)
## Not run: data_fram |> mutate(mngmt_week = statistical_week(date_field)) ## End(Not run)
#' Intended for internal use, makes some assumptions about inputs.
stock_age_checker(table_name, NumStk, old_stockcount, df, min_age, max_age)
stock_age_checker(table_name, NumStk, old_stockcount, df, min_age, max_age)
table_name |
Character of table name, for informative messages. |
NumStk |
Maximum number of stock, pulled from BaseID table |
old_stockcount |
Number of stock in previous FRAM baseperiod. Only looks for problems for StockID > this number. |
df |
Dataframe to check. Must have columns "stock_id" and "age" (which are the names for relevant columns of framrsquared::fetch_table). |
min_age |
Minimum age modeled. Should be the min_age from the baseid_df. |
max_age |
Maximum age modeled. Should be the max_age from the baseid_df. |
numeri; 0 if no warning, 1 if warning.
More thorough checking than stock_id_comp. Checks that the number of stock IDs makes sense given NumStk
,
that Stock IDs are sequential (in the sense that if NumStk = n, every integer up to n is represented).
Optionally, can check that each stock ID is unique.
stock_check_helper(table_name, NumStk, stock_vec, uniques_only = FALSE)
stock_check_helper(table_name, NumStk, stock_vec, uniques_only = FALSE)
table_name |
Character of table name, for informative messages. |
NumStk |
Maximum number of stock, pulled from BaseID table |
stock_vec |
vector of stock ids to check. Presumably column of fetched table. |
uniques_only |
Do we want warnings if there are duplicats of StockIDs? Useful for tables like Stock and Growth that should have only one entry per stock. Logical, default = |
Numeric, returning number of warnings detected.
Intended for internal use, makes some assumptions about inputs.
stock_id_comp(table_name, df, stock_ref)
stock_id_comp(table_name, df, stock_ref)
table_name |
Character of table name, for informative messages |
df |
Dataframe |
stock_ref |
numeric vector of all stock IDs. Should be stock_df$stock_id. |
numeric; 0 if no warning, 1 if warning.
Returns a tibble matching the Fishery Mortality screen.
stock_mortality(fram_db, run_id = NULL)
stock_mortality(fram_db, run_id = NULL)
fram_db |
FRAM database object |
run_id |
Run ID |
## Not run: fram_db |> stock_mortality(run_id=132) |> filter(stock_id == 17, fishery_id == 36) ## End(Not run)
## Not run: fram_db |> stock_mortality(run_id=132) |> filter(stock_id == 17, fishery_id == 36) ## End(Not run)
For easy readability, we want to use consistent coding style when developing code for framrsquared.
Presently this includes (a) using <-
for assignment, and (b) using snakecase for
variable and function names. The functions here streamlining checking R code for
consistency with this style.
frs_stylecheck_assignment(filepath, n = Inf) frs_stylecheck_snakecase(filepath, n = Inf)
frs_stylecheck_assignment(filepath, n = Inf) frs_stylecheck_snakecase(filepath, n = Inf)
filepath |
Path to R file to be checked |
n |
Number of rows to print. Default is to print all rows, but set to smaller values if output is overwhelming. |
frs_stylecheck_assignment()
takes the path to an R file, and prints (and returns)
any rows that may be mis-using the =
for assignment. Note that it will give false positives
for arguments defined in function calls if the call spans multiple lines, as well as
=
signs included in character strings.
frs_stylecheck_snakecase
takes the path to an R file
and prints (and returns) the names of any variables assigned using <-
that do not include underscores.
This will identify variables that do not use snakecase, but will also give false positive
matches for variables that are single words and thus do not need snakecase.
## Not run: frs_stylecheck_assignment("R/copy.R") frs_stylecheck_snakecase("R/copy.R") ## End(Not run)
## Not run: frs_stylecheck_assignment("R/copy.R") frs_stylecheck_snakecase("R/copy.R") ## End(Not run)
Returns a dataframe with fisheries defined by the TRuns report driver
truns_fisheries(fram_db)
truns_fisheries(fram_db)
fram_db |
FRAM database object |
## Not run: truns <- truns_fisheries(fram_db)
## Not run: truns <- truns_fisheries(fram_db)
Returns a dataframe with stocks defined by the TRuns report driver
truns_stocks(fram_db)
truns_stocks(fram_db)
fram_db |
FRAM database object |
## Not run: truns <- truns_stocks(fram_db)
## Not run: truns <- truns_stocks(fram_db)
Convenience function to check fram_db input
validate_fram_db( fram_db, db_type = NULL, db_species = NULL, call = rlang::caller_env() )
validate_fram_db( fram_db, db_type = NULL, db_species = NULL, call = rlang::caller_env() )
fram_db |
FRAM database object |
db_type |
Enforcement of a database type 'full' or 'transfer' |
db_species |
Enforcement of a species 'COHO' or 'CHINOOK' |
call |
internal use: identify name of function that called this function (for informative error message) |
Convenience function to check run_id input
validate_run_id(fram_db, run_id, call = rlang::caller_env())
validate_run_id(fram_db, run_id, call = rlang::caller_env())
fram_db |
FRAM database object |
run_id |
one or more run_ids |
call |
internal use: identify name of function that called this function (for informative error message) |
Welcome message, summarizing database information
welcome(con)
welcome(con)
con |
FRAM database connection |
## Not run: welcome(con)
## Not run: welcome(con)