Uses a match/replace-style table like in modify_table(), but allows user to specify
scaling factors for individual columns rather than absolute values, and returns
the corresponding match/replace df to be used in modify_table(). This is intended to support
sensitivity analyses structured as "carry out 100 runs, with stock recruit scalers for stock X
running from 5% to 500% of the current value" (calc_fram_scaling() is only one part
of the pipeline for this). See modify_table() for details of setting up a match/replace dataframe;
the only difference here is that the columns to be scaled should start with "scale_" instead
of "replace_", and should contain the scalers.
Arguments
- fram_db
FRAM database
- table_name
name of FRAM table
- df
As the match/replace dataframe of
modify_table, but with "scale_" columns instead of "replace_" columns. Columns must start with either "match_" or "scale_", and should otherwise match the names of columns intable. Columns starting with "scale_" define the scaling factor to be applied to values in that column (for rows matched with the "match_" columns). For example, scaling the StartCohort values to 50% in the Cohort table might be achieved with columns "match_RunID", "match_StockID", "match_age", "match_TimeStep", "scale_StartCohort", with values of 0.5 in scale_Startcohort.
Value
A match/replace df for use in modify_table(), with "replace_" values
generated by scaling the corresponding values in the FRAM database. Includes additional "match_"
columns for all columns except "PrimaryKey"
Details
Note: In the StockRecruit table, RecruitScaleFactor and RecruitCohortSize should have a fixed relationship (scale factor = cohort size / base period size). For this reason, if applying scaling to only one of those columns, calc_fram_scaling will automatically apply the same scaling to the other. If the scaling for both is provided and they do not match, calc_fram_scaling will error out.
Examples
if (FALSE) { # \dontrun{
## in run 31, decrease stock 1's recruit numbers by 50% and double 2's recruit numbers
library(here)
fram_db <- connect_fram_db(here("example_fram_db.mdb"))
df <- data.frame(match_RunID = c(31, 31), match_StockID = 1:2, scale_RecruitScaleFactor = c(.5, 2))
df_scaled <- calc_fram_scaling(fram_db, "StockRecruit", df)
## here's what the values become:
df_scaled
## we can then modify the database
modified <- modify_table(fram_db, "StockRecruit", df_scaled)
disconnect_fram_db(fram_db)
} # }