st <- list(
imm = g3_stock(c("fish", maturity = "imm"), c(10, 20, 30)),
mat = g3_stock(c("fish", maturity = "mat"), c(10, 20, 30)) )
st2 <- g3_stock("other", c(10, 20, 30))
# Set up a projected parameter to share over both stocks
st_Mdn <- g3_param_project(
"Mdn",
g3_param_project_dnorm(),
# Append common part of stock names to parameter name
by_stock = st )
actions <- list(
g3a_time(1990, 1994, c(6,6)),
gadget3:::g3a_initialconditions_manual(st$imm,
quote( 100 + stock__minlen ),
quote( 1e4 + 0 * stock__minlen ) ),
gadget3:::g3a_initialconditions_manual(st$mat,
quote( 100 + stock__minlen ),
quote( 1e4 + 0 * stock__minlen ) ),
gadget3:::g3a_initialconditions_manual(st2,
quote( 100 + stock__minlen ),
quote( 1e4 + 0 * stock__minlen ) ),
# Natural mortality with per-step deviates
g3a_naturalmortality(st$imm, g3a_naturalmortality_exp(st_Mdn)),
g3a_naturalmortality(st$mat, g3a_naturalmortality_exp(st_Mdn)),
# Natural mortality with per-year random walk
g3a_naturalmortality(st2, g3a_naturalmortality_exp(
g3_param_project(
"Mrw",
g3_param_project_rwalk(),
# The same value will be used for each step
by_step = FALSE,
# by_stock means the stock name will be included in parameter names
by_stock = st2 ))),
NULL )
model_fn <- g3_to_r(c(actions, list(
g3a_report_history(actions, 'proj_.*', out_prefix = NULL),
NULL )))
# Mdn has a parameter for each year/step, as well as mean/sd (added above) & likelihood weighting
grep("^fish.Mdn", names(attr(model_fn, 'parameter_template')), value = TRUE)
# Mrw has parameters for each year
grep("^other.Mrw", names(attr(model_fn, 'parameter_template')), value = TRUE)
attr(model_fn, 'parameter_template') |>
g3_init_val("stst.Mdn.#.#", 0.5, lower = 0.1, upper = 0.9, random = TRUE) |>
g3_init_val("stst.Mdn.proj.dnorm.lmean", 0.1) |>
g3_init_val("stst.Mdn.proj.dnorm.lstddev", 0.001) |>
g3_init_val("other.Mrw.proj.rwalk.mean", 0) |>
g3_init_val("other.Mrw.proj.rwalk.stddev", 0.001) |>
g3_init_val("other.Mrw.#", 0.5, lower = 0.1, upper = 0.9, random = TRUE) |>
# Project forwards 20 years
g3_init_val("project_years", 20) |>
# Don't include projections in nll calculations:
# allows a stddev to be supplied for projections, but estimated freely
g3_init_val("proj_rwalk_fish_Mrw_weight", 0) |>
g3_init_val("proj_dnorm_fish_Mdn_weight", 0) |>
identity() -> params
r <- attributes(model_fn(params))
# Values used for dnorm
plot(r$proj_dnorm_fish_Mdn__var)
# Values used for random walk
plot(r$proj_rwalk_other_Mrw__var)
### Plot values for an individual projection function
actions <- list( g3a_time(1990, 1991), g3_param_project("M", g3_param_project_dlnorm()) )
model_fn <- g3_to_r(c(actions, list(
g3a_report_history(actions, 'proj_.*', out_prefix = NULL),
NULL )))
attr(model_fn, 'parameter_template') |>
g3_init_val("M.proj.dlnorm.lmean", log(20)) |>
g3_init_val("M.proj.dlnorm.lstddev", log(1e-6)) |>
g3_init_val("M.#.#", 20) |>
g3_init_val("project_years", 100) |>
identity() -> params
par(mfrow=c(3, 1))
plot(attr(model_fn(params |>
g3_init_val("M.proj.dlnorm.lstddev", log(1.001)) ), "proj_dlnorm_M__lvar"), ylim = c(15, 25))
plot(attr(model_fn(params |>
g3_init_val("M.proj.dlnorm.lstddev", log(1e-1)) ), "proj_dlnorm_M__lvar"), ylim = c(15, 25))
plot(attr(model_fn(params |>
g3_init_val("M.proj.dlnorm.lstddev", log(1e-2)) ), "proj_dlnorm_M__lvar"), ylim = c(15, 25))
Run the code above in your browser using DataLab