diff --git a/DESCRIPTION b/DESCRIPTION index 9626d1e3..d1da3802 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 Description: This mizer extension package collects contributions from the mizer @@ -36,6 +36,7 @@ Suggests: Collate: alignResource.R manipulateParams.R + matchYield.R mizerExperimental-package.R plotDiet.R plotBiomassFlux.R diff --git a/NAMESPACE b/NAMESPACE index 0b3cd850..0d9a1e04 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(alignResource) export(distanceSSLogYield) export(getYieldVsF) export(markBackground) +export(matchYield) export(plotBiomassFlux) export(plotBiomassRelative) export(plotBiomassVsSpecies) diff --git a/R/matchYield.R b/R/matchYield.R new file mode 100644 index 00000000..f97b49a5 --- /dev/null +++ b/R/matchYield.R @@ -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) +} diff --git a/R/tuneParams_helpers.R b/R/tuneParams_helpers.R index 5ef29603..647bb546 100644 --- a/R/tuneParams_helpers.R +++ b/R/tuneParams_helpers.R @@ -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) { diff --git a/man/matchYield.Rd b/man/matchYield.Rd new file mode 100644 index 00000000..ae44196a --- /dev/null +++ b/man/matchYield.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/matchYield.R +\name{matchYield} +\alias{matchYield} +\title{Match observed yields} +\usage{ +matchYield(params) +} +\arguments{ +\item{params}{A MizerParams object} +} +\value{ +A MizerParams object with updated catchabilities +} +\description{ +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. +} diff --git a/tests/testthat/test-matchYield.R b/tests/testthat/test-matchYield.R new file mode 100644 index 00000000..4a9b0efa --- /dev/null +++ b/tests/testthat/test-matchYield.R @@ -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)) +})