| Title: | Efficient Modelling of Resource Competition |
|---|---|
| Description: | Generate, simulate and visualise ODE models of consumer-resource interactions. At its core, 'rescomp' provides a resource competition modelling focused interface to 'deSolve', alongside flexible functions for visualising model properties and dynamics. More information, documentation and examples can be found on the package website. |
| Authors: | Christopher R P Brown [aut, cre] (ORCID: <https://orcid.org/0000-0002-0393-3477>), Jan Engelstaedter [aut] (ORCID: <https://orcid.org/0000-0003-3340-918X>), Andrew Letten [aut] (ORCID: <https://orcid.org/0000-0001-6436-7942>) |
| Maintainer: | Christopher R P Brown <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-06-02 07:32:14 UTC |
| Source: | https://github.com/pyrrhicpachyderm/rescomp |
rescomp_event object to modify state variablesApplies the instantaneous changes specified by a rescomp_event object to the full set of state variables (species and resources).
apply_event(event_obj, species, resources, params, time)apply_event(event_obj, species, resources, params, time)
event_obj |
An object of class |
species |
A vector of species concentrations. |
resources |
A vector of resource concentrations. |
params |
A list of time-dependent parameters. |
time |
The current simulation time. |
This function is normally only for internal use, but is exported to aid users in debugging their created rescomp_event objects.
A vector of state variables, species concentrations followed by resource concentrations.
batch_transfer <- event_batch_transfer(dilution = 0.1, resources = c(1, 1)) apply_event( batch_transfer, species = c(100, 200), resources = c(0.5, 2), params = list(), time = 0 # Not used by batch transfer events. )batch_transfer <- event_batch_transfer(dilution = 0.1, resources = c(1, 1)) apply_event( batch_transfer, species = c(100, 200), resources = c(0.5, 2), params = list(), time = 0 # Not used by batch transfer events. )
spnum rows and resnum columnsThis produces an object that, when passed through rescomp functions such as funcresp_type1(), and ultimately provided to spec_rescomp(), inherits spnum and resnum from the arguments to spec_rescomp().
Thus, it is shorthand for matrix(c(...), nrow = spnum, ncol = resnum, byrow = TRUE).
Inheritance only works when passed directly through rescomp functions; it will fail if used in the func provided to a function such as funcresp_custom().
crmatrix(..., byrow = TRUE)crmatrix(..., byrow = TRUE)
... |
One or more numeric vectors that, concatenated by |
byrow |
A logical vector of length 1, equivalent to |
S3 object of class rescomp_crmatrix.
pars <- spec_rescomp( spnum = 3, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.10, 0.08, 0.12, 0.15, 0.05 )) ) plot_rescomp(sim_rescomp(pars))pars <- spec_rescomp( spnum = 3, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.10, 0.08, 0.12, 0.15, 0.05 )) ) plot_rescomp(sim_rescomp(pars))
Define consumer resource ODE function
def_cr_ode(t, y, pars)def_cr_ode(t, y, pars)
t |
The current time of the simulation. |
y |
The vector of current estimates of consumers and resources in the simulation. |
pars |
S3 object of class |
List of length one, containing a vector of derivatives of y with respect to time.
Produces an event object representing a batch transfer by dilution with new medium.
First, all existing species and resource concentrations are multiplied by dilution.
Then, the resource concentrations listed in resources, multiplied by 1 - dilution, are added.
event_batch_transfer(dilution, resources)event_batch_transfer(dilution, resources)
dilution |
A numeric vector or |
resources |
A numeric vector or |
S3 object of class rescomp_event.
m1 <- spec_rescomp( events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event object representing a pulse of added/removed resources.
event_res_add(resources, min_zero = TRUE)event_res_add(resources, min_zero = TRUE)
resources |
A numeric vector or |
min_zero |
If this is TRUE, resulting resource concentrations are clamped to a minimum of zero. If this is FALSE, negative values of |
S3 object of class rescomp_event.
m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_res_add(c(-0.1, 0.1)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_res_add(c(-0.1, 0.1)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event object suitable for building event schedules to pass spec_rescomp().
event_res_custom(func, resnum = NULL)event_res_custom(func, resnum = NULL)
func |
A function that takes |
resnum |
The number of resources. |
If resnum is NULL, spec_rescomp() will attempt to infer it.
This is fine if the result is used directly in an event schedule, but may fail if the result must be combined with other rescomp_events first.
S3 object of class rescomp_event.
pars <- spec_rescomp( events = list( event_schedule_fixed( event = event_res_custom(function(res, params) { res * 2 }), times = 500 ) ) ) plot_rescomp(sim_rescomp(pars))pars <- spec_rescomp( events = list( event_schedule_fixed( event = event_res_custom(function(res, params) { res * 2 }), times = 500 ) ) ) plot_rescomp(sim_rescomp(pars))
Produces an event object a pulse of multiplicatively increased/decreased resource populations, e.g. a density-independent disturbance which kills prey species.
event_res_mult(resources_mult)event_res_mult(resources_mult)
resources_mult |
A numeric vector or |
S3 object of class rescomp_event.
m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_res_mult(c(0.5, 1.5)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_res_mult(c(0.5, 1.5)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event schedule object suitable for passing in the events argument to spec_rescomp().
event_schedule_fixed(event_obj, times, priority = 0)event_schedule_fixed(event_obj, times, priority = 0)
event_obj |
An object of class |
times |
A numeric vector of times at which the event should occur. |
priority |
A number. If two events occur at the same time, the event with the lower priority number is processed first. |
S3 object of class rescomp_event_schedule.
m1 <- spec_rescomp( events = list( event_schedule_fixed( event_res_add(0.5), times = c(200, 400) ), event_schedule_fixed( event_sp_add(-500), times = c(600, 800) ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( events = list( event_schedule_fixed( event_res_add(0.5), times = c(200, 400) ), event_schedule_fixed( event_sp_add(-500), times = c(600, 800) ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event schedule object suitable for passing in the events argument to spec_rescomp().
event_schedule_periodic(event_obj, period, start_time = period, priority = 0)event_schedule_periodic(event_obj, period, start_time = period, priority = 0)
event_obj |
An object of class |
period |
A number: the period at which the event occurs. |
start_time |
A number: the time at which the event first occurs. |
priority |
A number. If two events occur at the same time, the event with the lower priority number is processed first. |
S3 object of class rescomp_event_schedule.
m1 <- spec_rescomp( events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ), event_schedule_periodic( event_batch_transfer(dilution = 0.5, resources = 1), period = 250, start_time = 125 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ), event_schedule_periodic( event_batch_transfer(dilution = 0.5, resources = 1), period = 250, start_time = 125 ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event schedule object suitable for passing in the events argument to spec_rescomp().
event_set_introseq(times, concentrations, priority = 0)event_set_introseq(times, concentrations, priority = 0)
times |
A numeric vector, of length |
concentrations |
A numeric vector or |
priority |
A number. If two events occur at the same time, the event with the lower priority number is processed first. |
S3 object of class rescomp_event_schedule.
m1 <- spec_rescomp( spnum = 4, resnum = 4, funcresp = funcresp_type1(crmatrix( 0.10, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.10 )), events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.5, resources = 1), period = 1000 / 16, priority = 0 ), event_set_introseq( times = c(0, 250, 500, 750), concentrations = c(10, 100, 1000, 2000), priority = 1 ) ), cinit = 0 ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( spnum = 4, resnum = 4, funcresp = funcresp_type1(crmatrix( 0.10, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.10, 0.05, 0.05, 0.05, 0.05, 0.10 )), events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.5, resources = 1), period = 1000 / 16, priority = 0 ), event_set_introseq( times = c(0, 250, 500, 750), concentrations = c(10, 100, 1000, 2000), priority = 1 ) ), cinit = 0 ) plot_rescomp(sim_rescomp(m1))
Produces an event object representing a pulse of added/removed consumers, e.g. a species introduction.
event_sp_add(species, min_zero = TRUE)event_sp_add(species, min_zero = TRUE)
species |
A numeric vector or |
min_zero |
If this is TRUE, resulting species concentrations are clamped to a minimum of zero. If this is FALSE, negative values of |
S3 object of class rescomp_event.
m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_sp_add(c(-100, 100)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_sp_add(c(-100, 100)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))
Produces an event object suitable for building event schedules to pass spec_rescomp().
event_sp_custom(func, spnum = NULL)event_sp_custom(func, spnum = NULL)
func |
A function that takes |
spnum |
The number of species. |
If spnum is NULL, spec_rescomp() will attempt to infer it.
This is fine if the result is used directly in an event schedule, but may fail if the result must be combined with other rescomp_events first.
S3 object of class rescomp_event.
pars <- spec_rescomp( events = list( event_schedule_fixed( event = event_sp_custom(function(sp, params) { sp * 2 }), times = 500 ) ) ) plot_rescomp(sim_rescomp(pars))pars <- spec_rescomp( events = list( event_schedule_fixed( event = event_sp_custom(function(sp, params) { sp * 2 }), times = 500 ) ) ) plot_rescomp(sim_rescomp(pars))
Produces an event object a pulse of multiplicatively increased/decreased consumer populations, e.g. a density-independent disturbance which kills consumer species.
event_sp_mult(species_mult)event_sp_mult(species_mult)
species_mult |
A numeric vector or |
S3 object of class rescomp_event.
m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_sp_mult(c(0.5, 1.5)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1(crmatrix( 0.10, 0.08, 0.06, 0.12 )), events = list( event_schedule_periodic( event_sp_mult(c(0.5, 1.5)), period = 250 ) ) ) plot_rescomp(sim_rescomp(m1))
Convert object of class deSolve to a data frame and name columns
frame_and_name(model)frame_and_name(model)
model |
List output from |
data frame
pars <- spec_rescomp() m1 <- sim_rescomp(pars) frame_and_name(m1)pars <- spec_rescomp() m1 <- sim_rescomp(pars) frame_and_name(m1)
Produces an object suitable to pass as the funcresp to spec_rescomp().
funcresp_custom(func, spnum = NULL, resnum = NULL)funcresp_custom(func, spnum = NULL, resnum = NULL)
func |
A function that takes |
spnum |
The number of species; the number of rows in the matrix returned by |
resnum |
The number of resources; the expected length of the |
If spnum or resnum are NULL, spec_rescomp() will attempt to infer them.
This is fine if the result is passed directly to spec_rescomp(), but may fail if the result must be combined with other rescomp_funcresps first.
S3 object of class rescomp_funcresp.
# Type 1 functional response with fixed growth rates m1 <- spec_rescomp( spnum = 2, resnum = 3, funcresp_custom( function(resources, params) { growth_rates <- c(0.2, 0.3) outer(growth_rates, resources) }, spnum = 2 ) ) plot_funcresp(m1)# Type 1 functional response with fixed growth rates m1 <- spec_rescomp( spnum = 2, resnum = 3, funcresp_custom( function(resources, params) { growth_rates <- c(0.2, 0.3) outer(growth_rates, resources) }, spnum = 2 ) ) plot_funcresp(m1)
Produces an object suitable to pass as the funcresp to spec_rescomp().
Creates a Hill functional response with maximum growth rate mumax, half saturation constant ks, and Hill coefficient n.
mu_ij(R_j) = mumax_ij * (R_j)^n_ij / (ks_ij + (R_j)^n_ij)
This an alternative parameterisation of a type 3 functional response, typically used with quota rather than efficiency.
funcresp_hill(mumax, ks, n)funcresp_hill(mumax, ks, n)
mumax |
A matrix or |
ks |
A matrix or |
n |
A matrix or |
S3 object of class rescomp_funcresp.
m1 <- spec_rescomp( spnum = 3, funcresp = funcresp_hill( mumax = crmatrix(0.2, 0.4, 0.4), ks = crmatrix(0.2, 1, 1), n = crmatrix(1, 1, 2) ) ) plot_funcresp(m1, maxx = 3)m1 <- spec_rescomp( spnum = 3, funcresp = funcresp_hill( mumax = crmatrix(0.2, 0.4, 0.4), ks = crmatrix(0.2, 1, 1), n = crmatrix(1, 1, 2) ) ) plot_funcresp(m1, maxx = 3)
Produces an object suitable to pass as the funcresp to spec_rescomp().
Creates a Monod functional response with maximum growth rate mumax and half saturation constant ks.
mu_ij(R_j) = mumax_ij * R_j / (ks_ij + R_j)
This an alternative parameterisation of a type 2 functional response, typically used with quota rather than efficiency.
funcresp_monod(mumax, ks)funcresp_monod(mumax, ks)
mumax |
A matrix or |
ks |
A matrix or |
S3 object of class rescomp_funcresp.
m1 <- spec_rescomp( spnum = 2, funcresp = funcresp_monod( mumax = crmatrix(0.2, 0.4), ks = crmatrix(0.2, 1) ) ) plot_funcresp(m1, maxx = 3)m1 <- spec_rescomp( spnum = 2, funcresp = funcresp_monod( mumax = crmatrix(0.2, 0.4), ks = crmatrix(0.2, 1) ) ) plot_funcresp(m1, maxx = 3)
Produces an object suitable to pass as the funcresp to spec_rescomp().
Creates a linear or type 1 functional response with attack rate a.
mu_ij(R_j) = a_ij * R_j
funcresp_type1(a)funcresp_type1(a)
a |
A matrix or |
S3 object of class rescomp_funcresp.
m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1( crmatrix( 0.2, 0.3, 0.4, 0.2 ) ) ) plot_funcresp(m1) m2 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1( rescomp_coefs_lerp( crmatrix( 0.2, 0.3, 0.4, 0.2 ), crmatrix( 0.2, 0.3, 0.1, 0.1 ), "antibiotic_concentration" ) ), params = list(antibiotic_concentration = rescomp_param_square()) ) plot_funcresp(m2, display_values = list(antibiotic_concentration = c(0, 0.5, 1)))m1 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1( crmatrix( 0.2, 0.3, 0.4, 0.2 ) ) ) plot_funcresp(m1) m2 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type1( rescomp_coefs_lerp( crmatrix( 0.2, 0.3, 0.4, 0.2 ), crmatrix( 0.2, 0.3, 0.1, 0.1 ), "antibiotic_concentration" ) ), params = list(antibiotic_concentration = rescomp_param_square()) ) plot_funcresp(m2, display_values = list(antibiotic_concentration = c(0, 0.5, 1)))
Produces an object suitable to pass as the funcresp to spec_rescomp().
Creates a Holling type 2 functional response with attack rate a and handling time h.
mu_ij(R_j) = (a_ij * R_j) / (1 + a_ij * h_ij * R_j)
funcresp_type2(a, h)funcresp_type2(a, h)
a |
A matrix or |
h |
A matrix or |
S3 object of class rescomp_funcresp.
m1 <- spec_rescomp( spnum = 2, funcresp = funcresp_type2( a = crmatrix(0.4, 0.2), h = crmatrix(3, 1) ) ) plot_funcresp(m1, maxx = 3)m1 <- spec_rescomp( spnum = 2, funcresp = funcresp_type2( a = crmatrix(0.4, 0.2), h = crmatrix(3, 1) ) ) plot_funcresp(m1, maxx = 3)
Produces an object suitable to pass as the funcresp to spec_rescomp().
Creates a Holling type 3 functional response with attack rate a, handling time h, and exponent k.
mu_ij(R_j) = (a_ij * (R_j)^k_ij) / (1 + a_ij * h_ij * (R_j)^k_ij)
funcresp_type3(a, h, k)funcresp_type3(a, h, k)
a |
A matrix or |
h |
A matrix or |
k |
A matrix or |
S3 object of class rescomp_funcresp.
m1 <- spec_rescomp( spnum = 3, funcresp = funcresp_type3( a = crmatrix(0.5, 0.3, 0.2), h = crmatrix(4, 2, 1), k = crmatrix(2) ) ) plot_funcresp(m1, maxx = 3)m1 <- spec_rescomp( spnum = 3, funcresp = funcresp_type3( a = crmatrix(0.5, 0.3, 0.2), h = crmatrix(4, 2, 1), k = crmatrix(2) ) ) plot_funcresp(m1, maxx = 3)
rescomp_coefs_vector/rescomp_coefs_matrix objectProvides a generic interface to a vector/matrix or a rescomp_coefs_vector/rescomp_coefs_matrix object.
Called on a numeric vector/matrix (as appropriate to the function used), this will just return the vector/matrix.
Called on a rescomp_coefs_vector/rescomp_coefs_matrix, this allows time-dependence via parameters.
get_coefs() works on vector- or matrix-type objects, while the others work only on their specific types.
get_coefs(coefs_obj, params) get_coefs_vector(coefs_obj, params) get_coefs_matrix(coefs_obj, params)get_coefs(coefs_obj, params) get_coefs_vector(coefs_obj, params) get_coefs_matrix(coefs_obj, params)
coefs_obj |
A numeric vector/matrix or an object of class |
params |
A list of time-dependent parameters. |
This function is normally only for internal use, but is exported to aid users in debugging their created rescomp_coefs objects.
A vector/matrix of coefficients.
get_coefs(c(0.2, 0.3, 0.4), list()) get_coefs(matrix(c(0.2, 0.3, 0.4, 0.2), nrow = 2), list()) get_coefs_vector(c(0.2, 0.3, 0.4), list()) try(get_coefs_matrix(c(0.2, 0.3, 0.4), list())) coefs <- rescomp_coefs_matrix_custom( function(params) { matrix(c(0.2, 0.3, 0.4, params$fourth_coeff), nrow = 2, ncol = 2) }, nrow = 2, ncol = 2 ) get_coefs(coefs, list(fourth_coeff = 0.5))get_coefs(c(0.2, 0.3, 0.4), list()) get_coefs(matrix(c(0.2, 0.3, 0.4, 0.2), nrow = 2), list()) get_coefs_vector(c(0.2, 0.3, 0.4), list()) try(get_coefs_matrix(c(0.2, 0.3, 0.4), list())) coefs <- rescomp_coefs_matrix_custom( function(params) { matrix(c(0.2, 0.3, 0.4, params$fourth_coeff), nrow = 2, ncol = 2) }, nrow = 2, ncol = 2 ) get_coefs(coefs, list(fourth_coeff = 0.5))
rescomp_event_schedule objectThis function is normally only for internal use, but is exported to aid users in debugging their created rescomp_event_schedule objects.
get_event_times(event_schedule_obj, totaltime)get_event_times(event_schedule_obj, totaltime)
event_schedule_obj |
An object of class |
totaltime |
The total time which the simulation will run for. |
A vector of times.
schedule1 <- event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ) get_event_times(schedule1, 1000) get_event_times(schedule1, 999) schedule2 <- event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250, start_time = 125 ) get_event_times(schedule2, 1000) get_event_times(schedule2, 999)schedule1 <- event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250 ) get_event_times(schedule1, 1000) get_event_times(schedule1, 999) schedule2 <- event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 250, start_time = 125 ) get_event_times(schedule2, 1000) get_event_times(schedule2, 999)
rescomp_funcresp objectGets the growth rates of each species on each resource, given resource concentrations. These must be combined, according to whether the resouces are essential or substitutable, to get the overall growth rate for each species.
get_funcresp(funcresp_obj, spnum, resources, params)get_funcresp(funcresp_obj, spnum, resources, params)
funcresp_obj |
An object of class |
spnum |
The number of species. |
resources |
A vector of resource concentrations. |
params |
A list of time-dependent parameters. |
This function is normally only for internal use, but is exported to aid users in debugging their created rescomp_funcresp objects.
A matrix of species growth rates on each resource, with spnum rows and length(resources) columns.
funcresp <- funcresp_custom( function(resources, params) { growth_rates <- params$scale * c(0.2, 0.3) outer(growth_rates, resources) }, spnum = 2 ) get_funcresp(funcresp, 2, c(1, 4, 5, 6), list(scale = 2)) get_funcresp(funcresp, 2, 0.7, list(scale = 0.5)) try(get_funcresp(funcresp, 3, 0.7, list(scale = 0.5)))funcresp <- funcresp_custom( function(resources, params) { growth_rates <- params$scale * c(0.2, 0.3) outer(growth_rates, resources) }, spnum = 2 ) get_funcresp(funcresp, 2, c(1, 4, 5, 6), list(scale = 2)) get_funcresp(funcresp, 2, 0.7, list(scale = 0.5)) try(get_funcresp(funcresp, 3, 0.7, list(scale = 0.5)))
rescomp_param objectThis function is normally only for internal use, but is exported to aid users in debugging their created rescomp_param objects.
get_params(param_obj, t)get_params(param_obj, t)
param_obj |
An object of class |
t |
The time at which to get the parameters. |
A numeric vector of length 1; the parameter value.
# Repeated pulsing and exponential decay antibiotic_conc <- rescomp_param_custom(function(t) { 0.5^(10 * (t %% 1)) }) get_params(antibiotic_conc, 0) get_params(antibiotic_conc, 0.5) get_params(antibiotic_conc, 1) params <- list(r = 0.2, antibiotic_conc = antibiotic_conc) get_params(params, 0.5)# Repeated pulsing and exponential decay antibiotic_conc <- rescomp_param_custom(function(t) { 0.5^(10 * (t %% 1)) }) get_params(antibiotic_conc, 0) get_params(antibiotic_conc, 0.5) get_params(antibiotic_conc, 1) params <- list(r = 0.2, antibiotic_conc = antibiotic_conc) get_params(params, 0.5)
rescomp_ressupply objectGets the resource supply rates of each resource, given the current resource concentrations.
get_ressupply(ressupply_obj, resources, params)get_ressupply(ressupply_obj, resources, params)
ressupply_obj |
An object of class |
resources |
A vector of resource concentrations. |
params |
A list of time-dependent parameters. |
This function is normally only for internal use, but is exported to aid users in debugging their created rescomp_ressupply objects.
A vector of rates of change of resource concentrations, of the same length as resources.
# Two resources, A and B, with constant supply of A, and A spontaneously converting to B ressupply <- ressupply_custom( function(resources, params) { conversion <- params$conversion * resources[1] return(c(params$supply - conversion, conversion)) }, resnum = 2 ) get_ressupply(ressupply, c(10, 20), list(supply = 3, conversion = 0.2)) try(get_ressupply(ressupply, c(10, 20, 30), list(supply = 3, conversion = 0.2)))# Two resources, A and B, with constant supply of A, and A spontaneously converting to B ressupply <- ressupply_custom( function(resources, params) { conversion <- params$conversion * resources[1] return(c(params$supply - conversion, conversion)) }, resnum = 2 ) get_ressupply(ressupply, c(10, 20), list(supply = 3, conversion = 0.2)) try(get_ressupply(ressupply, c(10, 20, 30), list(supply = 3, conversion = 0.2)))
Plot functional responses
plot_funcresp(pars, maxx = 1, display_values, madj = FALSE)plot_funcresp(pars, maxx = 1, display_values, madj = FALSE)
pars |
S3 object of class |
maxx |
Numeric vector of length 1. Resource value to calculate per-capita growth rates up to (xlim). |
display_values |
Named list of vectors, with names matching names of pars$params.
Each vector gives the values of the respective model parameters at which to plot the functional responses.
Defaults are automatically inferred for most |
madj |
Logical vector of length 1. Whether to standardise per capita growth rates by mortality. |
It is assumed that the funcresp of pars is constructed such that the growth rate of a species on a given resource depends only on the concentration of that resource, and not on other resources.
This is the case for all built-in functional responses, but is not necessary the case if using funcresp_custom().
Plots are likely to be nonsensical or incorrect if this assumption is violated.
A ggplot object.
pars <- spec_rescomp() plot_funcresp(pars) pars <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_monod( mumax = crmatrix( 0.7, 0.3, 0.4, 0.5 ), ks = crmatrix(1) ) ) plot_funcresp(pars) plot_funcresp(pars, madj = TRUE) plot_funcresp(pars, maxx = 5) pars <- spec_rescomp( spnum = 2, funcresp = funcresp_type1( a = rescomp_coefs_lerp( crmatrix(0.12, 0.08), crmatrix(0.08, 0.12), param_name = "temperature" ) ), params = list(temperature = rescomp_param_sine(period = 250)) ) plot_funcresp(pars) plot_funcresp(pars, display_values = list(temperature = c(0.0, 0.25, 0.5, 0.75, 1.0)))pars <- spec_rescomp() plot_funcresp(pars) pars <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_monod( mumax = crmatrix( 0.7, 0.3, 0.4, 0.5 ), ks = crmatrix(1) ) ) plot_funcresp(pars) plot_funcresp(pars, madj = TRUE) plot_funcresp(pars, maxx = 5) pars <- spec_rescomp( spnum = 2, funcresp = funcresp_type1( a = rescomp_coefs_lerp( crmatrix(0.12, 0.08), crmatrix(0.08, 0.12), param_name = "temperature" ) ), params = list(temperature = rescomp_param_sine(period = 250)) ) plot_funcresp(pars) plot_funcresp(pars, display_values = list(temperature = c(0.0, 0.25, 0.5, 0.75, 1.0)))
Plot consumer and resource dynamics from deSolve output
plot_rescomp( model, consumers = TRUE, resources = TRUE, logy = FALSE, lwd = 1, xlims = NULL )plot_rescomp( model, consumers = TRUE, resources = TRUE, logy = FALSE, lwd = 1, xlims = NULL )
model |
List output from |
consumers |
Plot consumer dynamics? Default = TRUE. |
resources |
Plot resource dynamics? Default = TRUE. |
logy |
Log transfrom y-axis (default = FALSE). |
lwd |
Line width (default = 1) |
xlims |
Vector of length giving the time frame to plot. |
ggplot object
pars <- spec_rescomp() m1 <- sim_rescomp(pars) plot_rescomp(m1)pars <- spec_rescomp() m1 <- sim_rescomp(pars) plot_rescomp(m1)
Produces an object that may be used in place of a vector or matrix in the creation of arguments to spec_rescomp().
Linearly interpolates between two sets of coefficients according to the value of a time-dependent parameter.
rescomp_coefs_lerp(coefs0, coefs1, param_name, param0 = 0, param1 = 1)rescomp_coefs_lerp(coefs0, coefs1, param_name, param0 = 0, param1 = 1)
coefs0, coefs1
|
Vectors, matrices, or objects of class |
param_name |
A character vector of length 1; the name of the parameter to be interpolated according to. |
param0, param1
|
The values of the parameter to take as the fixed points for interpolation. |
Coefficients are taken to have the values in coef0 at param = param0, and the values in coef1 at param = param1, and are linearly interpolated between.
Each parameter in the vector/matrix is interpolated independently.
S3 object of class rescomp_coefs_vector or rescomp_coefs_matrix, according to the types of coefs0 and coefs1.
coefs_vec <- rescomp_coefs_lerp( c(0, 0, 0), c(2, 3, 5), "scale" ) get_coefs(coefs_vec, list(scale = 0)) get_coefs(coefs_vec, list(scale = 0.5)) get_coefs(coefs_vec, list(scale = 1)) get_coefs(coefs_vec, list(scale = 1.5)) coefs_mat1 <- rescomp_coefs_lerp( matrix(c(0.2, 0.4, 0.3, 0.2), nrow = 2), matrix(c(0.2, 0.1, 0.3, 0.1), nrow = 2), "antibiotic1_concentration", param0 = 0, param1 = 200 ) get_coefs(coefs_mat1, list(antibiotic1_concentration = 0)) get_coefs(coefs_mat1, list(antibiotic1_concentration = 100)) get_coefs(coefs_mat1, list(antibiotic1_concentration = 200)) coefs_mat2 <- rescomp_coefs_lerp( coefs_mat1, matrix(c(0.0, 0.0, 0.0, 0.0), nrow = 2), "antibiotic2_concentration", param0 = 0, param1 = 100 ) get_coefs(coefs_mat2, list(antibiotic1_concentration = 100, antibiotic2_concentration = 50))coefs_vec <- rescomp_coefs_lerp( c(0, 0, 0), c(2, 3, 5), "scale" ) get_coefs(coefs_vec, list(scale = 0)) get_coefs(coefs_vec, list(scale = 0.5)) get_coefs(coefs_vec, list(scale = 1)) get_coefs(coefs_vec, list(scale = 1.5)) coefs_mat1 <- rescomp_coefs_lerp( matrix(c(0.2, 0.4, 0.3, 0.2), nrow = 2), matrix(c(0.2, 0.1, 0.3, 0.1), nrow = 2), "antibiotic1_concentration", param0 = 0, param1 = 200 ) get_coefs(coefs_mat1, list(antibiotic1_concentration = 0)) get_coefs(coefs_mat1, list(antibiotic1_concentration = 100)) get_coefs(coefs_mat1, list(antibiotic1_concentration = 200)) coefs_mat2 <- rescomp_coefs_lerp( coefs_mat1, matrix(c(0.0, 0.0, 0.0, 0.0), nrow = 2), "antibiotic2_concentration", param0 = 0, param1 = 100 ) get_coefs(coefs_mat2, list(antibiotic1_concentration = 100, antibiotic2_concentration = 50))
Produces an object that may be used in place of a matrix in the creation of arguments to spec_rescomp().
rescomp_coefs_matrix_custom(func, nrow, ncol)rescomp_coefs_matrix_custom(func, nrow, ncol)
func |
A function that takes |
nrow |
The number of rows in the matrix returned by |
ncol |
The number of columns in the matrix returned by |
S3 object of class rescomp_coefs_matrix.
# Matrix with one time-varying coefficient rescomp_coefs_matrix_custom( function(params) { matrix(c(0.2, 0.3, 0.4, params$fourth_coeff), nrow = 2, ncol = 2) }, nrow = 2, ncol = 2 )# Matrix with one time-varying coefficient rescomp_coefs_matrix_custom( function(params) { matrix(c(0.2, 0.3, 0.4, params$fourth_coeff), nrow = 2, ncol = 2) }, nrow = 2, ncol = 2 )
Produces an object that may be used in place of a vector in the creation of arguments to spec_rescomp().
rescomp_coefs_vector_custom(func, length)rescomp_coefs_vector_custom(func, length)
func |
A function that takes |
length |
The length of the vector returned by |
S3 object of class rescomp_coefs_vector.
# Vector with one time-varying coefficient rescomp_coefs_vector_custom( function(params) { c(0.2, 0.3, 0.4, params$fourth_coeff) }, length = 4 )# Vector with one time-varying coefficient rescomp_coefs_vector_custom( function(params) { c(0.2, 0.3, 0.4, params$fourth_coeff) }, length = 4 )
Produces an object suitable to include in a list of rescomp parameters, providing time-dependence.
rescomp_param_custom(func, display_values = NULL)rescomp_param_custom(func, display_values = NULL)
func |
A function that takes |
display_values |
A numeric vector of values of the parameter to use in |
S3 object of class rescomp_param.
# Repeated pulsing and exponential decay antibiotic_conc <- rescomp_param_custom(function(t) { 0.5^(10 * (t %% 1)) }) get_params(antibiotic_conc, 0) get_params(antibiotic_conc, 0.5) get_params(antibiotic_conc, 1)# Repeated pulsing and exponential decay antibiotic_conc <- rescomp_param_custom(function(t) { 0.5^(10 * (t %% 1)) }) get_params(antibiotic_conc, 0) get_params(antibiotic_conc, 0.5) get_params(antibiotic_conc, 1)
Produces an object suitable to include in a list of rescomp parameters, providing time-dependence. Triangle and square waves are phase-shifted to be similiar in shape to a sine wave with the same period and offset, such that the peaks and troughs occur in the same places.
rescomp_param_sine( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) ) rescomp_param_triangle( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) ) rescomp_param_square( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) )rescomp_param_sine( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) ) rescomp_param_triangle( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) ) rescomp_param_square( period = 1, min = 0, max = 1, offset = 0, display_values = c(min, max) )
period |
The period of the wave. |
min |
The minimum value of the parameter; the mean minus the amplitude. |
max |
The maximum value of the parameter; the mean plus the amplitude. |
offset |
The phase shift of the wave. For a sine or triangle wave the time at which its value is equal to the mean and increasing. For a square wave, the time at which it increases to the maximum value. |
display_values |
A numeric vector of values of the parameter to use in |
S3 object of class rescomp_param.
sine <- rescomp_param_sine(period = 1) cosine <- rescomp_param_sine(period = 1, offset = -0.25) triangle <- rescomp_param_triangle(period = 1) square <- rescomp_param_square(period = 1) times <- seq(from = 0, to = 2, by = 0.01) plot(times, get_params(sine, times), type = "l", col = "black") lines(times, get_params(cosine, times), col = "blue") lines(times, get_params(triangle, times), col = "maroon3") lines(times, get_params(square, times), col = "orange")sine <- rescomp_param_sine(period = 1) cosine <- rescomp_param_sine(period = 1, offset = -0.25) triangle <- rescomp_param_triangle(period = 1) square <- rescomp_param_square(period = 1) times <- seq(from = 0, to = 2, by = 0.01) plot(times, get_params(sine, times), type = "l", col = "black") lines(times, get_params(cosine, times), col = "blue") lines(times, get_params(triangle, times), col = "maroon3") lines(times, get_params(square, times), col = "orange")
Produces an object suitable to pass as the ressupply to spec_rescomp().
ressupply_chemostat(dilution, concentration)ressupply_chemostat(dilution, concentration)
dilution |
A numeric vector or |
concentration |
A vector or |
S3 object of class rescomp_ressupply.
ressupply <- ressupply_chemostat( dilution = 0.01, concentration = rescomp_coefs_lerp(c(0, 0, 0), c(2, 3, 4), "ressupply_scaling") ) get_ressupply(ressupply, c(2, 4, 10), list(ressupply_scaling = 0)) get_ressupply(ressupply, c(3, 3, 3), list(ressupply_scaling = 1))ressupply <- ressupply_chemostat( dilution = 0.01, concentration = rescomp_coefs_lerp(c(0, 0, 0), c(2, 3, 4), "ressupply_scaling") ) get_ressupply(ressupply, c(2, 4, 10), list(ressupply_scaling = 0)) get_ressupply(ressupply, c(3, 3, 3), list(ressupply_scaling = 1))
Produces an object suitable to pass as the ressupply to spec_rescomp().
ressupply_constant(rate)ressupply_constant(rate)
rate |
A vector or |
S3 object of class rescomp_ressupply.
ressupply <- ressupply_constant(c(0.2, 0.3)) get_ressupply(ressupply, c(2, 10), list()) get_ressupply(ressupply, c(5, 20), list()) # The above two give the same result; constant supply doesn't depend on existing concentration. ressupply <- ressupply_constant(rescomp_coefs_lerp(c(0.2, 0.3), c(0.4, 0.6), "extra_supply")) get_ressupply(ressupply, c(2, 10), list(extra_supply = 0.2)) get_ressupply(ressupply, c(2, 10), list(extra_supply = 0.8))ressupply <- ressupply_constant(c(0.2, 0.3)) get_ressupply(ressupply, c(2, 10), list()) get_ressupply(ressupply, c(5, 20), list()) # The above two give the same result; constant supply doesn't depend on existing concentration. ressupply <- ressupply_constant(rescomp_coefs_lerp(c(0.2, 0.3), c(0.4, 0.6), "extra_supply")) get_ressupply(ressupply, c(2, 10), list(extra_supply = 0.2)) get_ressupply(ressupply, c(2, 10), list(extra_supply = 0.8))
Produces an object suitable to pass as the ressupply to spec_rescomp().
ressupply_custom(func, resnum = NULL)ressupply_custom(func, resnum = NULL)
func |
A function that takes |
resnum |
The number of resources; the expected length of the |
If resnum is NULL, spec_rescomp() will attempt to infer it.
This is fine if the result is passed directly to spec_rescomp(), but may fail if the result must be combined with other rescomp_ressupply first.
S3 object of class rescomp_ressupply.
# Two resources, A and B, with constant supply of A, and A spontaneously converting to B ressupply <- ressupply_custom( function(resources, params) { conversion <- params$conversion * resources[1] return(c(params$supply - conversion, conversion)) }, resnum = 2 ) get_ressupply(ressupply, c(10, 20), list(supply = 3, conversion = 0.2))# Two resources, A and B, with constant supply of A, and A spontaneously converting to B ressupply <- ressupply_custom( function(resources, params) { conversion <- params$conversion * resources[1] return(c(params$supply - conversion, conversion)) }, resnum = 2 ) get_ressupply(ressupply, c(10, 20), list(supply = 3, conversion = 0.2))
Produces an object suitable to pass as the ressupply to spec_rescomp().
ressupply_logistic(r, k)ressupply_logistic(r, k)
r |
A vector or |
k |
A vector or |
S3 object of class rescomp_ressupply.
ressupply <- ressupply_logistic( r = rescomp_coefs_lerp(c(0.2, 0.3), c(0, 0), "growth_inhibition"), k = c(10, 20) ) get_ressupply(ressupply, c(2, 10), list(growth_inhibition = 0)) get_ressupply(ressupply, c(20, 0), list(growth_inhibition = 0)) get_ressupply(ressupply, c(2, 10), list(growth_inhibition = 0.8))ressupply <- ressupply_logistic( r = rescomp_coefs_lerp(c(0.2, 0.3), c(0, 0), "growth_inhibition"), k = c(10, 20) ) get_ressupply(ressupply, c(2, 10), list(growth_inhibition = 0)) get_ressupply(ressupply, c(20, 0), list(growth_inhibition = 0)) get_ressupply(ressupply, c(2, 10), list(growth_inhibition = 0.8))
deSolve::ode())Simulate resource competition (a convenience wrapper for
deSolve::ode())
sim_rescomp(pars, totaltime, cinit, rinit, ...)sim_rescomp(pars, totaltime, cinit, rinit, ...)
pars |
S3 object of class |
totaltime |
Numeric vector of length 1: the total simulation time.
If provided, overrides the value in |
cinit |
Numeric vector of length 1 or length |
rinit |
Numeric vector of length 1 or length |
... |
Other arguments passed to |
A list of two comprising i) the model dynamics and ii) model specifications.
pars <- spec_rescomp() results1 <- sim_rescomp(pars = pars) plot_rescomp(results1) results2 <- sim_rescomp(pars = pars, totaltime = 100, cinit = 1000) plot_rescomp(results2)pars <- spec_rescomp() results1 <- sim_rescomp(pars = pars) plot_rescomp(results1) results2 <- sim_rescomp(pars = pars, totaltime = 100, cinit = 1000) plot_rescomp(results2)
sim_rescomp()
Generate list of parameters for a consumer-resource model to be
passed to sim_rescomp()
spec_rescomp( spnum = 1, resnum = 1, funcresp = funcresp_type1(crmatrix(0.1)), quota = crmatrix(0.001), efficiency, essential = FALSE, mort = 0.03, ressupply = ressupply_chemostat(0.03, 1), params = list(), events = list(), totaltime = 1000, cinit = 10, rinit = 1, verbose = FALSE )spec_rescomp( spnum = 1, resnum = 1, funcresp = funcresp_type1(crmatrix(0.1)), quota = crmatrix(0.001), efficiency, essential = FALSE, mort = 0.03, ressupply = ressupply_chemostat(0.03, 1), params = list(), events = list(), totaltime = 1000, cinit = 10, rinit = 1, verbose = FALSE )
spnum |
Integer vector of length 1: the number of consumers. |
resnum |
Integer vector of length 1: the number of resources. |
funcresp |
An object of class |
quota |
Numeric matrix or |
efficiency |
Numeric matrix or |
essential |
Logical vector of length 1. If FALSE resources are substitutable. |
mort |
Numeric vector or |
ressupply |
An object of class |
params |
A list specifying a set of parameters which may vary with time,
on which other parameters of the model (e.g. funcresp, ressupply) may depend.
|
events |
A list of objects of class |
totaltime |
Numeric vector of length 1: the total simulation time. |
cinit |
Numeric vector of length 1 or length |
rinit |
Numeric vector of length 1 or length |
verbose |
If TRUE (default) prints model and simulation summary specs. |
Only one of efficiency and quota should be specified. Specifying both is an error.
The default, if neither is specified, is to use quota.
If using quota, the functional responses are taken to give per capita growth rates.
If using efficiency, the functional responses are taken to give attack rates.
S3 object of class rescomp.
# Using default parameters. m1 <- spec_rescomp() plot_rescomp(sim_rescomp(m1)) # With two species, two resources, and type 2 functional responses. m2 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type2( a = crmatrix( 0.1, 0.2, 0.15, 0.15 ), h = crmatrix(1) ), totaltime = 500 ) plot_rescomp(sim_rescomp(m2)) # With serial dilution (batch transfer) events. m3 <- spec_rescomp( ressupply = ressupply_constant(0), mort = 0, events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 125 ) ), totaltime = 500 ) plot_rescomp(sim_rescomp(m3)) # Growth rates affected by seasonal temperature fluctuations. m4 <- spec_rescomp( spnum = 2, funcresp = funcresp_type1( a = rescomp_coefs_lerp( crmatrix(0.12, 0.08), crmatrix(0.08, 0.12), param_name = "temperature" ) ), params = list(temperature = rescomp_param_sine(period = 125)), totaltime = 500 ) plot_rescomp(sim_rescomp(m4))# Using default parameters. m1 <- spec_rescomp() plot_rescomp(sim_rescomp(m1)) # With two species, two resources, and type 2 functional responses. m2 <- spec_rescomp( spnum = 2, resnum = 2, funcresp = funcresp_type2( a = crmatrix( 0.1, 0.2, 0.15, 0.15 ), h = crmatrix(1) ), totaltime = 500 ) plot_rescomp(sim_rescomp(m2)) # With serial dilution (batch transfer) events. m3 <- spec_rescomp( ressupply = ressupply_constant(0), mort = 0, events = list( event_schedule_periodic( event_batch_transfer(dilution = 0.1, resources = 1), period = 125 ) ), totaltime = 500 ) plot_rescomp(sim_rescomp(m3)) # Growth rates affected by seasonal temperature fluctuations. m4 <- spec_rescomp( spnum = 2, funcresp = funcresp_type1( a = rescomp_coefs_lerp( crmatrix(0.12, 0.08), crmatrix(0.08, 0.12), param_name = "temperature" ) ), params = list(temperature = rescomp_param_sine(period = 125)), totaltime = 500 ) plot_rescomp(sim_rescomp(m4))