Skip to content

Commit

Permalink
getDiet() now also includes the contribution of the external encoun…
Browse files Browse the repository at this point in the history
…ter rate to the diet.
  • Loading branch information
gustavdelius committed Aug 19, 2024
1 parent cc039f2 commit 4b2cef7
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 35 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Development version

- `getDiet()` now also includes the contribution of the external encounter rate
to the diet.
- Improved scaling of the y-axis in `plotGrowthCurves()`.
- `steadySingleSpecies()` no longer requires species to grow to `w_max`.
- `matchGrowth()` now also rescales the external encounter rate.
Expand Down
41 changes: 23 additions & 18 deletions R/summary_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ NULL
#' Get diet of predator at size, resolved by prey species
#'
#' Calculates the rate at which a predator of a particular species and size
#' consumes biomass of each prey species and resource.
#' The diet has units of grams/year.
#' consumes biomass of each prey species, resource, and other components of the
#' ecosystem. Returns either the rates in grams/year or the proportion of the
#' total consumption rate.
#'
#' Returns the rates \eqn{D_{ij}(w)} at which a predator of species \eqn{i}
#' and size \eqn{w} consumes biomass from prey species \eqn{j}. This is
#' The rates \eqn{D_{ij}(w)} at which a predator of species \eqn{i}
#' and size \eqn{w} consumes biomass from prey species \eqn{j} are
#' calculated from the predation kernel \eqn{\phi_i(w, w_p)},
#' the search volume \eqn{\gamma_i(w)}, the feeding level \eqn{f_i(w)}, the
#' species interaction matrix \eqn{\theta_{ij}} and the prey abundance density
Expand All @@ -51,25 +52,27 @@ NULL
#' D_{ij}(w, w_p) = (1-f_i(w)) \gamma_i(w) \theta_{ij}
#' \int N_j(w_p) \phi_i(w, w_p) w_p dw_p.
#' }
#' The prey index \eqn{j} runs over all species and the resource. It also runs
#' over any extra ecosystem components in your model for which you have
#' defined an encounter rate function. This encounter rate is multiplied by
#' \eqn{1-f_i(w)} to give the rate of consumption of biomass from these extra
#' components.
#' The prey index \eqn{j} runs over all species and the resource.
#'
#' This function performs the same integration as
#' [getEncounter()] but does not aggregate over prey species, and
#' multiplies by \eqn{1-f_i(w)} to get the consumed biomass rather than the
#' available biomass. Outside the range of sizes for a predator species the
#' returned rate is zero.
#' Extra columns are added for the external encounter rate and for any extra
#' ecosystem components in your model for which you have defined an encounter
#' rate function. These encounter rates are multiplied by \eqn{1-f_i(w)} to give
#' the rate of consumption of biomass from these extra components.
#'
#' This function performs the same integration as [getEncounter()] but does not
#' aggregate over prey species, and multiplies by \eqn{1-f_i(w)} to get the
#' consumed biomass rather than the available biomass. Outside the range of
#' sizes for a predator species the returned rate is zero.
#'
#' @inheritParams getEncounter
#' @param proportion If TRUE (default) the function returns the diet as a
#' proportion of the total consumption rate. If FALSE it returns the
#' consumption rate in grams per year.
#'
#' @return An array (predator species x predator size x
#' (prey species + resource + other components) )
#' (prey species + resource + other components). Dimnames are "prey", "w",
#' and "predator".
#'
#' @export
#' @family summary functions
#' @concept summary_function
Expand All @@ -92,11 +95,11 @@ getDiet <- function(params,
other_names <- names(params@other_encounter)
assert_that(identical(dim(n), c(no_sp, no_w)),
length(n_pp) == no_w_full)
diet <- array(0, dim = c(no_sp, no_w, no_sp + 1 + no_other),
diet <- array(0, dim = c(no_sp, no_w, no_sp + 2 + no_other),
dimnames = list("predator" = species,
"w" = dimnames(params@initial_n)$w,
"prey" = c(as.character(species),
"Resource",
"Resource", "External",
other_names)))
# idx_sp are the index values of object@w_full such that
# object@w_full[idx_sp] = object@w
Expand Down Expand Up @@ -141,9 +144,11 @@ getDiet <- function(params,
diet[, , 1:(no_sp + 1)] <- sweep(sweep(diet[, , 1:(no_sp + 1), drop = FALSE],
c(1, 3), inter, "*"),
c(1, 2), params@search_vol, "*")
# Add diet from external sources
diet[, , no_sp + 2] <- params@ext_encounter
# Add diet from other components
for (i in seq_along(params@other_encounter)) {
diet[, , no_sp + 1 + i] <-
diet[, , no_sp + 2 + i] <-
do.call(params@other_encounter[[i]],
list(params = params,
n = n, n_pp = n_pp, n_other = n_other,
Expand Down
32 changes: 17 additions & 15 deletions man/getDiet.Rd

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

129 changes: 129 additions & 0 deletions tests/testthat/_snaps/summary_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3997,6 +3997,135 @@
Cod 3.002041e+00 4.763716e+00 8.214362e+00 1.294631e+01 2.035887e+01
Saithe 3.407671e+00 5.497502e+00 9.324405e+00 1.491433e+01 2.372086e+01
, , prey = External
w
predator 0.001 0.00119 0.00142 0.0017 0.00203 0.00242 0.00289 0.00345 0.00411
Sprat 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 0 0 0 0
Dab 0 0 0 0 0 0 0 0 0
Whiting 0 0 0 0 0 0 0 0 0
Sole 0 0 0 0 0 0 0 0 0
Gurnard 0 0 0 0 0 0 0 0 0
Plaice 0 0 0 0 0 0 0 0 0
Haddock 0 0 0 0 0 0 0 0 0
Cod 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0
w
predator 0.00491 0.00586 0.00699 0.00834 0.00995 0.0119 0.0142 0.0169 0.0202
Sprat 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 0 0 0 0
Dab 0 0 0 0 0 0 0 0 0
Whiting 0 0 0 0 0 0 0 0 0
Sole 0 0 0 0 0 0 0 0 0
Gurnard 0 0 0 0 0 0 0 0 0
Plaice 0 0 0 0 0 0 0 0 0
Haddock 0 0 0 0 0 0 0 0 0
Cod 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0
w
predator 0.0241 0.0288 0.0343 0.0409 0.0489 0.0583 0.0696 0.083 0.0991 0.118
Sprat 0 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
w
predator 16.7 19.9 23.8 28.4 33.8 40.4 48.2 57.5 68.7 81.9 97.8 117 139 166
Sprat 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dab 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Whiting 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sole 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Gurnard 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Plaice 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Haddock 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Cod 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0 0 0 0 0 0
w
predator 198 237 282 337 402 480 573 683 816 973 1160 1390 1650 1970 2360 2810
Sprat 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dab 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Whiting 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sole 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Gurnard 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Plaice 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Haddock 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Cod 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
w
predator 3350 4000 4780 5700 6800 8120 9690 11600 13800 16500 19600 23400
Sprat 0 0 0 0 0 0 0 0 0 0 0 0
Sandeel 0 0 0 0 0 0 0 0 0 0 0 0
N.pout 0 0 0 0 0 0 0 0 0 0 0 0
Herring 0 0 0 0 0 0 0 0 0 0 0 0
Dab 0 0 0 0 0 0 0 0 0 0 0 0
Whiting 0 0 0 0 0 0 0 0 0 0 0 0
Sole 0 0 0 0 0 0 0 0 0 0 0 0
Gurnard 0 0 0 0 0 0 0 0 0 0 0 0
Plaice 0 0 0 0 0 0 0 0 0 0 0 0
Haddock 0 0 0 0 0 0 0 0 0 0 0 0
Cod 0 0 0 0 0 0 0 0 0 0 0 0
Saithe 0 0 0 0 0 0 0 0 0 0 0 0
w
predator 28000 33400 39900
Sprat 0 0 0
Sandeel 0 0 0
N.pout 0 0 0
Herring 0 0 0
Dab 0 0 0
Whiting 0 0 0
Sole 0 0 0
Gurnard 0 0 0
Plaice 0 0 0
Haddock 0 0 0
Cod 0 0 0
Saithe 0 0 0

# getSSB works

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-summary_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ test_that("getDiet works with additional components", {

diet1 <- getDiet(params, proportion = FALSE)
diet2 <- getDiet(p, proportion = FALSE)
expect_identical(diet1[, , 1:13], diet2[, , 1:13])
expect_identical(diet2[1, 1, 14], 111)
expect_identical(diet1[, , 1:14], diet2[, , 1:14])
expect_identical(diet2[1, 1, 15], 111)
})


Expand Down

0 comments on commit 4b2cef7

Please sign in to comment.