Skip to content

Commit 8954576

Browse files
committed
Improving documentation
1 parent 5bc0e89 commit 8954576

File tree

277 files changed

+2155
-112873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+2155
-112873
lines changed

R/MizerSim-class.R

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,7 @@ MizerSim <- function(params, t_dimnames = NA, t_max = 100, t_save = 1) {
252252
#' \linkS4class{MizerSim} class or the \linkS4class{MizerParams} class gains
253253
#' extra slots. MizerSim objects created in older versions of mizer are then no
254254
#' longer valid in the new version because of the missing slots. You need to
255-
#' upgrade them with
256-
#' ```
257-
#' sim <- validSim(sim)
258-
#' ```
259-
#' where `sim` should be replaced by the name of your MizerSim object.
255+
#' upgrade them with this function.
260256
#'
261257
#' This function adds the missing slots and fills them with default values. It
262258
#' also calls [validParams()] to upgrade the MizerParams object inside the
@@ -347,7 +343,6 @@ NResource <- function(sim) {
347343
#' @param sim A MizerSim object
348344
#' @return For `finalN()`: An array (species x size) holding the consumer
349345
#' number densities at the end of the simulation
350-
#' @seealso [idxFinalT()]
351346
#' @export
352347
#' @examples
353348
#' str(finalN(NS_sim))
@@ -372,10 +367,8 @@ finalNResource <- function(sim) {
372367
sim@n_pp[dim(sim@n_pp)[[1]], ]
373368
}
374369

375-
#' Time index at end of simulation
376-
#'
377-
#' @param sim A MizerSim object
378-
#' @return An integer giving the index for extracting the
370+
#' @rdname finalN
371+
#' @return For `idxFinalT()`: An integer giving the index for extracting the
379372
#' results for the final time step
380373
#' @export
381374
#' @examples

R/plotBiomassObservedVsModel.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@
5555
#' params <- calibrateBiomass(params)
5656
#'
5757
#' # Plot with default options
58-
#' plotBiomassObservedVsModel(params)
58+
#' plotBiomassObservedVsModel(params, ratio = FALSE)
5959
#'
6060
#' # Plot including also species without observations
61-
#' plotBiomassObservedVsModel(params, show_unobserved = TRUE)
61+
#' plotBiomassObservedVsModel(params, show_unobserved = TRUE, ratio = FALSE)
6262
#'
6363
#' # Show the ratio instead
64-
#' plotBiomassObservedVsModel(params, ratio = TRUE)
64+
#' plotBiomassObservedVsModel(params)
6565
plotBiomassObservedVsModel <- function(object, species = NULL, ratio = TRUE,
6666
log_scale = TRUE, return_data = FALSE,
6767
labels = TRUE, show_unobserved = FALSE) {

R/plots.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,6 @@ plotDiet <- function(object, species = NULL, return_data = FALSE) {
13721372
#' params <- NS_params
13731373
#' sim <- project(params, effort=1, t_max=20, t_save = 2, progress_bar = FALSE)
13741374
#' plot(sim)
1375-
#' plot(sim, time_range = 10:20) # change time period for size-based plots
1376-
#' plot(sim, min_w = 10, max_w = 1000) # change size range for biomass plot
13771375
#' }
13781376
setMethod("plot", signature(x = "MizerSim", y = "missing"),
13791377
function(x, ...) {
@@ -1409,12 +1407,10 @@ setMethod("plot", signature(x = "MizerSim", y = "missing"),
14091407
#' @export
14101408
#' @family plotting functions
14111409
#' @seealso [plotting_functions]
1412-
#' @rdname plotMizerSim
14131410
#' @examples
14141411
#' \donttest{
14151412
#' params <- NS_params
14161413
#' plot(params)
1417-
#' plot(params, min_w = 10, max_w = 1000) # change size range for biomass plot
14181414
#' }
14191415
setMethod("plot", signature(x = "MizerParams", y = "missing"),
14201416
function(x, ...) {

R/pred_kernel_funcs.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
#' predator/prey mass ratios in the \code{ppmr} argument.
2525
#' @export
2626
#' @family predation kernel
27+
#' @seealso [setPredKernel()]
28+
#' @examples
29+
#' params <- NS_params
30+
#' plot(w_full(params), getPredKernel(params)["Cod", 10, ], type="l", log="x")
31+
#' # The restriction that the kernel is zero for w/w_p < 1 is more
32+
#' # noticeable for larger sigma
33+
#' species_params(params)$sigma <- 4
34+
#' plot(w_full(params), getPredKernel(params)["Cod", 10, ], type="l", log="x")
2735
lognormal_pred_kernel <- function(ppmr, beta, sigma) {
2836
Beta <- log(beta)
2937
phi <- exp(-(log(ppmr) - Beta)^2 / (2 * sigma^2))
@@ -58,6 +66,11 @@ lognormal_pred_kernel <- function(ppmr, beta, sigma) {
5866
#' predator/prey mass ratios in the `ppmr` argument.
5967
#' @export
6068
#' @family predation kernel
69+
#' @seealso [setPredKernel()]
70+
#' @examples
71+
#' params <- NS_params
72+
#' species_params(params)$pred_kernel_type <- "truncated_lognormal"
73+
#' plot(w_full(params), getPredKernel(params)["Cod", 10, ], type="l", log="x")
6174
truncated_lognormal_pred_kernel <- function(ppmr, beta, sigma) {
6275
Beta <- log(beta)
6376
phi <- exp(-(log(ppmr) - Beta)^2 / (2 * sigma^2))
@@ -86,6 +99,14 @@ truncated_lognormal_pred_kernel <- function(ppmr, beta, sigma) {
8699
#' predator/prey mass ratios in the `ppmr` argument.
87100
#' @export
88101
#' @family predation kernel
102+
#' @seealso [setPredKernel()]
103+
#' @examples
104+
#' params <- NS_params
105+
#' # Set all required paramters before changing kernel type
106+
#' species_params(params)$ppmr_max <- 4000
107+
#' species_params(params)$ppmr_min <- 200
108+
#' species_params(params)$pred_kernel_type <- "box"
109+
#' plot(w_full(params), getPredKernel(params)["Cod", 10, ], type="l", log="x")
89110
box_pred_kernel <- function(ppmr, ppmr_min, ppmr_max) {
90111
assert_that(ppmr_min < ppmr_max)
91112
phi <- rep(1, length(ppmr))
@@ -122,6 +143,17 @@ box_pred_kernel <- function(ppmr, ppmr_min, ppmr_max) {
122143
#' predator/prey mass ratios in the `ppmr` argument.
123144
#' @export
124145
#' @family predation kernel
146+
#' @seealso [setPredKernel()]
147+
#' @examples
148+
#' params <- NS_params
149+
#' # Set all required paramters before changing kernel type
150+
#' species_params(params)["Cod", "kernel_exp"] <- -0.8
151+
#' species_params(params)["Cod", "kernel_l_l"] <- 4.6
152+
#' species_params(params)["Cod", "kernel_u_l"] <- 3
153+
#' species_params(params)["Cod", "kernel_l_r"] <- 12.5
154+
#' species_params(params)["Cod", "kernel_u_r"] <- 4.3
155+
#' species_params(params)["Cod", "kernel_type"] <- "power_law"
156+
#' plot(w_full(params), getPredKernel(params)["Cod", 10, ], type="l", log="x")
125157
power_law_pred_kernel <- function(ppmr, kernel_exp,
126158
kernel_l_l, kernel_u_l,
127159
kernel_l_r, kernel_u_r) {

R/rate_functions.R

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ getFeedingLevel <- function(object, n, n_pp, n_other,
168168
#' @param params A MizerParams object
169169
#' @return A matrix (species x size) with the critical feeding level
170170
#' @export
171+
#' @examples
172+
#' \donttest{
173+
#' str(getFeedingLevel(NS_params))
174+
#' }
171175
getCriticalFeedingLevel <- function(params) {
172176
params <- validParams(params)
173177
params@metab / params@intake_max / params@species_params$alpha
@@ -195,8 +199,10 @@ getCriticalFeedingLevel <- function(params) {
195199
#' # Project with constant fishing effort for all gears for 20 time steps
196200
#' sim <- project(params, t_max = 20, effort = 0.5)
197201
#' # Get the energy at a particular time step
198-
#' getEReproAndGrowth(params, n = N(sim)[15, , ],
199-
#' n_pp = NResource(sim)[15, ], t = 15)
202+
#' e <- getEReproAndGrowth(params, n = N(sim)[15, , ],
203+
#' n_pp = NResource(sim)[15, ], t = 15)
204+
#' # Rate at this time for Sprat of size 2g
205+
#' e["Sprat", "2"]
200206
#' }
201207
getEReproAndGrowth <- function(params, n = initialN(params),
202208
n_pp = initialNResource(params),
@@ -233,10 +239,14 @@ getEReproAndGrowth <- function(params, n = initialN(params),
233239
#' @examples
234240
#' \donttest{
235241
#' params <- NS_params
242+
#' # Predation rate in initial state
243+
#' pred_rate <- getPredRate(params)
244+
#' str(pred_rate)
236245
#' # With constant fishing effort for all gears for 20 time steps
237246
#' sim <- project(params, t_max = 20, effort = 0.5)
238247
#' # Get the feeding level at one time step
239-
#' getPredRate(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ])
248+
#' pred_rate <- getPredRate(params, n = N(sim)[15, , ],
249+
#' n_pp = NResource(sim)[15, ], t = 15)
240250
#' }
241251
getPredRate <- function(params, n = initialN(params),
242252
n_pp = initialNResource(params),
@@ -276,14 +286,18 @@ getPredRate <- function(params, n = initialN(params),
276286
#' @examples
277287
#' \donttest{
278288
#' params <- NS_params
289+
#' # Predation mortality in initial state
290+
#' M2 <- getPredMort(params)
291+
#' str(M2)
279292
#' # With constant fishing effort for all gears for 20 time steps
280293
#' sim <- project(params, t_max = 20, effort = 0.5)
281294
#' # Get predation mortality at one time step
282-
#' getPredMort(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ])
295+
#' M2 <- getPredMort(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ])
283296
#' # Get predation mortality at all saved time steps
284-
#' getPredMort(sim)
297+
#' M2 <- getPredMort(sim)
298+
#' str(M2)
285299
#' # Get predation mortality over the years 15 - 20
286-
#' getPredMort(sim, time_range = c(15, 20))
300+
#' M2 <- getPredMort(sim, time_range = c(15, 20))
287301
#' }
288302
getPredMort <- function(object, n, n_pp, n_other,
289303
time_range, drop = TRUE, ...) {
@@ -416,24 +430,25 @@ getM2Background <- getResourceMort
416430
#' @examples
417431
#' \donttest{
418432
#' params <-NS_params
419-
#' # Get the fishing mortality when effort is constant
420-
#' # for all gears and time:
421-
#' getFMortGear(params, effort = 1)
422-
#' # Get the fishing mortality when effort is different
423-
#' # between the four gears but constant in time:
424-
#' getFMortGear(params, effort = c(0.5, 1, 1.5, 0.75))
433+
#' # Get the fishing mortality in initial state
434+
#' F <- getFMortGear(params, effort = 1)
435+
#' str(F)
436+
#' # Get the initial fishing mortality when effort is different
437+
#' # between the four gears:
438+
#' F <- getFMortGear(params, effort = c(0.5, 1, 1.5, 0.75))
425439
#' # Get the fishing mortality when effort is different
426440
#' # between the four gears and changes with time:
427441
#' effort <- array(NA, dim = c(20, 4))
428442
#' effort[, 1] <- seq(from=0, to = 1, length = 20)
429443
#' effort[, 2] <- seq(from=1, to = 0.5, length = 20)
430444
#' effort[, 3] <- seq(from=1, to = 2, length = 20)
431445
#' effort[, 4] <- seq(from=2, to = 1, length = 20)
432-
#' getFMortGear(params, effort = effort)
446+
#' F <- getFMortGear(params, effort = effort)
447+
#' str(F)
433448
#' # Get the fishing mortality using the effort already held in a MizerSim object.
434449
#' sim <- project(params, t_max = 20, effort = 0.5)
435-
#' getFMortGear(sim)
436-
#' getFMortGear(sim, time_range = c(10, 20))
450+
#' F <- getFMortGear(sim)
451+
#' F <- getFMortGear(sim, time_range = c(10, 20))
437452
#' }
438453
#'
439454
getFMortGear <- function(object, effort, time_range) {
@@ -532,25 +547,26 @@ getFMortGear <- function(object, effort, time_range) {
532547
#' @examples
533548
#' \donttest{
534549
#' params <- NS_params
535-
#' # Get the total fishing mortality when effort is constant for all
536-
#' # gears and time:
537-
#' getFMort(params, effort = 1)
538-
#' # Get the total fishing mortality when effort is different
539-
#' # between the four gears but constant in time:
540-
#' getFMort(params, effort = c(0.5,1,1.5,0.75))
550+
#' # Get the total fishing mortality in the initial state
551+
#' F <- getFMort(params, effort = 1)
552+
#' str(F)
553+
#' # Get the initial total fishing mortality when effort is different
554+
#' # between the four gears:
555+
#' F <- getFMort(params, effort = c(0.5,1,1.5,0.75))
541556
#' # Get the total fishing mortality when effort is different
542557
#' # between the four gears and changes with time:
543558
#' effort <- array(NA, dim = c(20,4))
544559
#' effort[, 1] <- seq(from = 0, to = 1, length = 20)
545560
#' effort[, 2] <- seq(from = 1, to = 0.5, length = 20)
546561
#' effort[, 3] <- seq(from = 1, to = 2, length = 20)
547562
#' effort[, 4] <- seq(from = 2, to = 1, length = 20)
548-
#' getFMort(params, effort = effort)
563+
#' F <- getFMort(params, effort = effort)
564+
#' str(F)
549565
#' # Get the total fishing mortality using the effort already held in a
550566
#' # MizerSim object.
551567
#' sim <- project(params, t_max = 20, effort = 0.5)
552-
#' getFMort(sim)
553-
#' getFMort(sim, time_range = c(10, 20))
568+
#' F <- getFMort(sim)
569+
#' F <- getFMort(sim, time_range = c(10, 20))
554570
#' }
555571
getFMort <- function(object, effort, time_range, drop = TRUE) {
556572
if (is(object, "MizerParams")) {
@@ -665,8 +681,10 @@ getFMort <- function(object, effort, time_range, drop = TRUE) {
665681
#' # Project with constant fishing effort for all gears for 20 time steps
666682
#' sim <- project(params, t_max = 20, effort = 0.5)
667683
#' # Get the total mortality at a particular time step
668-
#' getMort(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ],
669-
#' t = 15, effort = 0.5)
684+
#' mort <- getMort(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ],
685+
#' t = 15, effort = 0.5)
686+
#' # Mortality rate at this time for Sprat of size 2g
687+
#' mort["Sprat", "2"]
670688
#' }
671689
getMort <- function(params,
672690
n = initialN(params),
@@ -718,8 +736,10 @@ getZ <- getMort
718736
#' params <- NS_params
719737
#' # Project with constant fishing effort for all gears for 20 time steps
720738
#' sim <- project(params, t_max = 20, effort = 0.5)
721-
#' # Get the energy at a particular time step
722-
#' getERepro(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ], t = 15)
739+
#' # Get the rate at a particular time step
740+
#' erepro <- getERepro(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ], t = 15)
741+
#' # Rate at this time for Sprat of size 2g
742+
#' erepro["Sprat", "2"]
723743
#' }
724744
getERepro <- function(params, n = initialN(params),
725745
n_pp = initialNResource(params),
@@ -763,7 +783,9 @@ getESpawning <- getERepro
763783
#' # Project with constant fishing effort for all gears for 20 time steps
764784
#' sim <- project(params, t_max = 20, effort = 0.5)
765785
#' # Get the energy at a particular time step
766-
#' getEGrowth(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ], t = 15)
786+
#' growth <- getEGrowth(params, n = N(sim)[15, , ], n_pp = NResource(sim)[15, ], t = 15)
787+
#' # Growth rate at this time for Sprat of size 2g
788+
#' growth["Sprat", "2"]
767789
#' }
768790
getEGrowth <- function(params, n = initialN(params),
769791
n_pp = initialNResource(params),

R/setFishing.R

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,28 @@ getInitialEffort <- function(params) {
402402
#' effort invested into fishing with that gear. The effort value for each gear
403403
#' is multiplied by the catchability and the selectivity to determine the
404404
#' fishing mortality imposed by that gear, see [setFishing()] for more details.
405-
#'
406405
#' The initial effort you have set can be overruled when running a simulation
407406
#' by providing an `effort` argument to [project()] which allows you to
408407
#' specify a time-varying effort.
409408
#'
409+
#' A valid effort vector is a named vector with one effort value for each gear.
410+
#' However you can also supply the effort value in different ways:
411+
#'
412+
#' * a scalar, which is then replicated for each gear
413+
#' * an unnamed vector, which is then assumed to be in the same order as the
414+
#' gears in the params object
415+
#' * a named vector in which the gear names have a different order than in the
416+
#' params object. This is then sorted correctly.
417+
#' * a named vector which only supplies values for some of the gears.
418+
#' The effort for the other gears is then set to zero.
419+
#'
420+
#' These conversions are done by the function `validEffortVector()`.
421+
#'
422+
#' An `effort` argument will lead to an error if it is either
423+
#' * unnamed and of the wrong length
424+
#' * named but where some names do not match any of the gears
425+
#' * not numeric
426+
#'
410427
#' @param params A MizerParams object
411428
#' @return Effort vector
412429
#' @export
@@ -415,7 +432,8 @@ initial_effort <- function(params) {
415432
}
416433

417434
#' @rdname initial_effort
418-
#' @param value The initial fishing effort
435+
#' @param value A vector or scalar with the initial fishing effort, see Details
436+
#' below.
419437
#' @export
420438
`initial_effort<-` <- function(params, value) {
421439
setFishing(params, initial_effort = value)
@@ -602,29 +620,12 @@ validGearParams <- function(gear_params, species_params) {
602620
gear_params
603621
}
604622

605-
#' Return valid effort vector
623+
#' Make a valid effort vector
606624
#'
607-
#' A valid effort vector is a named vector with one effort value for each gear.
608-
#'
609-
#' The function also accepts an `effort` that is not yet valid:
610-
#'
611-
#' * a scalar, which is then replicated for each gear
612-
#' * an unnamed vector, which is then assumed to be in the same order as the
613-
#' gears in the params object
614-
#' * a named vector in which the gear names have a different order than in the
615-
#' params object. This is then sorted correctly.
616-
#' * a named vector which only supplies values for some of the gears.
617-
#' The effort for the other gears is then set to zero.
618-
#'
619-
#' An `effort` argument will lead to an error if it is either
620-
#' * unnamed and of the wrong length
621-
#' * named but where some names do not match any of the gears
622-
#' * not numeric
623-
#'
624-
#' @param effort A vector or scalar.
625+
#' @param effort A vector or scalar with the initial fishing effort, see Details
626+
#' below.
625627
#'
626628
#' @export
627-
#' @concept helper
628629
#' @rdname initial_effort
629630
validEffortVector <- function(effort, params) {
630631
assert_that(is(params, "MizerParams"),
@@ -679,6 +680,7 @@ validEffortVector <- function(effort, params) {
679680
#' @export
680681
#' @examples
681682
#' params <- NS_params
683+
#' str(calc_selectivity(params))
682684
#' calc_selectivity(params)["Pelagic", "Herring", ]
683685
calc_selectivity <- function(params) {
684686

0 commit comments

Comments
 (0)