Skip to content

Commit

Permalink
🔥 fix 3 - Final for vignette rendering?
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed May 10, 2024
1 parent c1aa0f6 commit 2768eb8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 45 deletions.
4 changes: 3 additions & 1 deletion R/add_constraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ methods::setMethod(
"add_constraint",
methods::signature(mod = "BiodiversityScenario"),
function(mod, method, ...) {
assertthat::assert_that(!missing(method),
msg = "Set a method for the constraint!")
assertthat::assert_that(
inherits(mod, "BiodiversityScenario"),
!is.Waiver(mod$get_predictors()),
Expand Down Expand Up @@ -725,7 +727,7 @@ methods::setMethod(
#' @param value A [`numeric`] value describing the minimum amount of area of a
#' given patch
#' @param unit A [`character`] of the unit of area. Options available are
#' \code{km2} (Default) and \code{ha}.
#' \code{km2} (Default), \code{ha} and \code{pixel}.
#' @param establishment_step A [`logical`] flag indicating whether a given patch
#' is only to be removed if wasn't small in a previous time step (not yet
#' implemented!)
Expand Down
6 changes: 5 additions & 1 deletion R/project.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ methods::setMethod(
# get predictor names of integrated model
if (!is.Waiver(fit$.internals)) {
int_pred_names <- lapply(fit$.internals, function(i) i$model$model$predictors_names)
} else (int_pred_names <- NULL)
} else { int_pred_names <- NULL }

if (is.Waiver(fit$.internals)) {
assertthat::assert_that(all(mod_pred_names %in% pred_names),
Expand Down Expand Up @@ -234,6 +234,10 @@ methods::setMethod(
}
}

if(("threshold" %in% names(scenario_constraints)) && is.Waiver(baseline_threshold)){
if(getOption('ibis.setupmessages', default = TRUE)) myLog('[Scenario]','yellow','Threshold constraint found but not threshold set? Apply threshold()!')
}

if("connectivity" %in% names(scenario_constraints) && "dispersal" %notin% names(scenario_constraints)){
if(getOption('ibis.setupmessages', default = TRUE)) myLog('[Scenario]','yellow','Connectivity contraints make most sense with a dispersal constraint.')
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils-scenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ st_minsize <- function(obj, value, unit = "km2", establishment_step = FALSE){
new <- terra::mask(new, ori.obj)
} else {
# Now first label
labs <- terra::patches(obj)
labs <- terra::patches(obj,zeroAsNA = TRUE)

# Then calculate area in km2
ar <- terra::mask(terra::cellSize(labs, unit = "km"), labs)
Expand Down
13 changes: 8 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,20 @@ capitalize_text <- function(x) {
#' @keywords utils
#'
#' @examples
#' capitalize_text('presence')
#' capitalize_text('ducks are the best birds')
#' factor_to_numeric(factor("5"))
#' factor_to_numeric(factor("test"))
#'
#' @noRd
#'
#' @keywords internal
factor_to_numeric <- function(x) {
if(is.numeric(x)) return(x)
assertthat::assert_that(is.factor(x) || is.character(x))
as.numeric(
as.character(
x
suppressWarnings(
as.numeric(
as.character(
x
)
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion vignettes/articles/04_biodiversity_projections.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ sc.fit2 <- sc |>
# Also fit one projection a nichelimit has been added
sc.fit3 <- sc |>
add_constraint(method = "sdd_nex", value = 1e5) |>
add_constraint(method = "sdd_nex", value = 1e5) |>
add_constraint_adaptability(method = "nichelimit") |>
# Directly fit the object
project(stabilize = F)
Expand Down
74 changes: 38 additions & 36 deletions vignettes/articles/05_mechanistic_estimation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ op <- try({
quiet = TRUE,
build_vignettes = FALSE) # For steps
},silent = TRUE)
if(inherits(op, "try-error")) stop("steps could not be installed for this vignette!")
```

### Adding dispersal to scenarios with `KISSMig`
Expand Down Expand Up @@ -263,42 +262,45 @@ assumptions have been questioned before and should be interpreted with caution (
__Users should always clearly understand the rationale behind parameter choices!__

```{r steps}
require("steps")
# Define some arbitrary vital rates for the transition for this purpose
# Define vital rates
vt <- matrix(c(0.0,0.52,0.75,
0.52,0.28,0.0,
0.0,0.52,0.75),
nrow = 3, ncol = 3, byrow = TRUE)
colnames(vt) <- rownames(vt) <- c('juvenile','subadult','adult')
if("steps" %in% installed.packages()[,1]){
require("steps")
# Define some arbitrary vital rates for the transition for this purpose
# Define vital rates
vt <- matrix(c(0.0,0.52,0.75,
0.52,0.28,0.0,
0.0,0.52,0.75),
nrow = 3, ncol = 3, byrow = TRUE)
colnames(vt) <- rownames(vt) <- c('juvenile','subadult','adult')
# We again specify a scenario as before using the fitted model
prj <- scenario(fit) |>
# Apply the same variable transformations as above.
add_predictors(pred_future, transform = 'scale') |>
# Calculate thresholds at each time step. The threshold estimate is taken from
# the fitted model object.
threshold() |>
# We then specify that we we
simulate_population_steps(vital_rates = vt)
# Notice how we have added steps as additional simulation outcome
prj
# Now project
scenario1 <- project(prj)
plot(scenario1, "population")
# Also see a different one where we add a dispersal constraint and density dependence
dispersal <- steps::fast_dispersal(dispersal_kernel = steps::exponential_dispersal_kernel(distance_decay = 1))
scenario2 <- project(prj |>
simulate_population_steps(vt,
dispersal = dispersal,
density_dependence = steps::ceiling_density(3) ) )
# We can see that the dispersal constraint and higher density dependence cleary
# results in a population abundance that tends to be concentrated in central Europe.
plot(scenario2, "population")
# We again specify a scenario as before using the fitted model
prj <- scenario(fit) |>
# Apply the same variable transformations as above.
add_predictors(pred_future, transform = 'scale') |>
# Calculate thresholds at each time step. The threshold estimate is taken from
# the fitted model object.
threshold() |>
# We then specify that we we
simulate_population_steps(vital_rates = vt)
# Notice how we have added steps as additional simulation outcome
prj
# Now project
scenario1 <- project(prj)
plot(scenario1, "population")
# Also see a different one where we add a dispersal constraint and density dependence
dispersal <- steps::fast_dispersal(dispersal_kernel = steps::exponential_dispersal_kernel(distance_decay = 1))
scenario2 <- project(prj |>
simulate_population_steps(vt,
dispersal = dispersal,
density_dependence = steps::ceiling_density(3) ) )
# We can see that the dispersal constraint and higher density dependence cleary
# results in a population abundance that tends to be concentrated in central Europe.
plot(scenario2, "population")
}
```

0 comments on commit 2768eb8

Please sign in to comment.