Skip to content

Commit

Permalink
tuneParams() can now keep the yield matched to observations.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavdelius committed Aug 28, 2024
1 parent 3eb2b59 commit a7b87e6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: mizerExperimental
Type: Package
Title: Extends the mizer package with experimental features
Version: 2.4.0.9004
Version: 2.4.0.9005
Author: Various contributors
Maintainer: Gustav Delius <[email protected]>
Description: This mizer extension package collects contributions from the mizer
Expand Down Expand Up @@ -36,6 +36,7 @@ Suggests:
Collate:
alignResource.R
manipulateParams.R
matchYield.R
mizerExperimental-package.R
plotDiet.R
plotBiomassFlux.R
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(alignResource)
export(distanceSSLogYield)
export(getYieldVsF)
export(markBackground)
export(matchYield)
export(plotBiomassFlux)
export(plotBiomassRelative)
export(plotBiomassVsSpecies)
Expand Down
26 changes: 26 additions & 0 deletions R/matchYield.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Match observed yields
#'
#' This function matches the observed yields of all the gears for all the
#' species by scaling the catchabilities by the ratio between current modelled
#' yield and observed yield.
#'
#' @param params A MizerParams object
#' @return A MizerParams object with updated catchabilities
#' @export
matchYield <- function(params) {
gp <- params@gear_params
if (is.null(gp$yield_observed)) {
stop("You need to specify `yield_observed` in the gear parameter dataframe.")
}
gp <- gp[!is.na(gp$yield_observed), ]
yields <- getYieldGear(params)
for (i in seq_len(nrow(gp))) {
gear <- as.character(gp$gear[i])
species <- as.character(gp$species[i])
ratio <- gp$yield_observed[i] / yields[gear, species]
if (!is.nan(ratio)) {
gear_params(params)[i, "catchability"] <- gp$catchability[i] * ratio
}
}
return(params)
}
6 changes: 3 additions & 3 deletions R/tuneParams_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ tuneParams_run_steady <- function(p, params, params_old, logs, session, input,
progress <- shiny::Progress$new(session)
on.exit(progress$close())

if ("yield" %in% input$match) {
p <- matchYields(p)
}
if ("biomass" %in% input$match) {
p <- matchBiomasses(p)
}
if ("growth" %in% input$match) {
p <- matchGrowth(p, keep = "biomass")
}
if ("yield" %in% input$match) {
p <- matchYield(p)
}

# Run to steady state
if (return_sim) {
Expand Down
19 changes: 19 additions & 0 deletions man/matchYield.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-matchYield.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("matchYield works", {
params <- NS_params
initial_effort(params) <- 1
yields <- getYieldGear(params)
gp <- params@gear_params
# Add yield_observed column but make it twice too large
for (i in seq_len(nrow(gp))) {
species <- as.character(gp$species[i])
gear <- as.character(gp$gear[i])
gp$yield_observed[i] <- yields[gear, species] * 2
}
gear_params(params) <- gp
p <- matchYield(params)
expect_equal(getYieldGear(params) * 2, getYieldGear(p))
})

0 comments on commit a7b87e6

Please sign in to comment.